Leetcoe高频题(三) 我就不信邪了 LeetCode滑动窗口和双指针高频题

3. 无重复字符的最长子串(滑动窗口)

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

     //数组记录字母出现频率
        int freq[]=new int[256];
        int l=0;
        int r=-1;
        int n=s.length();  
        int max=0;


        while(l<n){
    
     
          if(r+1<n&&freq[s.charAt(r+1)]==0){
    
    
                freq[s.charAt(++r)]++;  
          }
          //如果s.charAt(r+1)没出现过,freq[s.charAt(++r)]++
           else{
    
    
               freq[s.charAt(l)]--;
               l++;       
          }
          max=Math.max(max,r-l+1);
        }
            return max==0?0:max;

11. 盛最多水的容器(双指针)

在这里插入图片描述

 public int maxArea(int[] height) {
    
    

        int l=0;
        int r=height.length-1;
        int res=0;
//由于是找最大容水量,如果height[l]<height[r],l++;否则r--
        while(l<r){
    
    
           if(Math.min(height[r],height[l])*(r-l)>res){
    
    
               res=Math.min(height[r],height[l])*(r-l);
           }
           if(height[l]>height[r]) r--;
           else l++; 
        }  
            return res;
    }

15. 三数之和(双指针)

在这里插入图片描述

class Solution {
    
    
   
    public List<List<Integer>> threeSum(int[] nums) {
    
    
        
      List<List<Integer>> res=new ArrayList<>();
        int n=nums.length;
        if(n<3) return res;
        Arrays.sort(nums);
 

        for(int i=0;i<nums.length;i++){
    
    
            if(nums[i]>0) break; 
            if(i>0&&nums[i]==nums[i-1]) continue;            //判断每一轮nums[i]和nums[i-1]是不是一样

            int target=0-nums[i];
            int l=i+1;
            int r=nums.length-1;
            while(l<r){
    
          //注意这里界限,是从左到右
               if(nums[l]+nums[r]==target){
    
    
                  res.add(new ArrayList(Arrays.asList(nums[i],nums[l],nums[r])));
                   l++;
                   r--;
               while(l<r&&nums[l]==nums[l-1]) l++;           //判断同一轮nums[l]和nums[l-1]是不是一样
               while(l<r&&nums[l]==nums[r+1]) r--;
               }
                else if(nums[l]+nums[r]<target) l++;
                else r--;  
            } 
        }
            return res;

    }
}

16. 最接近的三数之和(双指针)

在这里插入图片描述

class Solution {
    
    
    public int threeSumClosest(int[] nums, int target) {
    
    
             
              int n=nums.length;
              if(n<3) return -1;
              Arrays.sort(nums);   
              int closeSum=nums[0]+nums[1]+nums[2];
              int min=Math.abs(target-closeSum);
               

              for(int i=0;i<nums.length;i++){
    
    
              int l=i+1;
              int r=n-1; 
              while(l<r){
    
     
                 int sum=nums[i]+nums[l]+nums[r]; 
                 if(sum==target) return target; 
                 else if(sum<target) {
    
    
                     l++;
                 }
                 else if(sum>target) {
    
    
                     r--;
                 } 
                if(min>Math.abs(sum-target)){
    
    
                    min=Math.abs(sum-target);
                     closeSum=sum;   
                }  
              }  
          }
 
          return closeSum;
    }
}

26. 删除排序数组中的重复项(双指针)

在这里插入图片描述

 public int removeDuplicates(int[] nums) {
    
    

         if(nums.length==0||nums==null) return 0;

       int l=0;
       int r=0;
       int n=nums.length;  
  

       while(l<n&&r<n){
    
    
         if(nums[l]==nums[r]){
    
    
             r++;
         }else{
    
    
         //[1,1,2]  左指针先右移一位,在和右指针交换
             nums[++l]=nums[r++];
         } 
       }
        return l+1;

    }

42. 接雨水

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
在这里插入图片描述

//双指针
    public int trap(int[] height) {
    
    
        int res = 0, leftMax = 0, rightMax = 0, left = 0, right = height.length - 1;
        while (left <= right) {
    
    
            if (leftMax <  rightMax) {
    
    
                leftMax = Math.max(leftMax, height[left]);
                res += leftMax - height[left++];
            } else {
    
    
                rightMax = Math.max(rightMax, height[right]);
                res += rightMax - height[right--];
            }
        } 
        return res;
      }

121. 买卖股票的最佳时机

在这里插入图片描述

class Solution {
    
    
    public int maxProfit(int[] prices) {
    
    

        if(prices==null||prices.length==0) return 0;

         int res=0; //最大利润
         int min=prices[0]; //最低价格
         int n=prices.length;
             
         for(int i=1;i<n;i++){
    
    
         if(prices[i]<min){
    
    
               min=prices[i]; 
         }else{
    
    
            res=Math.max(res,prices[i]-min);
         }    
      }
      return res;   
    }
}

209. 长度最小的子数组(滑动窗口)

在这里插入图片描述

class Solution {
    
    
    public int minSubArrayLen(int s, int[] nums) {
    
    
    if(nums.length==0) return 0;
    
       int l=0;
       int n=nums.length;
       int r=-1;
       int res= nums.length+1;
       int sum=0;

       while(l<n){
    
    
           if(r+1<n&&sum<s){
    
    
               sum+=nums[++r];
           }else{
    
    
               sum-=nums[l++];
           }
           if(sum>=s){
    
    
           res=Math.min(r-l+1,res);
           }
       }
           return res==nums.length+1?0:res;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38847154/article/details/108785211