数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
@Test public void test() { int[] test = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}; System.out.println(maxArea2(test)); }
public int maxArea1(int[] height) { int maxArea = 0; for (int i = 0; i < height.length; i++) { for (int j = i + 1; j < height.length; j++) { maxArea = Math.max(maxArea, Math.min(height[i], height[j]) * (j - i)); } } return maxArea; }
public int maxArea2(int[] height) { int i = 0, j = height.length - 1, maxArea = 0; while (i < j) { maxArea = height[i] > height[j] ? Math.max(maxArea, (j - i) * height[j--]) : Math.max(maxArea, (j - i) * height[i++]); } return maxArea; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
@Test public void test() { int[] test = new int[]{0, 1, 0, 3, 12}; moveZeroes1(test); moveZeroes2(test); }
public void moveZeroes1(int[] nums) { int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != 0) { nums[count++] = nums[i]; } } for (int i = count; i < nums.length; i++) { nums[i] = 0; } System.out.println(Arrays.toString(nums)); }
public void moveZeroes2(int[] nums) { int j = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != 0) { if (i > j) { nums[j] = nums[i]; nums[i] = 0; } j++; } } Syste.out.println(Arrays.toString(nums)); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
@Test public void test() { System.out.println(climbStairs(3)); }
public int climbStairs(int n) { int p = 0; int q = 0; int r = 1; for (int i = 1; i <= n; i++) { p = q; q = r; r = p + q; } return r; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
@Test public void test() { System.out.println(Arrays.toString(twoSum1(new int[]{1, 3, 4, 2}, 6))); System.out.println(Arrays.toString(twoSum2(new int[]{1, 3, 4, 2}, 6))); System.out.println(Arrays.toString(twoSum3(new int[]{1, 3, 4, 2}, 6))); }
public int[] twoSum1(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] + nums[j] == target) { return new int[]{i, j}; } } } throw new IllegalArgumentException("No two sum solution"); }
public int[] twoSum2(int[] nums, int target) { Map<Integer, Integer> hashMap = new HashMap<>(); for (int i = 0; i < nums.length; i++) { hashMap.put(nums[i], i); } for (int i = 0; i < nums.length; i++) { if (hashMap.containsKey(target - nums[i]) && hashMap.get(target - nums[i]) != i) { return new int[]{i, hashMap.get(target - nums[i])}; } } throw new IllegalArgumentException("No two sum solution"); }
public int[] twoSum3(int[] nums, int target) { Map<Integer, Integer> hashMap = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (hashMap.containsKey(target - nums[i]) && hashMap.get(target - nums[i]) != i) { return new int[]{hashMap.get(target - nums[i]), i}; } hashMap.put(nums[i], i); } throw new IllegalArgumentException("No two sum solution"); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
@Test public void test() { System.out.println(threeSum(new int[]{-1, 0, 1, 2, -1, -4})); System.out.println(threeSum(new int[]{0, 0, 0, 0, 0, 0})); }
public List<List<Integer>> threeSum(int[] nums) { ArrayList<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); int n = nums.length; if (n < 3) { return list; } for (int i = 0; i < n; i++) { if (nums[i] > 0) { break; } if (i > 0 && nums[i] == nums[i - 1]) { continue; } int l = i + 1; int r = n - 1; while (l < r) { int result = nums[i] + nums[l] + nums[r]; if (result == 0) { list.add(Arrays.asList(nums[i], nums[l], nums[r])); while (l < r && nums[l] == nums[l + 1]) { l++; } while (l < r && nums[r] == nums[r - 1]) { r--; } l++; r--; } else if (result > 0) { r--; } else { l++; } } } return list; }
|
这个题目真的需要自己去思考,debug调试一下,我写的注释仅仅是我个人理解。
链表