每日算法--2023.2.4

1.回文子串

class Solution {
    public int countSubstrings(String s) {
        int res = 0 , n = s.length();
        for(int i = 0;i<n;i++){
            int l = i, r  = i;
            while(l>=0&&r<n){
                if(s.charAt(l--) == s.charAt(r++)){
                    res++;
                }else{
                    break;
                }
            }
            int left = i, right = i+1;
            while(left>=0&&right<n){
                if(s.charAt(left--) == s.charAt(right++)){
                    res++;
                }else{
                    break;
                }
            }
        }
        return res;
    }
}

2.任务调度器

class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] cnts = new int[26];
        for (char c : tasks) cnts[c - 'A']++;
        int max = 0, tot = 0;
        for (int i = 0; i < 26; i++) max = Math.max(max, cnts[i]);
        for (int i = 0; i < 26; i++) tot += max == cnts[i] ? 1 : 0;
        return Math.max(tasks.length, (n + 1) * (max - 1) + tot);
    }
}

3.合并二叉树

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null){
            return root2;
        }else if(root2 == null){
            return root1;
        }

        TreeNode res = new TreeNode(root1.val+root2.val);
        res.left = mergeTrees(root1.left,root2.left);
        res.right = mergeTrees(root1.right,root2.right);
        return res;

    }
}

4.最短无序连续子数组

class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int n = nums.length;
        int[] temp = new int[n];
        for(int i = 0;i<n;i++){
            temp[i] = nums[i];
        }
        Arrays.sort(temp);
        int left = 0, right = n-1;
        while(left<right){
            if(nums[left]!=temp[left]){
                //System.out.println(left);
                break;
            }
            left++;
        }
        while(left<right){
            //System.out.println(right);
            if(nums[right]!=temp[right]){
                //System.out.println(right);
                break;
            }
            right--;
        }
        if(left>=right){
            return 0;
        }
        return right-left+1;

    }
}

5.和为k的子数组

class Solution {
    public int subarraySum(int[] nums, int k) {
        //前缀和+哈希表,因为这里有负数,不同的前缀和可能有相同的值,所以用哈希表和不是set
        HashMap<Integer, Integer> map = new HashMap<>();
        //前缀和的值为k,提前初始化
        map.put(0,1);
        int n = nums.length, cnt = 0;
        int[] addNums = new int[n+1]; 
        for(int i = 1;i<=n;i++){
            //计算到i的前缀和
            addNums[i] = addNums[i-1] + nums[i-1];
            //如果addNums[i]-k的值,之前的前缀和有则有答案返回
            if(map.containsKey(addNums[i] - k)){
                cnt += map.get(addNums[i]-k);
            }
            //最后都要将该前缀和加入到哈希表中,
            map.put(addNums[i],map.getOrDefault(addNums[i], 0)+1);
        }
        return cnt;
    }
}

6.二叉树的直径

class Solution {
    public int res = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        if(root == null){
            return 0;
        }
        if(root.left == null&&root.right==null){
            return 0;
        }
        int temp = 0;
        if(root.left!=null){
            int left = MaxLength(root.left);
            temp += left+1;
        }
        if(root.right!=null){
            int right = MaxLength(root.right);
            temp += right+1;
        }
        return Math.max(res,temp);
    }
    public int MaxLength(TreeNode root){
        if(root.left == null&&root.right==null){
            return 0;
        }
        int temp = 0,left = 0,right = 0;
        if(root.left!=null){
            left = MaxLength(root.left);
            temp += left+1;
        }
        if(root.right!=null){
            right = MaxLength(root.right);
            temp += right+1;
        }
        res = Math.max(res,temp);
        return Math.max(left,right)+1;

    }

}

7.找到所有数组中消失的数字

class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        int n = nums.length;
        for(int i = 0;i<n;i++){
            int temp = nums[i];
            if(nums[i]<0){
                temp = -nums[i];
            }
            if(nums[temp-1]>0){
                nums[temp-1] = -nums[temp-1];
            }
        }
        List<Integer> res = new LinkedList<>();
        for(int i = 0;i<n;i++){
            if(nums[i]>0){
                res.add(i+1);
            }
        }
        return res;
    }
}

  

  

  

  

  

  

posted @ 2023-02-05 00:26  lyjps  阅读(15)  评论(0)    收藏  举报