![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
weixin_44398018
这个作者很懒,什么都没留下…
展开
-
81.search
81.搜索旋转排序数组Ⅱclass Solution { public boolean search(int[] nums, int target) { int n=nums.length; if(n==0){ return false; } if(n==1){ return nums[0]==target?true:false; } int left=0原创 2021-01-04 19:39:01 · 124 阅读 · 0 评论 -
12.intToRoman
12.整数转罗马数字class Solution { public String intToRoman(int num) { int[] values={1000,900,500,400,100,90,50,40,10,9,5,4,1}; String[] symbols={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; StringBuilder sb=new StringBu原创 2021-01-04 17:48:35 · 96 阅读 · 0 评论 -
78.subsets
78.子集class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res=new ArrayList<>(); backtrack(res,nums,new ArrayList<Integer>(),0); return res; } public原创 2021-01-04 17:18:51 · 75 阅读 · 0 评论 -
47.permuteUnique
47.全排列Ⅱclass Solution { boolean[] vis; public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> res=new ArrayList<>(); List<Integer> output=new ArrayList<>(); vis=原创 2021-01-04 16:41:15 · 227 阅读 · 1 评论 -
46.permute
46.全排列class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res=new ArrayList<>(); List<Integer> output=new ArrayList(); for(int num:nums){ output.原创 2021-01-04 16:09:18 · 114 阅读 · 0 评论 -
216.combinationSum3
216.组合总和Ⅲclass Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> res=new ArrayList<>(); Deque<Integer> path=new ArrayDeque<>(); if(n<1){原创 2021-01-04 14:45:56 · 79 阅读 · 1 评论 -
40.combinationSum2
40.组合总和Ⅱclass Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { Arrays.sort(candidates); List<List<Integer>> res=new ArrayList<List<Integer>>(); Deque<I原创 2021-01-04 14:30:54 · 115 阅读 · 0 评论 -
39.combinationSum
39.组合总和class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res=new ArrayList<List<Integer>>(); List<Integer> cur=new ArrayList<Inte原创 2021-01-04 14:05:46 · 66 阅读 · 0 评论 -
22.generateParenthesis
22.括号生成class Solution { public List<String> generateParenthesis(int n) { List<String> ans=new ArrayList<String>(); backtrack(ans,new StringBuilder(),0,0,n); return ans; } public void backtrack(List<原创 2021-01-04 13:18:40 · 170 阅读 · 0 评论 -
17.letterCombinations
17.电话号码的字母组合class Solution { public List<String> letterCombinations(String digits) { List<String> combiantions=new ArrayList<String>(); if(digits.length()==0){ return combiantions; } Map<原创 2021-01-04 12:23:58 · 487 阅读 · 0 评论 -
70.climbStairs
70.爬楼梯class Solution { public int climbStairs(int n) { if(n<=2){ return n; } int two=1; int one=2; int sum=0; for(int i=3;i<=n;i++){ sum=two+one; two=one;原创 2021-01-03 21:46:25 · 74 阅读 · 0 评论 -
69.mySqrt
69.x的平方根class Solution { public int mySqrt(int x) { int l=0; int r=x; int ans=0; while(l<=r){ int mid=l+(r-l)/2; if((long)mid*mid<=x){ ans=mid; l=mid+1;原创 2021-01-03 21:34:53 · 150 阅读 · 0 评论 -
67.addBinary
67.二进制求和class Solution { public String addBinary(String a, String b) { int i=a.length()-1; int j=b.length()-1; int carry=0; StringBuilder builder=new StringBuilder(); while(i>=0&&j>=0){原创 2021-01-03 20:54:07 · 51 阅读 · 0 评论 -
66.plusOne
66.加一class Solution { public int[] plusOne(int[] digits) { for(int i=digits.length-1;i>=0;i--){ digits[i]++; digits[i]%=10; if(digits[i]!=0){ return digits; } }原创 2021-01-03 20:26:44 · 45 阅读 · 0 评论 -
64.minPathSum
64.最小路径和class Solution { public int minPathSum(int[][] grid) { if(grid==null||grid.length==0||grid[0].length==0){ return 0; } int m=grid.length; int n=grid[0].length; int[][] dp=new int[m][n];原创 2021-01-03 20:13:44 · 136 阅读 · 0 评论 -
62.uniquePaths
62.不同路径class Solution { public int uniquePaths(int m, int n) { int[][] f=new int[m][n]; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(i==0||j==0){ f[i][j]=1; }else{原创 2021-01-02 17:28:17 · 46 阅读 · 0 评论 -
61.rotateRight
61.旋转链表/** * 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;原创 2021-01-02 17:17:15 · 68 阅读 · 0 评论 -
48.rotate
48.旋转图像class Solution { public void rotate(int[][] matrix) { if(matrix.length==0||matrix.length!=matrix[0].length){ return; } int n=matrix.length; for(int i=0;i<n;i++){ for(int j=0;j<n-i-1;原创 2021-01-02 16:55:02 · 54 阅读 · 0 评论 -
56.merge
56.合并区间class Solution { public int[][] merge(int[][] intervals) { Arrays.sort(intervals, (v1,v2)->v1[0]-v2[0]); int n=intervals.length; int[][] res=new int[n][2]; int id=-1; for(int i=0;i<n;i++){原创 2021-01-02 14:09:02 · 43 阅读 · 0 评论 -
55.canJump
55.跳跃游戏class Solution { public boolean canJump(int[] nums) { int most=0; int n=nums.length; for(int i=0;i<n;i++){ if(i<=most){ most=Math.max(most,i+nums[i]); } if(most&g原创 2021-01-02 12:51:10 · 141 阅读 · 0 评论 -
59.generateMatrix
59.螺旋数组Ⅱclass Solution { public int[][] generateMatrix(int n) { int left=0; int right=n-1; int top=0; int bottom=n-1; int[][] order=new int[n][n]; int num=1; int target=n*n; while(num<原创 2021-01-02 12:28:19 · 290 阅读 · 0 评论 -
54.spiralOrder
54.螺旋矩阵class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> order=new ArrayList<Integer>(); if(matrix==null||matrix.length==0||matrix[0].length==0){ return order; }原创 2020-12-29 17:26:57 · 52 阅读 · 0 评论 -
50.myPow
50.Pow(x,n)class Solution { double quickMul(double x, long N) { double ans = 1.0; // 贡献的初始值为 x double x_contribute = x; // 在对 N 进行二进制拆分的同时计算答案 while (N > 0) { if (N % 2 == 1) { //原创 2020-12-29 16:38:29 · 183 阅读 · 0 评论 -
58.lengthOfLastWord
58.最后一个单词的长度class Solution { public int lengthOfLastWord(String s) { int end=s.length()-1; while(end>=0&&s.charAt(end)==' '){ end--; } if(end<0){ return 0; } int sta原创 2020-12-29 11:35:40 · 43 阅读 · 0 评论 -
31.nextPermutation
31.下一个序列class Solution { public void nextPermutation(int[] nums) { int n=nums.length; for(int i=n-1;i>0;i--){ if(nums[i]>nums[i-1]){ Arrays.sort(nums,i,n); for(int j=i;j<n;j++){原创 2020-12-29 11:34:13 · 30 阅读 · 0 评论 -
33.search
33.搜索旋转排序数组class Solution { public int search(int[] nums, int target) { int n=nums.length; if(n==0){ return -1; } if(n==1){ return nums[0]==target?0:-1; } int left=0; int原创 2020-12-29 10:53:44 · 39 阅读 · 0 评论 -
53.maxSubArray
53.最大字序和class Solution { public int maxSubArray(int[] nums) { int pre = 0, maxAns = nums[0]; for (int x : nums) { pre = Math.max(pre + x, x); maxAns = Math.max(maxAns, pre); } return maxAns;原创 2020-12-29 11:36:04 · 119 阅读 · 0 评论 -
24.swapPairs
24两两交换链表中的节点/** * 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 =原创 2020-12-28 21:31:48 · 620 阅读 · 0 评论 -
34.searchRange
34.在排序数组中查找元素的第一个和最后一个元素class Solution { public int[] searchRange(int[] nums, int target) { int find=reverse(nums,target); if(find==-1){ return new int[]{-1,-1}; } int left=find-1; int rigth=find+1;原创 2020-12-28 20:22:49 · 155 阅读 · 0 评论 -
36.isValidSudoku
36.有效的数独class Solution { public boolean isValidSudoku(char[][] board) { int[][] rows=new int[9][9]; int[][] col=new int[9][9]; int[][] box=new int[9][9]; for(int i=0;i<9;i++){ for(int j=0;j<9;j++){原创 2020-12-28 19:36:04 · 78 阅读 · 0 评论 -
25.reverseKGroup
25.K个一组翻转链表/** * 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 = n原创 2020-12-28 18:33:46 · 144 阅读 · 0 评论 -
38.countAndSay
38.外观数列class Solution { public String countAndSay(int n) { if(n==1){ return "1"; } StringBuffer res=new StringBuffer(); String str=countAndSay(n-1); int length=str.length(); int start=0;原创 2020-12-28 16:58:19 · 50 阅读 · 0 评论 -
35.searchInsert
35.搜索插入位置class Solution { public int searchInsert(int[] nums, int target) { int length=nums.length; int j=0; for(;j<length;j++){ if(nums[j]==target){ return j; }else if(nums[j]>targ原创 2020-12-28 16:28:59 · 90 阅读 · 0 评论 -
28.strStr
28.实现strStr()class Solution { public int strStr(String haystack, String needle) { int m=haystack.length(); int n=needle.length(); if(n==0){ return 0; } for(int i=0;i<=m-n;i++){ for(int原创 2020-12-28 16:12:21 · 37 阅读 · 0 评论 -
27.removeElement
27.移除元素class Solution { public int removeElement(int[] nums, int val) { int length=nums.length; if(length==0){ return 0; } int i=0; for(int j=0;j<length;j++){ if(nums[j]!=val){原创 2020-12-28 15:30:58 · 57 阅读 · 0 评论 -
26.removeDuplicates
26.删除排序数组中的重复项class Solution { public int removeDuplicates(int[] nums) { int length=nums.length; if(length==0){ return 0; } int i=0; for(int j=1;j<length;j++){ if(nums[i]!=nums[j]){原创 2020-12-28 15:20:12 · 224 阅读 · 0 评论 -
18.fourSum
18.四数之和在这里插入代码片原创 2020-12-28 15:09:01 · 88 阅读 · 0 评论 -
19.removeNthFromEnd
19.删除链表的倒数第N个节点class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { return removeNode(head,n)==n?head.next:head; } public int removeNode(ListNode node,int n) { if(node.next == null) return 1; int m = removeNode(n原创 2020-12-27 21:01:41 · 332 阅读 · 0 评论 -
16.threeSumClosest
16.最接近的三数之和class Solution { public int threeSumClosest(int[] nums, int target) { int n=nums.length; Arrays.sort(nums); int best=10000000; for(int first=0;first<n-2;first++){ if(first>0&&nums[fi原创 2020-12-27 19:58:27 · 68 阅读 · 0 评论 -
15.threeSum
15.三数之和class Solution { public List<List<Integer>> threeSum(int[] nums) { int n=nums.length; Arrays.sort(nums); List<List<Integer>> ans=new ArrayList<List<Integer>>(); for(int first=0原创 2020-12-27 18:56:17 · 37 阅读 · 0 评论