- 博客(14)
- 收藏
- 关注
原创 15. 3Sum
15. 3Sum先排序,然后遍历选出一个数,并找出另两个数的和是他的相反数即可。每个循环中左右指针靠拢查找是否和为他的相反数,注意遍历时重复的去掉。class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> r
2018-01-07 14:09:31 97
原创 66. Plus One
66. Plus Oneclass Solution { public int[] plusOne(int[] digits) { for(int i = digits.length-1;i>=0;i--){ if(digits[i]<9){ digits[i]++; return dig
2018-01-07 12:18:44 112
原创 56. Merge Intervals
56. Merge Intervals先对Interval排序,然后如果下一个的start大于前一个的end,结果集里新增一个Interval,否则只需把当前最后一个Interval的end改为和当前Interval的end中较大的那个。/** * Definition for an interval. * public class Interval { * int start; *
2018-01-07 11:57:31 119
原创 45. Jump Game II
45. Jump Game II题目已经假定可以达到终点 step记录步数,reachInStep记录在step内可到达的最大距离,reach在遍历nums时记录0到i中的最大可到达距离,reachInStep一旦小于i就说明必须要再走一步。class Solution { public int jump(int[] nums) { int step = 0;
2018-01-06 22:43:40 133
原创 55. Jump Game
55. Jump Game遍历数组,同时一个变量记录最大距离(reach)class Solution { public boolean canJump(int[] nums) { int i = 0; int reach = 0; while(i<nums.length&&i<=reach){ reach = Math
2018-01-06 21:18:13 133
原创 53、Maximum Subarray
53、Maximum Subarray一个变量(result)记录到i为止最大和,另一个(max)累积到小于零就清零。class Solution { public int maxSubArray(int[] nums) { int result = Integer.MIN_VALUE; int max = 0; for(int i = 0;i
2018-01-06 20:27:57 141
原创 48、Rotate Image
48. Rotate Imageclass Solution { public void rotate(int[][] matrix) { int l = 0; int r = matrix.length-1; int m = (l+r)/2; for(;l<=m;l++){ for(int i = l;
2018-01-05 22:57:36 101
原创 35. Search Insert Position
35. Search Insert Position二分查找,如果target不存在于数组中,最后跳出while循环时的mid就是target的相邻元素下标,判断target如果大于mid,结果就是mid+1,如果小于mid,结果就是mid。class Solution { public int searchInsert(int[] nums, int target) { i
2018-01-04 23:17:04 100
原创 34. Search for a Range
34. Search for a Range 使用二分查找,和普通二分查找不同的是,mid=target时继续查找,直到左指针等于右指针,来找出数组中等于target的元素里下标最小和最大的。 class Solution { public int[] searchRange(int[] nums, int target) { if(nums.length==0){
2018-01-04 22:37:06 125
原创 31. Next Permutation
31. Next Permutationclass Solution { public void nextPermutation(int[] nums) { int len = nums.length-1; int key = 0; for(int i=len;i>=1;i--){ if(nums[i]<=nums[i-
2018-01-03 23:55:34 81
原创 26、Remove Duplicates from Sorted Array
26、 Remove Duplicates from Sorted Array public int removeDuplicates(int[] nums) { int index = 0; if(nums.length<=1){ return nums.length; } for(int i = 1;i
2018-01-03 20:46:14 106
原创 11、Container With Most Water
11、Container With Most Waterclass Solution { public int maxArea(int[] height) { if(height.length<2) return 0; int l = 0; int max = 0; int r = height.leng
2018-01-03 17:40:19 106
原创 1、Two Sum
1、Two Sumpublic int[] twoSum(int[] nums, int target) { HashMap<Integer,Integer> m = new HashMap<Integer,Integer>(); for(int i = 0;i<nums.length;i++){ if(m.containsKey(targe
2018-01-03 17:33:00 123
原创 替换空格
No.4 public class Example { public static void main(String args[]){ String s = "Hello World!"; System.out.print(replaceSpace(s)); } public static String replaceSpace(String s){ if(s==null)
2017-05-13 22:31:57 234
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人