【LEETCODE】60、數組分類,適中級別,題目:75、560、105

package y2019.Algorithm.array.medium;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: SortColors
 * @Author: xiaof
 * @Description: TODO 75. Sort Colors
 * Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent,
 * with the colors in the order red, white and blue.
 * Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
 *
 * Input: [2,0,2,1,1,0]
 * Output: [0,0,1,1,2,2]
 *
 * 給定一個包含紅色、白色和藍色,一共 n 個元素的數組,原地對它們進行排序,使得相同顏色的元素相鄰,並按照紅色、白色、藍色順序排列。
 * 此題中,我們使用整數 0、 1 和 2 分別表示紅色、白色和藍色。
 * 來源:力扣(LeetCode)
 * 鏈接:https://leetcode-cn.com/problems/sort-colors
 * 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
 *
 * 大神答案參考:https://leetcode.com/problems/sort-colors/discuss/26472/Share-my-at-most-two-pass-constant-space-10-line-solution
 *
 * @Date: 2019/7/19 9:03
 * @Version: 1.0
 */
public class SortColors {

    public void solution(int[] nums) {
        //這裏進行hash放置的話,可能會產生衝突,那麼就需要把hash值依次往後排,然後和對應的值交換位置
        //針對這題,因爲只有三類數據,那麼0再最開頭,2再最末尾,其餘的就會被放到中間
        int index0 = 0, index2 = nums.length - 1;
        for(int i = 0; i <= index2; ++i) {
            while(nums[i] == 2 && i < index2) {
                //當指定位置是2,那麼我們吧他交換到指定的2的位置
                //並且這裏賦值字後,如果值改變了,那麼就會跳出while循環,NB
                nums[i] = nums[index2];
                nums[index2--] = 2;
            }
            //然後交換0的位置
            while(nums[i] == 0 && i > index0) {
                nums[i] = nums[index0];
                nums[index0++] = 0;
            }
        }

    }

    public static void main(String[] args) {
        int data[] = {2,0,2,1,1,0};
        SortColors fuc = new SortColors();
        fuc.solution(data);
        System.out.println();
    }
}

 

package y2019.Algorithm.array.medium;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: SubarraySum
 * @Author: xiaof
 * @Description: TODO 560. Subarray Sum Equals K
 *
 * Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
 * Input:nums = [1,1,1], k = 2
 * Output: 2
 *
 * @Date: 2019/7/19 9:50
 * @Version: 1.0
 */
public class SubarraySum {

    public int solution(int[] nums, int k) {
        //查子串和,那麼我們遍歷這個數組,然後求子串值,如果和超了,那麼就把最前面的數剔除掉,然後再比較,小了就往後
        int sum = 0, count = 0;
        Map<Integer, Integer> sumMap = new HashMap();
        sumMap.put(0, 1); //和爲0的情況那麼就是單獨一個元素
        //由於是求子串,子串是連續的,那麼我們只要求出i->j連續的和是k即可
        //而i->j = 0->j - 0->i 的差值,轉而言之,我們需要把k + {0->i} = {0->j}求出來個數即可
        for(int i = 0; i < nums.length; ++i) {
            sum += nums[i];
            //這裏不需要考慮後面沒有put進去的sum,因爲我們要求i->j的和,只要判斷前面的數據就可以了,後面的和放在後面比較
            if(sumMap.containsKey(sum - k)) {
                //那麼就是存在
                count += sumMap.get(sum - k);
            }
            //如果不包含,那麼就把值放入,進行下一個子串的遍歷
            sumMap.put(sum, sumMap.getOrDefault(sum, 0) + 1);
        }


        return count;
    }
}

 

package y2019.Algorithm.array.medium;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: BuildTree
 * @Author: xiaof
 * @Description: TODO 105. Construct Binary Tree from Preorder and Inorder Traversal
 *  Given preorder and inorder traversal of a tree, construct the binary tree.
 *
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 *
 * preorder = [3,9,20,15,7]
 * inorder = [9,3,15,20,7]
 *
 *      3
 *    / \
 *   9  20
 *     /  \
 *    15   7
 *
 * @Date: 2019/7/19 10:31
 * @Version: 1.0
 */
public class BuildTree {

    public class TreeNode {
        private int value;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(int x) {
            this.value = x;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }
    }

    public TreeNode solution(int[] preorder, int[] inorder) {
        //根據前序遍歷和中序組建一顆樹
        //探索類,考慮遞歸
        return backTrack(preorder, 0, inorder, 0, inorder.length - 1);

    }

    public TreeNode backTrack(int[] preorder, int preLeft, int[] inorder, int inLeft, int inRight) {
        if(preLeft > preorder.length - 1 || inLeft > inRight) {
            return null;
        }
        //如果沒有,我們可以吧當前樹的前序遍歷第一個當成根
        TreeNode curRoot = new TreeNode(preorder[preLeft]);
        int midIndex = 0;
        //然後我們去中序尋找中間點
        for(int i = inLeft; i <= inRight; ++i) {
            if(preorder[preLeft] == inorder[i]) {
                //找到位置
                midIndex = i;
            }
        }

        //中分兩邊中序
        //左子樹
        curRoot.setLeft(backTrack(preorder, preLeft + 1, inorder, inLeft, midIndex - 1));
        curRoot.setRight(backTrack(preorder, preLeft + midIndex + 1 - inLeft, inorder, midIndex + 1, inRight));
        return curRoot;
    }


    public static void main(String[] args) {
        int preorder1[] = {3,9,20,15,7};
        int inorder1[] = {9,3,15,20,7};
        BuildTree fuc = new BuildTree();
        fuc.solution(preorder1, inorder1);
        System.out.println();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章