【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();
    }
}

 

posted @ 2019-07-19 11:10  cutter_point  阅读(206)  评论(0编辑  收藏  举报