leetcode刷题分类笔记

Posted Panda_Java

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode刷题分类笔记相关的知识,希望对你有一定的参考价值。

leetcode刷题分类笔记20220413

0. 常规操作

1. 部分数组复制  Arrays.copyOfRange(nums,start,end)  // 包含开始元素 不包含结尾元素
2. 字符串数组复制:String[] strs = "flower","flow","flight";
3. 将整型转化为字符串: String str = String.valueOf(n);
4. 将char转化为int: int c = str.charAt(i)-'0';
5. 字符串转化为字符数组: char[] nums = str.toCharArray();
6. 截取字符串: String a = s.substring(index,index+i); 不含末尾元素
7. 将字符串转为整形: Integer.valueOf(s) > 255 
8. 获取字符串某一个字符 char ch = str.charAt(j)
9. 比较字符串大小(求k个高频单词) words[i].compareTo(words[j])<0
10. Java转换大小写之toLowerCase()和toUpperCase()
11. char转化int:s=String.valueOf(c);i=Integer.parseInt(s)

1. 链表

83. 删除排序链表中的重复元素

class Solution 
    public ListNode deleteDuplicates(ListNode head) 
        if(head == null)
            return head;
        ListNode dump = new ListNode(-1);
        ListNode q = head; // 慢指针
        dump.next = q;
        ListNode p = head.next; // 快指针
        int a = head.val;
        while(p!=null)
            if(a != p.val)
                q.next = p;
                a = p.val;   // 存值方便下一次再比较
                q = p;       // 慢指针也要往下一个位置
                p = p.next; 
            else
                 p = p.next;
            
        
        q.next = null;
        return dump.next;
    

203. 移除链表元素

/**
 * 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 removeElements(ListNode head, int val) 
        if(head == null)
            return head;
        
        ListNode dump = new ListNode(-1);
        dump.next = null;
        ListNode q = dump;  // q慢指针
        ListNode p = head;  // p快指针

        while(p != null)
            if(p.val != val)
                q.next = p;
                q = p;
                p = p.next;
            else
                p = p.next;
            
        
        q.next = null;
        return dump.next;
    

2. 二叉树

leetcode108. 将有序数组转换为二叉搜索树

class Solution 
    public TreeNode sortedArrayToBST(int[] nums) 
        if(nums.length == 0)
            return null;    
        int i = 0;
        int j = nums.length;
        int k = (i+j)/2;
        int a = nums[k];
        TreeNode root = new TreeNode(a);
        
        root.left = sortedArrayToBST(Arrays.copyOfRange(nums,i,k));
        root .right = sortedArrayToBST(Arrays.copyOfRange(nums,k+1,j));
        
        return root;
    

111. 二叉树的最小深度

class Solution 
    public int minDepth(TreeNode root) 
        if(root == null)
            return 0;
        if(root.left== null && root.right == null)
            return 1;
        int res = Integer.MAX_VALUE;
        if(root.left != null)
            res = Math.min(minDepth(root.left),res);
        if(root.right != null)
            res = Math.min(minDepth(root.right),res);
        return res+1;
    

113. 路径总和 II

class Solution 
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) 
        List<List<Integer>> res = new LinkedList<>();
        List<Integer> list =  new LinkedList<>();
        if(root == null)
            return res;
        dfs(res,root,targetSum,list);
        return res;   
    
    public void dfs(List<List<Integer>> res,TreeNode root, int targetSum, List<Integer> list)
        if(root == null)
             return;
        list.add(root.val);
        if(root.left==null && root.right== null&& targetSum == root.val)
           List<Integer> list1 =  new LinkedList<>(list);
            res.add(list1);      
            return;   
        
        if(root.left != null)
            dfs(res,root.left,targetSum-root.val,list);
            list.remove(list.size()-1);
        
        if(root.right != null)
            dfs(res,root.right,targetSum-root.val,list);
            list.remove(list.size()-1);
        
    

543. 二叉树的直径

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    int res;
    public int diameterOfBinaryTree(TreeNode root) 
        if(root == null)
            return 0;     
        dfs(root);
        return res;
    
    public int dfs(TreeNode root)
        if(root == null)
            return 0;
        int l = dfs(root.left);
        int r = dfs(root.right);

        if(l + r > res)
            res = l + r;
        
        return Math.max(l,r) + 1;
    

3. 动态规划


45. 跳跃游戏 II

class Solution 
    public int jump(int[] nums) 
       // int[] dp = new int[nums.length]; 
       // 此题并不是用动态规划而是贪心算法思想
       int res = 0;
       int maxPosition = 0;
       int end = 0;
       for(int i = 0; i < nums.length; i++)
           maxPosition = Math.max(maxPosition,i+nums[i]);
           if(end >= nums.length-1)  //越界不用再跳了
                 break;
                   
           if(i == end)
               res++;
               end = maxPosition;   
           
        
        return res;
    

96. 不同的二叉搜索树

class Solution 
    public int numTrees(int n) 
       int[] res = new int[n+1];

       res[0] = 1;
       res[1] = 1;

       for(int i = 2; i <= n; i++)
           int sum = 0;
           for(int j = 1; j <= i; j++)
               sum += res[j-1]*res[i-j];  
            
           res[i] = sum;
        
    return res[n];
    

3 最长回文子串(动态规划或者暴力)

class Solution 
    public String longestPalindrome(String s) 
        if(s.length() == 1)
            return s;
        char[] str = s.toCharArray();
        int l = s.length();
        boolean[][] dp = new boolean[l][l];

        for(int i = 0; i < s.length(); i++)
            dp[i][i] = true;
        
        int max = 1;
        int start = 0;
        
        for(int i = 0; i < l-1; i++)
           for(int j = 1; j < l; j++)
                if(str[i] != str[j])
                    dp[i][j] = false;
                else
                    if(j - i < 3)
                        dp[i][j] = true;
                    else
                        dp[i][j] = dp[i+1][j-1];
                
                if(dp[i][j] == true && j-i+1> max)
                    start = i;
                    max =j-i+1;   
                
            
        
    return s.substring(start,start + max);
    

4. 深度遍历与回溯

93. 复原 IP 地址

// Dfs 
class Solution 
    public List<String> restoreIpAddresses(String s) 
        List<String> res = new LinkedList<>();
        dfs(0,0,res,"",s);
        return res;
    
    public void dfs(int index, int count, List<String> res, String str, String s)
       // if(count > 4) return;
        if(count == 4 && index == s.length())
            res.add(str);
        
        for(int i = 1; i < 4; i++)
            if(index + i > s.length())
                 return;
            String a = s.substring(index,index+i);
            if(a.startsWith("0") && a.length() > 1 || Integer.valueOf(a) > 255)
                return;
            dfs(index+i, count+1, res, str+a+(count==3?"":"."), s);
        
    

131. 分割回文串

class Solution 
    public List<List<String>> partition(String s) 
        List<List<String>> res = new LinkedList<>();
        List<String> list =  new LinkedList<>();
        backtracking(res,list,s,0);
        return res;
    
    public void backtracking( List<List<String>> res, List<String> list,String s,int index)
        if(index == s.length())
            List<String> list1 =  new LinkedList<>(list);
            res.add(list1);
        

        for(int i = index; i <s.length();i++)
            String ss = s.substring(index,i+1);
            if(!check(ss))
                continue;
            
            list.add(ss);
            backtracking(res,list,s,i+1);
            list.remove(list.size()-1);
        
    
    public boolean check(String str)
        if (str == null || str.length() <= 1) 
            return true;
         
        int i = 0;
        int j = str.length()-1;
        while(i <= j)
            if(str.charAt(i) == str.charAt(j))
                i++;
                j--;
            else
                return false;
            
        
        return true;
    

方法二:

47. 全排列 II (去掉重复元素)

class Solution 
    public List<List<Integer>> permuteUnique(int[] nums) 
         List<List<Intege

以上是关于leetcode刷题分类笔记的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode刷题系列之-多数之和类型

LeetCode刷题系列之-多数之和类型

LeetCode刷题系列之-多数之和类型

LeetCode Java刷题笔记—113. 路径总和 II

LeetCode Java刷题笔记—112. 路径总和

LeetCode Java刷题笔记—1. 两数之和