首页 > 其他分享 >Day_1复习

Day_1复习

时间:2024-03-19 23:30:55浏览次数:21  
标签:ListNode 复习 nums int sum ++ ans Day

## 盛最多水的容器

class Solution {
    public int maxArea(int[] height) {
        int n = height.length , ans = 0;
        int i =  0 , j = n - 1;
        while(i < j){
            ans = Math.max(ans ,(j - i) *  Math.min(height[i],height[j]));
            if(height[i] < height[j]){
                i++;
            }else{
                j--;
            }
        }
        return ans;
    }
}

无重复字符的最长字串

class Solution {
    public int lengthOfLongestSubstring(String s) {

        Map<Character ,  Integer> map = new HashMap<>();
        int ans = 0;
        for(int start = 0 , end = 0 ; end < s.length() ; end++){

            char right = s.charAt(end);
            map.put(right, map.getOrDefault(right , 0) + 1);
            while(map.get(right) > 1){

                char left = s.charAt(start);
                map.put(left , map.get(left) - 1);
                start++;
            }
            ans = Math.max(ans, end - start + 1);


        }
        return ans;




    }
}

三数之和

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);

        List<List<Integer>> ans = new ArrayList<>();

        for(int i = 0 ; i < n ; i ++){
            if(i > 0 &&nums[i] == nums[i - 1] ) continue;
            int j = i + 1 , k = n - 1;
            while(j < k){
                while(j > i + 1 && j < n && nums[j] == nums[j - 1])j++;
                if(j >= k) break;
                int sum = nums[i] + nums[j] + nums[k];
                if(sum == 0){
                    ans.add(Arrays.asList(nums[i],nums[j],nums[k]));
                    j++;
                }else if(sum > 0){
                    k--;
                }else if(sum < 0){
                    j++;
                }


            }

        }
        return ans;



    }
}

四数之和

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {

        Arrays.sort(nums);
        int n = nums.length;
        List<List<Integer>> ans = new ArrayList<>();
        for(int i = 0 ; i < n ; i ++){

            //去重第一个数
            if(i > 0 && nums[i] == nums[i - 1]) continue;

            for(int j = i + 1; j < n ;j++){
                //去重第二个数
                if(j > i + 1 && nums[j] == nums[j - 1]) continue;

                //赋值第三个,第四个数
                int k = j + 1 ,p = n - 1;
                while(k < p){

                    //去重第三个数
                    while(k > j + 1 && k < n && nums[k] == nums[k - 1]) k++;

                    //如果k跳过相同元素之和的位置超过了p,本次循环结束
                    if(k >= p) break;

                    long sum = 0L + nums[i] + nums[j] + nums[k] + nums[p];

                    if(sum == target){
                        ans.add(Arrays.asList(nums[i],nums[j],nums[k],nums[p]));
                        k++;
                    }else if(sum > target){
                        p--;
                    }else if(sum < target){
                        k++;
                    }
                }

              
            }
        }
        return ans;
        
    }
}

删除链表的倒数第N个结点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode cur = new ListNode(0);
        cur.next = head;


        ListNode slow = cur;
        ListNode fast = cur;

        for(int i = 0 ; i <= n ;i++){
            fast = fast.next;
        }


        while(fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return cur.next;



    }
}

## 新学

最接近的三数之和

class Solution {
    public int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);
        int ans = nums[0] + nums[1] + nums[2];
        int n = nums.length;
        for(int i = 0 ; i < n ; i ++){
            if(i > 0 && nums[i] == nums[ i - 1]) continue;

            int j = i + 1 , k = n - 1;
            while(j < k){
                int sum = nums[i] + nums[j] + nums[k];
                if(Math.abs(sum - target) < Math.abs(ans - target)) ans = sum;
                if(ans == target){
                    return target;
                }else if(sum > target){
                    k--;
                }else if(sum < target){
                    j++;
                }

            }
        } 
        return ans;
        




    }
}

标签:ListNode,复习,nums,int,sum,++,ans,Day
From: https://blog.csdn.net/m0_57975540/article/details/136790806

相关文章

  • Day_14
    ##重复的DNA序列classSolution{publicList<String>findRepeatedDnaSequences(Strings){List<String>ans=newArrayList<>();intn=s.length();Map<String,Integer>map=newHashMap<>();......
  • JavaSE(上)-Day6
    JavaSE(上)-Day6数组数组的定义数组的初始化打印数组分析数组索引数组内存图方法方法的定义和调用方法的重载方法的内存图二维数组二位数组的创建和初始化二维数组的内存图数组1.数组是一种容器,可以一次存储多个相同类型的数据数组的定义 int[]array1;......
  • 代码随想录算法训练营day28 | leetcode 93. 复原 IP 地址、78. 子集、90. 子集 II
    目录题目链接:93.复原IP地址-中等题目链接:78.子集-中等题目链接:90.子集II-中等题目链接:93.复原IP地址-中等题目描述:有效IP地址正好由四个整数(每个整数位于0到255之间组成,且不能含有前导0),整数之间用'.'分隔。例如:"0.1.2.201"和"192.168.1.1"是有效IP......
  • python自动化——自动化框架常用封装代码复习
    日志模块:importloggingimportosdefcreat_log(name="mylog",level="DEBUG",filename="log.log",sh_level="DEBUG",fh_level="DEBUG"):log=logging.getLogger(name)log.setLevel(level)fh=lo......
  • 2024/03/18(网络编程·day4)
    一、思维导图二、广播广播发送端#include<myhead.h>intmain(intargc,constchar*argv[]){ //1、创建套接字 intsfd=socket(AF_INET,SOCK_DGRAM,0); if(sfd==-1) { perror("socketerror"); return-1; } //2、设置允许广播 intbroadcast=1;......
  • 2024 蓝桥打卡Day15
    洛谷刷题P8752[蓝桥杯2021省B2]特殊年份题目[P8752[蓝桥杯2021省B2]特殊年份](https://www.luogu.com.cn/problem/P8752)题解P8780[蓝桥杯2022省B]刷题统计题目[P8780[蓝桥杯2022省B]刷题统计](https://www.luogu.com.cn/problem/P8780)题解P......
  • 代码随想录算法训练营day27 | leetcode 39. 组合总和、40. 组合总和 II、131. 分割回
    目录题目链接:39.组合总和-中等题目链接:40.组合总和II-中等题目链接:131.分割回文串-中等题目链接:39.组合总和-中等题目描述:给你一个无重复元素的整数数组candidates和一个目标整数target,找出candidates中可以使数字和为目标数target的所有不同组合,并以列表形......
  • 蓝桥杯day4刷题日记
    P8605[蓝桥杯2013国AC]网络寻路思路来源于https://www.luogu.com.cn/article/iat8irsf#include<iostream>usingnamespacestd;intn,m;intq[10010];intv[100010],u[100010];longlongres;intmain(){ cin>>n>>m; for(inti=0;i<m;i++) { cin......
  • 毕设Day11
     JWT实现用户登录鉴权     ......
  • LeetCode刷题记录——day1
    https://leetcode.cn/problems/h-index/description/?envType=study-plan-v2&envId=top-interview-150注:题目有点难理解,多读几遍可以这样考虑,建立另一个临时数组temp,当第i篇文章被引用citiations[i]次时,令j<=citiations[i]的temp[j]均加一,也就是现在对于任意j至少有temp[j]篇论......