0
点赞
收藏
分享

微信扫一扫

717. 1比特与2比特字符 / 18. 四数之和 / 22. 括号生成

魔都魅影梅杜萨 2022-02-20 阅读 61

717. 1比特与2比特字符【简单题】【每日一题】

思路:

代码:

class Solution {
    public boolean isOneBitCharacter(int[] bits) {
        int len = bits.length,i = 0;
        while (i<len){
            if (bits[i] == 1){
                i+=2;
            }else {
                if (i == len-1){
                    return true;
                }else {
                    i++;
                }
            }
        }
        return false;
    }
}

用时:

在这里插入图片描述


18. 四数之和【中等题】

思路:

代码:
直接放第4版的代码了。

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        int len = nums.length;
        if (len<4){
            return ans;
        }
        Arrays.sort(nums);
        for (int i = 0; i < len-3; i++) {
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            long before4_1 = (long) nums[i]+nums[i+1]+nums[i+2]+nums[i+3];
            if (before4_1 > target){
                break;
            }else if (before4_1 == target){
                ans.add(Arrays.asList(nums[i],nums[i+1],nums[i+2],nums[i+3]));
                continue;
            }
            if ((long)nums[i]+nums[len-1]+nums[len-2]+nums[len-3] < target){
                continue;
            }
            for (int j = i+1; j < len-2; j++) {
                if(j > i+1 && nums[j] == nums[j-1]){
                    continue;
                }
                int left = j+1,right = len-1;
                long pre = (long) nums[i]+nums[j],before4_2 = pre + nums[left]+nums[left+1];
                if (before4_2 > target){
                    break;
                }
                if (pre +nums[right]+nums[right-1] < target){
                    continue;
                }
                while (left<right){
                    int cur = nums[left]+nums[right];
                    if (pre+cur<target){
                        left++;
                    }else if (pre+cur>target){
                        right--;
                    }else {
                        ans.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
                        while (left < right && nums[left] == nums[left + 1]) {
                            left++;
                        }
                        left++;
                        while (left < right && nums[right] == nums[right - 1]) {
                            right--;
                        }
                        right--;
                    }
                }
            }
        }
        return ans;
    }
}

用时:

在这里插入图片描述


22. 括号生成【中等题】

思路:

代码:

class Solution {
    public List<String> generateParenthesis(int n) {
        Set<String> ans = new HashSet<>();
        ans.add("()");
        for (int i = 2; i <= n; i++) {
            Set<String> temp = new HashSet<>();
            for (String str : ans){
                for (int j = 0; j < str.length(); j++) {
                    temp.add(str.substring(0,j)+"()"+str.substring(j));
                }
            }
            ans = temp;
        }
        return new ArrayList<>(ans);
    }
}

用时:

在这里插入图片描述

举报

相关推荐

0 条评论