LeetCode热题100道-day02

128 阅读1分钟

LeetCode热题100道-day02

1. 盛最多水的容器

11. 盛最多水的容器

  • 初见,暴力解法

  • 不出意外就出意外了,超时了

class Solution {  
    // 暴力破解,超时了
    public int maxArea(int[] height) {  
        int len = height.length;  
        if (len < 2) {  
            return 0;  
        }  
        int capacity = 0;  
        for (int i = 0; i < len - 1; i++) {  
            for (int j = i + 1; j < len; j++) {  
                int getCapacity = (j - i) * (Math.min(height[i], height[j]));  
                capacity = Math.max(getCapacity, capacity);  
            }  
        }  
        return capacity;  
    }  
}
  • 双指针
    • 水池的容量由底边和挡板的高度决定,底边会一直减小,而高可能变大或变小,向内移动高挡板,由于容量由矮挡板的高度决定,所以容量一定减少,而移动低挡板,容量可能不变或者增加。
    • 则,当左挡板高度大于右挡板高度,就移动高度低挡板(右挡板),反之亦然
public static int maxArea(int[] height) {  
    int l = 0, r = height.length - 1;  
    int ans = 0;  
    while (l < r) {  
        int capacity = (r - l) * (Math.min(height[l], height[r]));  
        ans = Math.max(capacity, ans);  
        if (height[l] > height[r]) {  
            r--;  
        } else {  
            l++;  
        }  
    }  
    return ans;  
}

2. 三数之和

15. 三数之和

  • new集合,排序
  • 循环
    • 大于0直接结束循环,因为排过序
    • 去重:nums[i] == nums[i-1] continue
    • 特殊情况判断 =null ,<3
    • L = i+1 ;R = len -1;
    • 循环
      • 求sum
      • if (sum == 0) 加入到集合 去重复结果↓
        • 循环
          • (L<R && nums[L] == nums[L+1]) L++;
        • 循环:
          • (L<R && nums[R] == nums[R-1]) R--;
        • L++; R--;
      • else if (sum < 0) L++;
      • else if (sum > 0) R--;
class Solution {  
    public List<List<Integer>> threeSum(int[] nums) {  
        ArrayList<List<Integer>> ans = new ArrayList<>();  
        int len = nums.length;  
        if(nums == null || len < 3) return ans;  
        Arrays.sort(nums);  
        for (int i = 0; i < len; i++) {  
            if (nums[i] > 0) break;  
            if (i > 0 && nums[i] == nums[i - 1]) continue;  
            int L = i + 1;  
            int R = len - 1;  
            while (L < R) {  
                int sum = nums[i] + nums[L] + nums[R];  
                if (sum == 0) {  
                    ans.add(Arrays.asList(nums[i], nums[L], nums[R]));  
                    while (L < R && nums[L] == nums[L + 1]) L++;  
                    while (L < R && nums[R] == nums[R - 1]) R--;  
                    L++;  
                    R--;  
                }else if (sum < 0){  
                    L++;  
                }else if (sum > 0){  
                    R--;  
                }  
            }  
        }  
        return ans;  
    }  
}

3. 电话号码的字母组合

17. 电话号码的字母组合

  • 回溯
class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> combinations = new ArrayList<String>();
        if (digits.length() == 0) {
            return combinations;
        }
        Map<Character, String> phoneMap = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        backtrack(combinations, phoneMap, digits, 0, new StringBuffer());
        return combinations;
    }

    public void backtrack(List<String> combinations, Map<Character, String> phoneMap, String digits, int index, StringBuffer combination) {
        if (index == digits.length()) {
            combinations.add(combination.toString());
        } else {
            char digit = digits.charAt(index);
            String letters = phoneMap.get(digit);
            int lettersCount = letters.length();
            for (int i = 0; i < lettersCount; i++) {
                combination.append(letters.charAt(i));
                backtrack(combinations, phoneMap, digits, index + 1, combination);
                combination.deleteCharAt(index);
            }
        }
    }
}