LeetCode滑動窗口和雙指針高頻題(三)

我是方圓,無它,唯手熟爾

3. 無重複字符的最長子串(滑動窗口)

在這裏插入圖片描述

class Solution{
	public int lengthOfLongestSubstring(String s){
		int len = s.length();
		int ans = 0;
		//key用來存儲具體的字符,value用來存儲重複字符索引的下一位
		HashMap<Character,Integer> map = new HashMap<>();
		
		for(int start = 0,end = 0;end < len;end++){
			//判空
			if(len == 0)
				return 0;

			//正式開始
			if(map.containsKey(s.charAt(end)){
				//這裏必須取最大值,避免start左移的情況
				//特殊例子,a..b..a..b 
				//假設start在第二個a處,直接map.get(b)的話,會左移
				start = Math.max(start,map.get(s.charAt(end)));
			}
			map.put(s.charAt(end),end + 1);
			ans = Math.max(ans,end - start);
		}	
		return ans;
	}
}

11. 盛最多水的容器(雙指針)

在這裏插入圖片描述

class Solution{
	public int maxArea(int[] height){
		int ans = 0;
		int strat = 0;
		int end = height.length - 1;
	
		while(start < end){
			ans = Math.max(ans,(end - start)*Math.min(height[start],height[end]));

			if(height[start] < height[end]){
				start++;
			}else{
				end--;
			}
		}
		return ans;
	}
}

15. 三數之和(雙指針)

在這裏插入圖片描述

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<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[R] + nums[L];
                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){
                    R--;
                }else{
                    L++;
                }
            }
        }
        return ans;
    }
}

16. 最接近的三數之和(雙指針)

在這裏插入圖片描述

class Solution {
    public int threeSumClosest(int[] nums, int target) {
    	//排序,雙指針問題一般都要排序
    	//這樣可以確定指針的移動條件
        Arrays.sort(nums);
        int len = nums.length;
        int ans = nums[0] + nums[1] + nums[2];

        for(int i = 0;i < len;i++){
            int L = i + 1;
            int R = len - 1;
            while(L < R){
                int sum = nums[i] + nums[L] + nums[R];
                
                if(Math.abs(sum - target) < Math.abs(ans - target)){
                    ans = sum;
                }
                if(sum > target){
                    R--;
                }else if(sum < target){
                    L++;
                }else{
                    return ans;
                }
            }
        }
        return ans;
    }
}

26. 刪除排序數組中的重複項(雙指針)

在這裏插入圖片描述

class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums == null || nums.length == 0){
            return 0;
        }
		//最後p+1的值即爲所求
        int p = 0;
        int q = 1;
        while(q < nums.length){
        	//當p和q所指的值不相等的時候,讓p的下一個值等於q
        	//並且p指針後移一位
            if(nums[p] != nums[q]){
                nums[p+1] = nums[q];
                p++;
            }
            q++;
        }
        return p+1;
    }
}

121. 買賣股票的最佳時機

在這裏插入圖片描述

class Solution {
    public int maxProfit(int[] prices) {
    	
        int len = prices.length;
        //找出最低的股價
        int minPrice = Integer.MAX_VALUE;
        int ans = 0;

        for(int i = 0;i < len;i++){

            if(minPrice > prices[i]){
                minPrice = prices[i];
            }else if(minPrice < prices[i]){
                ans = Math.max(ans,prices[i] - minPrice);
            }
        }
        return ans;
    }
}

209. 長度最小的子數組(滑動窗口)

在這裏插入圖片描述

class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        int left = 0;
        int sum = 0;
        int ans = Integer.MAX_VALUE;

        for(int right = 0;right < nums.length;right++){
            sum += nums[right];
            while(sum >= s){
                ans = Math.min(ans,right - left + 1);
                sum -= nums[left++];
            }
        }
        return ans == Integer.MAX_VALUE?0:ans;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章