13 LeetCode精选100题
Lamiaomiao
好好学习,天天向上
展开
-
LeetCode精选100题——第92题——反转链表 II
class Solution { public ListNode reverseBetween(ListNode head, int m, int n) { ListNode cur=head; ListNode p=null; while(m>1){ p=cur; cur=cur.ne...原创 2019-10-12 16:25:19 · 196 阅读 · 0 评论 -
LeetCode精选100题——第90题——子集 II
class Solution { public List<List<Integer>> subsetsWithDup(int[] nums) { ArrayList<List<Integer>> res=new ArrayList<>(); Arrays.sort(nums); h...原创 2019-10-12 14:51:24 · 202 阅读 · 0 评论 -
LeetCode精选100题——第88题——合并两个有序数组
class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] nums1Copy=new int[m]; System.arraycopy(nums1,0,nums1Copy,0,m); int p=0; int p1=0...原创 2019-10-12 14:23:26 · 156 阅读 · 0 评论 -
LeetCode精选100题——第86题——分隔链表
class Solution { public ListNode partition(ListNode head, int x) { ListNode dummyHead1=new ListNode(-1); ListNode dummyHead2=new ListNode(-1); ListNode p1=dummyHead1; ...原创 2019-10-12 13:09:58 · 271 阅读 · 0 评论 -
LeetCode精选100题——第83题——删除排序链表中的重复元素
class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null||head.next==null){ return head; } ListNode pre=head; ListNode cur=pre....原创 2019-10-12 10:53:06 · 187 阅读 · 0 评论 -
LeetCode精选100题——第82题——删除排序链表中的重复元素 II
class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode dummyHead=new ListNode(-1); dummyHead.next=head; ListNode p=dummyHead; while(p.next!=nu...原创 2019-10-12 10:52:11 · 174 阅读 · 0 评论 -
LeetCode精选100题——第81题——搜索旋转排序数组 II
class Solution { public boolean search(int[] nums, int target) { if(nums==null||nums.length==0){ return false; } int left=0; int right=nums.length-1; ...原创 2019-10-12 10:14:42 · 145 阅读 · 0 评论 -
LeetCode精选100题——第59题——螺旋矩阵 II
class Solution { public int[][] generateMatrix(int n) { int rowStart=0; int rowEnd=n-1; int colStart=0; int colEnd=n-1; int[][] matrix=new int[n][n]; ...原创 2019-10-10 15:35:50 · 137 阅读 · 0 评论 -
LeetCode精选100题——第60题——第k个排列
class Solution { public String getPermutation(int n, int k) { ArrayList<String> res = new ArrayList<>(); helper(n, res, new StringBuffer()); return res.get(k -...原创 2019-10-10 16:39:38 · 125 阅读 · 0 评论 -
LeetCode精选100题——第61题——旋转链表
class Solution { public ListNode rotateRight(ListNode head, int k) { if(head==null||head.next==null){ return head; } ListNode tail=head; int length=1;...原创 2019-10-10 18:37:49 · 286 阅读 · 0 评论 -
LeetCode精选100题——第69题——x 的平方根
class Solution { public int mySqrt(int x) { int low=0; int high=x; while(low<=high){ long mid=low+(high-low)/2; if(mid*mid==x){ ...原创 2019-10-11 10:26:56 · 120 阅读 · 0 评论 -
LeetCode精选100题——第58题——最后一个单词的长度
class Solution { public int lengthOfLastWord(String s) { int p=s.length()-1; while(p>=0&&s.charAt(p)==' '){ p--; } int end=p; while(...原创 2019-10-10 14:32:08 · 165 阅读 · 0 评论 -
LeetCode精选100题——第56题——合并区间
class Solution { public int[][] merge(int[][] intervals) { if(intervals==null||intervals.length==0){ return intervals; } //按每行的第一位从小到大排列 Arrays.sort(in...原创 2019-10-10 13:23:45 · 79 阅读 · 0 评论 -
LeetCode精选100题——第55题——跳跃游戏
class Solution { public boolean canJump(int[] nums) { int maxIndex=0; for(int i=0;i<nums.length;i++){ if(maxIndex<i){ return false; }...原创 2019-10-10 12:30:59 · 148 阅读 · 0 评论 -
LeetCode精选100题——第54题——螺旋矩阵
class Solution { public List<Integer> spiralOrder(int[][] matrix) { ArrayList<Integer> list=new ArrayList<>(); if(matrix==null||matrix.length==0){ re...原创 2019-10-10 10:49:35 · 100 阅读 · 0 评论 -
LeetCode精选100题——第53题——最大子序和
class Solution { public int maxSubArray(int[] nums) { int s=nums[0]; int max=s; for(int i=1;i<nums.length;i++){ s=nums[i]+(s>0?s:0); if(s>...原创 2019-10-10 10:08:15 · 257 阅读 · 0 评论 -
LeetCode精选100题——第50题——Pow(x, n)
class Solution { public double myPow(double x, int n) { if (x > -0.0001 && x < 0.0001) { return 0; } if (n == 0) { return 1.0; ...原创 2019-10-10 09:40:58 · 121 阅读 · 0 评论 -
LeetCode精选100题——第49题——字母异位词分组
class Solution { public List<List<String>> groupAnagrams(String[] strs) { ArrayList<List<String>> res=new ArrayList<>(); if(strs==null||strs.length==...原创 2019-10-08 18:53:27 · 170 阅读 · 0 评论 -
LeetCode精选100题——第62题——不同路径
class Solution { public int uniquePaths(int m, int n) { int[][] res=new int[m][n]; for(int i=0;i<m;i++){ res[i][0]=1; } for(int i=0;i<n;i++){ ...原创 2019-10-10 19:40:38 · 125 阅读 · 0 评论 -
LeetCode精选100题——第63题——不同路径 II
class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int m=obstacleGrid.length; int n=obstacleGrid[0].length; int[][] res=new int[m][n]; fo...原创 2019-10-10 20:00:45 · 129 阅读 · 0 评论 -
LeetCode精选100题——第78题——子集
class Solution { public List<List<Integer>> subsets(int[] nums) { ArrayList<List<Integer>> res=new ArrayList<>(); helper(res,new ArrayList&l...原创 2019-10-11 20:15:12 · 104 阅读 · 0 评论 -
LeetCode精选100题——第77题——组合
class Solution { public List<List<Integer>> combine(int n, int k) { ArrayList<List<Integer>> res=new ArrayList<>(); helper(res,new ArrayList<Integ...原创 2019-10-11 19:54:59 · 104 阅读 · 0 评论 -
LeetCode精选100题——第75题——颜色分类
class Solution { public void sortColors(int[] nums) { int start=0; int end=nums.length-1; int cur=0; while(cur<=end){ if(nums[cur]==2){ ...原创 2019-10-11 19:28:21 · 146 阅读 · 0 评论 -
LeetCode精选100题——第74题——搜索二维矩阵
public class T074 { public boolean searchMatrix(int[][] matrix, int target) { if(matrix==null||matrix.length==0||matrix[0].length==0){ return false; } int m=ma...原创 2019-10-11 16:47:35 · 187 阅读 · 0 评论 -
LeetCode精选100题——第73题——矩阵置零
class Solution { public void setZeroes(int[][] matrix) { int[][] flag =new int[matrix.length][matrix[0].length]; for(int i=0;i<matrix.length;i++){ for(int j=0;j<...原创 2019-10-11 14:10:54 · 157 阅读 · 0 评论 -
LeetCode精选100题——第71题——简化路径
class Solution { public String simplifyPath(String path) { String[] strs=path.split("/"); Stack<String> stack=new Stack<>(); for(int i=0;i<strs.length;i++){...原创 2019-10-11 13:03:38 · 143 阅读 · 0 评论 -
LeetCode精选100题——第70题——x 的平方根
class Solution { public int climbStairs(int n) { if(n==1){ return 1; } if(n==2){ return 2; } return climbStairs(n-1)+climbStairs(n-...原创 2019-10-11 10:32:24 · 128 阅读 · 0 评论 -
LeetCode精选100题——第67题——二进制求和
class Solution { public String addBinary(String a, String b) { int i=a.length()-1; int j=b.length()-1; int carry=0; StringBuffer s=new StringBuffer(); whil...原创 2019-10-11 09:50:16 · 133 阅读 · 0 评论 -
LeetCode精选100题——第66题——加一
class Solution { public int[] plusOne(int[] digits) { for(int i=digits.length-1;i>=0;i--){ if(digits[i]<9){ digits[i]+=1; return digits;...原创 2019-10-10 20:57:23 · 166 阅读 · 0 评论 -
LeetCode精选100题——第64题——最小路径和
class Solution { public int minPathSum(int[][] grid) { int m=grid.length; int n=grid[0].length; int[][] res=new int[m][n]; res[0][0]=grid[0][0]; for(int i=...原创 2019-10-10 20:39:12 · 103 阅读 · 0 评论 -
LeetCode精选100题——第48题——旋转图像
class Solution { public void rotate(int[][] matrix) { int rowStart=0; int colStart=0; int rowEnd=matrix.length-1; int colEnd=matrix[0].length-1; while(rowS...原创 2019-10-08 18:15:58 · 98 阅读 · 0 评论 -
LeetCode精选100题——第47题——全排列 II
class Solution { public List<List<Integer>> permuteUnique(int[] nums) { ArrayList<List<Integer>> res=new ArrayList<>(); if(nums==null||nums.length==0...原创 2019-10-08 16:43:04 · 100 阅读 · 0 评论 -
LeetCode精选100题——第16题——最接近的三数之和
class Solution { public int threeSumClosest(int[] nums, int target) { int res=nums[0]+nums[1]+nums[nums.length-1]; Arrays.sort(nums); for(int i=0;i<nums.length-2;i++){...原创 2019-10-06 16:37:03 · 199 阅读 · 0 评论 -
LeetCode精选100题——第15题——三数之和
import java.util.ArrayList;import java.util.Arrays;import java.util.List;class Solution { public List<List<Integer>> threeSum(int[] nums) { ArrayList<List<Integer>...原创 2019-10-06 15:49:04 · 112 阅读 · 0 评论 -
LeetCode精选100题——第14题——最长公共前缀
class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length==0){ return ""; } int len=strs.length; int minLen=Integer.MAX_VALUE;...原创 2019-10-06 14:23:53 · 94 阅读 · 0 评论 -
LeetCode精选100题——第13题——罗马数字转整数
import java.util.HashMap;class Solution { public int romanToInt(String s) { HashMap<Character,Integer> map=new HashMap<>(); map.put('I',1); map.put('V',5); ...原创 2019-10-06 12:55:45 · 137 阅读 · 0 评论 -
LeetCode精选100题——第12题——整数转罗马数字
class Solution { public String intToRoman(int num) { int[] str1 = {1,4,5,9,10,40,50,90,100,400,500,900,1000}; String[] str2={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM",...原创 2019-10-06 12:10:17 · 75 阅读 · 0 评论 -
LeetCode精选100题——第11题——盛最多水的容器
class Solution { public int maxArea(int[] height) { int l=0; int r=height.length-1; int res=0; while(l<r){ int area=Math.min(height[l],height[r])*(r...原创 2019-10-02 15:36:48 · 179 阅读 · 0 评论 -
LeetCode精选100题——第09题——回文数
class Solution { public boolean isPalindrome(int x) { if (x < 0) { return false; } int temp = x; int rev = 0; while (x > 0) { ...原创 2019-10-02 14:45:56 · 70 阅读 · 0 评论 -
LeetCode精选100题——第08题——字符串转换整数
class Solution { public int myAtoi(String str) { if (str == null || str.trim().length() == 0) { return 0; } int start = 0; int sign = 1; long ...原创 2019-10-02 14:26:50 · 82 阅读 · 0 评论