LeetCode前100题目尝试不同解法,手写代码,加b站视频讲解

Posted 有时间指导毕业设计

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode前100题目尝试不同解法,手写代码,加b站视频讲解相关的知识,希望对你有一定的参考价值。

文章目录

LeetCode前100题目不同解法尝试

视频地址

b站视频地址

1.Two Sum 两数之和

package com.hou.monkey.leetcode;/*
 **
 *@author SmallMonkey
 *@Date 2022/12/31 10:23
 *
 *
 **/


import java.util.HashMap;
import java.util.Map;

public class TwoSum 
    public static void main(String[] args) 

        int[] nums =2,7,11,15;
        int target = 18;
        int[] res = twoSum(nums, target);
        print(res);

    
    static void print(int[] arr)
        for (int i = 0; i < arr.length; i++) 
            System.out.print(arr[i] + " ");
        
    
    public static int[] twoSum(int[] nums, int target) 
        int[] res = new int[2];
        for (int i = 0; i < nums.length; i++) 
            for (int j = i + 1; j < nums.length && nums[i] + nums[j] == target; j++) 
                        res[0] = i;
                        res[1] = j;

            
        
        return res;


    

    public static int[] twoSum1(int[] nums, int target) 
        int[] res = new int[2];
         Map<Integer, Integer> map = new HashMap<>(16);

        for (int i = 0; i < nums.length; i++) 
            if(map.containsKey(target - nums[i]))
                res[0] = map.get(target - nums[i]);
                res[1] = i;
                return res;
            
            map.put(nums[i],i);

        
        return res;


    





2. three Sum 三数之和

package com.hou.monkey.leetcode;/*
 **
 *@author SmallMonkey
 *@Date 2022/12/31 12:42
 *
 *
 **/

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
//nums = [-1,0,1,2,-1,-4]



public class ThreeSum 
    public static List<List<Integer>> threeSum(int[] nums) 
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);

        for (int i = 0; i < nums.length - 2; i++) 
            if(i > 0 && nums[i] == nums[i - 1]) continue;
            int l = i + 1,r = nums.length - 1;
            while (l < r)
                if( nums[i] + nums[l] + nums[r] == 0)
                    list.add(Arrays.asList(nums[i],nums[l],nums[r]));
                    l++;
                    r--;
                    while (l < r && nums[l] == nums[l + 1]) l++;
                    while (l < r && nums[r] == nums[r - 1]) r --;
                else if(nums[i] + nums[l] + nums[r] < 0)
                    //由于已经排好顺序,所以左边的数往左越来越大
                    l++;
                else 
                    r--;
                
            

        


        return list;
    
    public static void main(String[] args) 
        int[] nums = -1,0,1,2,-1,-4;
        List<List<Integer>> lists = threeSum(nums);
        for (List<Integer> list : lists) 
            System.out.print(list + " ");
        


    




3.Longest SubString without Repeating Characters 最长无重复子串

package com.hou.monkey.leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class LongestNoRepeatSubString 
    public static int lengthOfLongestSubstring(String s) 
        // 记录字符上一次出现的位置
        int[] last = new int[128];
//        for(int i = 0; i < 128; i++) 
//            last[i] = -1;
//        
        int n = s.length();

        int res = 0;
        int start = 0; // 窗口开始位置
        for(int i = 0; i < n; i++) 
            int index = s.charAt(i);
            // abcabcbb
            // 97  start = 0,last[97] + 1 = 1
            // res = Max.max(0, 0 - 1 + 1) = 0;
            //last[97] = 0

            // 98 start = 0,last[98] + 1 = 1
            //res = Math.max(0,1 - 1 + 1) = 1
            //last[98] = 1;

            // 99 start = 0,last[99] + 1 = 1
            //res = Math.max(0,2 - 1 + 1) = 2
            //last[99] = 1;


            // 97  start = 0,last[97] + 1 = 1
            // res = Max.max(0, 3 - 1 + 1) = 3;
            //last[97] = 3;


            start = Math.max(start, last[index] + 1);
            res   = Math.max(res, i - start + 1);
            last[index] = i;
        

        return res;

    



    public static int lengthOfLongestSubString1(String s)
        //用来存储最长子串
        Set<Character> sets = new HashSet<>();
        //使用双指针解决这个问题
        int rk = -1; //右指针
        int res = 0;
        for(int i = 0;i < s.length(); i ++)
            if( i != 0)
                sets.remove(s.charAt(i - 1));
            
            while (rk + 1 < s.length() && !sets.contains(s.charAt( rk + 1)))
                sets.add(s.charAt(rk + 1));
                System.out.println("sets:"+sets);
                rk ++;
            
            res = Math.max(res,rk + 1 - i);
        
        return res;
    

//    abcabcaa

    public static int longestLengthSubString4(String s)
        int maxLen = 1;
        for (int i = 0; i < s.length(); i++) 
            for (int j = i + 1; j <s.length() ; j++) 
                if(check(s,i,j))
                    maxLen = Math.max(maxLen,j - i + 1);

                

            
        
        return maxLen;


    

    private static boolean check(String s, int start, int end) 
        HashSet<Character> set = new HashSet<>();
        for (int k = start; k <= end; k++) 
            if(set.contains(s.charAt(k)))
                return false;
            
            set.add(s.charAt(k));
        
        return true;


    

    public static int lengthOfLongestSubstring5(String s) 
        HashMap<Character, Integer> map = new HashMap<>();
        int max = 0, start = 0;
        for (int end = 0; end < s.length(); end++) 
            char ch = s.charAt(end);
            if (map.containsKey(ch))
                //
                start = Math.max(map.get(ch)+1,start);
            
            max = Math.max(max,end - start + 1);
            map.put(ch,end);
        
        return max;
    
private static int longestLengthSubString1(String s) 
        int l = 0,r = 0, maxLen = 0;
        Set<Character> set = new HashSet<>();
        while (r < s.length())
           while (set.contains(s.charAt(r)))
               //左边移去,直到和右边没有一个重复的字符
               set.remove(s.charAt(l));
               l++;
           
           set.add(s.charAt(s.charAt(r)));
           maxLen = Math.max(maxLen,r - l + 1);
           r++;


        


        return maxLen;
    







    public static void main(String[] args) 
        String s = "pwwkew";
//        System.out.println(lengthOfLongestSubstring(s));

//        System.out.println(lengthOfLongestSubstring(s));
//        System.out.println(lengthOfLongestSubString1(s));
        System.out.println(longestLengthSubString4(s));
    


3.Median of Two Sorted Arrays 寻找两个有序数组中位数

package com.hou.monkey.leetcode;/*
 **
 *@author SmallMonkey
 *@Date 2023/1/4 10:04
 *
 *
 **/

import java.util.Arrays;

public class MedianOfTwoSortedArrays 
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) 
        int[] target = merge(nums1, nums2);
         Arrays.sort(target);
         double median =  0;
         if(target.length % 2 == 0)
             median = (target[target.length / 2] + target[target.length / 2 - 1]) / 2.0;
         else 
             median = target[(target.length - 1) / 2];

         
//         print(target);
//
        System.out.println("median:" + median);
        return median;
    

    private static int[] merge(int[] nums1, int[] nums2) 


        int[] num = new int[nums1.length + nums2.length];

     /*   System.arraycopy(nums1,0,num,0,nums1.length);
        System.arraycopy(nums2,0,num,nums1.length,nums2.length);
*/
        for (int i = 0; i < nums1.length; i++) 
            num[i] = nums1[i];
        
        for (int i = 0; i < nums2.length; i++) 
            num[i + nums1.length] = nums2[i];

        
        System.out.println(Arrays.toString(num));
        return  num;
    
    public static void print(int[] arr)
        for (int i = 0; i < arr.length; i++) 
            System.out.print(arr[i] + " ");

        
    

    public static void main(String[] args) 
        int[] nums1 =;
        int[

以上是关于LeetCode前100题目尝试不同解法,手写代码,加b站视频讲解的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 前 400 重点 250 题

Count and Say - LeetCode

[leetcode]100.Same Tree

leetcode热题100——单词拆分(dfs记忆化的考察)

Integer to Roman - LeetCode

LeetCode Hot 100 --- 寻找两个正序数组的中位数(java详解)