![](https://img-blog.csdnimg.cn/14326253df584d2bb350979bd2ba8660.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
Leetcode练习
文章平均质量分 62
记录力扣刷题的经历。
Hokachi
这个作者很懒,什么都没留下…
展开
-
leetcode75.颜色分类——学习笔记
题目:力扣https://leetcode-cn.com/problems/sort-colors/class Solution { public void sortColors(int[] nums) { quickSort(nums,0,nums.length-1); } private void swap(int[] nums,int index1,int index2){ nums[index1] ^= nums[index2];原创 2022-02-27 17:28:37 · 228 阅读 · 0 评论 -
leetcode66.加一——学习笔记
题目:力扣https://leetcode-cn.com/problems/plus-one/class Solution { public int[] plusOne(int[] digits) { int n = digits.length; for (int i=n-1;i>=0;i--) { if (digits[i] != 9) { digits[i]++;原创 2022-02-26 13:35:01 · 259 阅读 · 0 评论 -
leetcode59.螺旋矩阵II——学习笔记
题目:力扣https://leetcode-cn.com/problems/spiral-matrix-ii/class Solution { public int[][] generateMatrix(int n) { int target = n*n; int num = 1; int[][] map = new int[n][n]; int left = 0; int right = n-1;原创 2022-02-24 12:19:58 · 226 阅读 · 0 评论 -
leetcode58.最后一个单词的长度——学习笔记
题目:力扣https://leetcode-cn.com/problems/length-of-last-word/class Solution { public int lengthOfLastWord(String s) { char[] sentence = s.toCharArray(); int count = 0; int i = sentence.length-1; while(sentence[i]==' '){原创 2022-02-23 13:09:00 · 248 阅读 · 0 评论 -
leetcode57.插入区间——学习笔记
题目:力扣https://leetcode-cn.com/problems/insert-interval/class Solution { public int[][] insert(int[][] intervals, int[] newInterval) { int[][] finalInterval = new int[intervals.length+1][2]; int count = 0; while(count<=inter原创 2022-02-22 16:57:18 · 119 阅读 · 0 评论 -
leetcode56.合并区间——学习笔记
题目:力扣https://leetcode-cn.com/problems/merge-intervals/方法一:利用冒泡排序class Solution { public int[][] merge(int[][] intervals) { //排序 mysort(intervals); //执行 List<int[]> path = new ArrayList<int[]>();原创 2022-02-22 13:24:35 · 332 阅读 · 0 评论 -
leetcode55.跳跃游戏——学习笔记
题目:力扣https://leetcode-cn.com/problems/jump-game/class Solution { public boolean canJump(int[] nums) { int except = 0; for(int i=0;i<nums.length;i++){ if(i<=except){ except = Math.max(except,nums[i]+i原创 2022-02-15 21:58:46 · 2716 阅读 · 0 评论 -
leetcode54.螺旋矩阵——学习笔记
题目:力扣https://leetcode-cn.com/problems/spiral-matrix/class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> ans = new ArrayList<Integer>(); int top = 0; int bottom = matrix.length-1原创 2022-02-14 22:40:38 · 154 阅读 · 0 评论 -
leetcode53.最大子数组和——学习笔记
题目:力扣https://leetcode-cn.com/problems/maximum-subarray/class Solution { public int maxSubArray(int[] nums) { int max = Integer.MIN_VALUE; int sum = 0; int len = nums.length; for(int i=0;i<len;i++){ sum原创 2022-02-13 10:19:45 · 572 阅读 · 0 评论 -
leetcode52.N皇后II——学习笔记
题目:力扣https://leetcode-cn.com/problems/n-queens-ii/class Solution { public int totalNQueens(int n) { Set<Integer> diagonal1 = new HashSet<Integer>(); Set<Integer> diagonal2 = new HashSet<Integer>(); Set原创 2022-02-10 10:43:39 · 345 阅读 · 0 评论 -
Java常用归纳2.0
序号仅方便记录不代表任何意义,该文章仅用于方便自己使用,侵删。(0)自己的补充——零零碎碎的记录(00)Java中常见常用的类——类的总结(1)java中List的用法和实例详解——List(2)Java中HashSet用法——HashSet(3)【java】栈(Stack)的基本使用——Stack(4)Java Math类中的常用方法及使用规范——Math(5)JAVA各种数据类型的互相转换——类型转换(6)java获取长度的几种方法——获取长度(7)【J...原创 2022-01-30 12:24:56 · 602 阅读 · 0 评论 -
leetcode51.N皇后——学习笔记
题目:力扣https://leetcode-cn.com/problems/n-queens/class Solution { public List<List<String>> solveNQueens(int n) { Set<Integer> diagonal1 = new HashSet<Integer>(); Set<Integer> diagonal2 = new HashSet<原创 2022-01-30 11:58:18 · 697 阅读 · 0 评论 -
leetcode50.Pow(x,n)——学习笔记
题目:力扣https://leetcode-cn.com/problems/powx-n/不讲武德版:class Solution { public double myPow(double x, int n) { return Math.pow(x,n); }}思路:直接使用java的内置的数学方法解题,这么写完感觉对不住leetcode给的“中等难度”。然后又自己写了一版“面向测试例子的编程”。面向测试用例编程版:class Solution.原创 2022-01-28 16:29:07 · 674 阅读 · 0 评论 -
leetcode49.字母异位次分组——学习笔记
题目:https://leetcode-cn.com/problems/group-anagrams/https://leetcode-cn.com/problems/group-anagrams/class Solution { public List<List<String>> groupAnagrams(String[] strs) { Map<String,List<String>> map = new HashMap&l原创 2022-01-27 10:03:58 · 331 阅读 · 0 评论 -
leetcode48.旋转图像——学习笔记
题目:力扣https://leetcode-cn.com/problems/rotate-image/class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i=0;i<n/2;i++){ for(int j=0;j<(n+1)/2;j++){ int temp = matrix[i原创 2022-01-25 21:39:48 · 246 阅读 · 0 评论 -
leetcode47.全排列II——学习笔记
题目:力扣https://leetcode-cn.com/problems/permutations-ii/class Solution { public List<List<Integer>> permuteUnique(int[] nums) { int len = nums.length; int depth = 0; Deque<Integer> path = new ArrayDeque<>原创 2022-01-23 11:35:15 · 571 阅读 · 1 评论 -
leetcode46.全排列——学习笔记
题目:力扣https://leetcode-cn.com/problems/permutations/class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> ans = new ArrayList<List<Integer>>(); int len = nums.length;原创 2022-01-22 12:19:23 · 391 阅读 · 0 评论 -
leetcode45.跳跃游戏II——学习笔记
题目:力扣https://leetcode-cn.com/problems/jump-game-ii/class Solution { public int jump(int[] nums) { if(nums.length==1){ return 0; } return greedAlgorithm(nums); } private int greedAlgorithm(int[] nums){原创 2022-01-21 15:39:36 · 2992 阅读 · 0 评论 -
leetcode44.通配符匹配——学习笔记
题目:力扣https://leetcode-cn.com/problems/wildcard-matching/class Solution { public boolean isMatch(String s, String p) { boolean[][] dp = new boolean[s.length()+1][p.length()+1]; dp[0][0] =true; for(int j=1;j<p.length()+1;j++原创 2022-01-20 12:24:01 · 450 阅读 · 0 评论 -
leetcode42.接雨水——学习笔记
题目:力扣class Solution { public int trap(int[] height) { int len = height.length; int[] leftMax = new int[len]; int[] rightMax = new int[len]; int top = 0; int sum = 0; for(int i=0;i<len;i++){原创 2022-01-18 11:33:25 · 314 阅读 · 0 评论 -
Java常用归纳1.0
数组排序:1.Arrays.sort(arr);类型转换:Int转char:(1)int a = 9;String aStr = String.valueOf(a);char转Int:char转String:(1)String str1 = new String(chars);(2)String str2 = String.valueOf(chars);String转char:(1)char[] chars1 = str.toCharArra...原创 2022-01-16 10:18:33 · 102 阅读 · 0 评论 -
leetcode41.缺失的第一个正数——学习笔记
题目:力扣class Solution { public int firstMissingPositive(int[] nums) { Arrays.sort(nums); int num = 1; for(int i=0;i<nums.length;i++){ if(nums[i]<=0){ continue; } if(i>原创 2022-01-16 10:17:25 · 202 阅读 · 0 评论 -
leetcode40.组合总和II——学习笔记
题目:力扣https://leetcode-cn.com/problems/combination-sum-ii/class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer>> ans = new ArrayList<List<Integer>>()原创 2022-01-15 15:27:34 · 193 阅读 · 0 评论 -
leetcode39.组合总和——学习笔记
题目:力扣https://leetcode-cn.com/problems/combination-sum/class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> ans = new ArrayList<List<Integer>>();原创 2022-01-13 11:45:20 · 285 阅读 · 0 评论 -
leetcode38.外观数列——学习笔记
题目:力扣class Solution { public String countAndSay(int n) { if(n >=2){ String s = countAndSay(--n); return nextString(s); }else{ return "1"; } } public String nextString(String .原创 2022-01-12 12:37:24 · 275 阅读 · 0 评论 -
leetcode37.解数独——学习笔记
题目:力扣https://leetcode-cn.com/problems/sudoku-solver/class Solution { public void solveSudoku(char[][] board) { getNext(board); } private static List<String> getPossibleStatus(char[][] board,int x,int y){ List<String原创 2022-01-11 13:12:46 · 287 阅读 · 0 评论 -
leetcode36.有效的数独——学习笔记
题目:力扣https://leetcode-cn.com/problems/valid-sudoku/class Solution { public boolean isValidSudoku(char[][] board) { for(int num=0;num<9;num++){ boolean a = checkLine(board,num); boolean b = checkColumn(board,num);原创 2022-01-09 12:53:56 · 470 阅读 · 0 评论 -
leetcode35:搜索插入位置——学习笔记
题目:力扣https://leetcode-cn.com/problems/search-insert-position/class Solution { public int searchInsert(int[] nums, int target) { if(nums.length==1){ if(target<=nums[0]){ return 0; }else{原创 2022-01-06 23:23:36 · 537 阅读 · 0 评论 -
leetcode34.在排序数组中查找元素的第一个和最后一个位置——学习笔记
题目:力扣https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/class Solution { public int[] searchRange(int[] nums, int target) { if(nums.length==1){ if(nums[0]==target){ return new原创 2022-01-05 20:17:18 · 319 阅读 · 0 评论 -
leetcode33.搜索螺旋排序数组——学习笔记
题目:力扣https://leetcode-cn.com/problems/search-in-rotated-sorted-array/class Solution { public int search(int[] nums, int target) { int left = 0; int right = nums.length-1; while(left<=right){ int mid = (left+rig原创 2022-01-04 19:41:24 · 339 阅读 · 0 评论 -
leetcode32.最长有效括号——学习笔记
题目:力扣https://leetcode-cn.com/problems/longest-valid-parentheses/import java.util.Stack;class Solution { public int longestValidParentheses(String s) { int len = s.length(); int num = 0; int max_num = 0; char[] Str =原创 2022-01-03 22:21:29 · 85 阅读 · 0 评论 -
leetcode31.下一个排列——学习笔记
题目:力扣https://leetcode-cn.com/problems/next-permutation/class Solution { public void nextPermutation(int[] nums) { int length = nums.length; for (int a=length-1;a>0;a--) { if (nums[a]>nums[a-1]) {原创 2022-01-02 22:15:53 · 308 阅读 · 0 评论 -
leetcode30.串联所有单词的子串——学习笔记
题目:力扣https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/class Solution { public List<Integer> findSubstring(String s, String[] words) { List<String> baseList = Arrays.asList(words);//将字符出串数组转化为集合 L原创 2021-12-06 17:54:10 · 181 阅读 · 0 评论 -
leetcode29.两数相除——学习笔记
题目:力扣https://leetcode-cn.com/problems/divide-two-integers/class Solution { public int divide(int dividend, int divisor) { if (dividend == 0) { return 0; } if (dividend == Integer.MIN_VALUE && divisor == -1原创 2021-12-03 12:18:25 · 218 阅读 · 0 评论 -
leetcode28.实现strStr()——学习笔记
题目:力扣https://leetcode-cn.com/problems/implement-strstr/class Solution { public int strStr(String haystack, String needle){ if(needle.length()==0) return 0; if(needle.length()>haystack.length()) return -1;原创 2021-12-02 12:55:14 · 140 阅读 · 0 评论 -
leetcode27.移除元素——学习笔记
题目;力扣https://leetcode-cn.com/problems/remove-element/class Solution { public int removeElement(int[] nums, int val) { if(nums.length<1){ return nums.length; } int i = 0; int j = 0; while(j<nu原创 2021-11-19 20:40:26 · 338 阅读 · 0 评论 -
leetcode26.删除有序数组中的重复项——学习笔记
题目:力扣https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/class Solution { public int removeDuplicates(int[] nums) { if(nums.length<2){ return nums.length; } int i = 0; int j = 1;原创 2021-11-19 17:44:57 · 578 阅读 · 0 评论 -
leetcode25.K个一组翻转链表——学习笔记
题目:力扣https://leetcode-cn.com/problems/reverse-nodes-in-k-group//** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(i原创 2021-11-11 00:03:29 · 255 阅读 · 0 评论 -
leetcode24.两两交换链表中的节点——学习笔记
题目:力扣https://leetcode-cn.com/problems/swap-nodes-in-pairs//** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int va原创 2021-11-08 23:18:20 · 111 阅读 · 0 评论 -
leetcode23.合并K个升序链表——学习笔记
题目:力扣https://leetcode-cn.com/problems/merge-k-sorted-lists//** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int v原创 2021-11-05 16:04:01 · 138 阅读 · 0 评论