|
| 1 | +## Week01 homework |
| 2 | +<br> |
| 3 | + |
| 4 | +### 删除排序数组中的重复项 |
| 5 | +```java |
| 6 | +public int removeDuplicates(int[] nums) { |
| 7 | + if (nums == null && nums.length == 0) { |
| 8 | + return 0; |
| 9 | + } |
| 10 | + int left = 0; |
| 11 | + for (int i = 1; i < nums.length; i++) { |
| 12 | + if (nums[i] != nums[left]) { |
| 13 | + nums[++left] = nums[i]; |
| 14 | + } |
| 15 | + } |
| 16 | + return ++left; |
| 17 | +} |
| 18 | +``` |
| 19 | +<br/> |
| 20 | + |
| 21 | +### 旋转数组 |
| 22 | +```java |
| 23 | +public void rotate(int[] nums, int k) { |
| 24 | + // 使用环状替代,k %= nums.length |
| 25 | + k %= nums.length; |
| 26 | + int count = 0; |
| 27 | + for (int start = 0; count < nums.length; start++) { |
| 28 | + int current = start; |
| 29 | + int pre = nums[start]; |
| 30 | + do { |
| 31 | + int next = (current + k) % nums.length; |
| 32 | + int temp = nums[next]; |
| 33 | + nums[next] = pre; |
| 34 | + pre = temp; |
| 35 | + current = next; |
| 36 | + count++; |
| 37 | + } while (start != current); |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | +<br/> |
| 42 | + |
| 43 | +### 合并两个有序链表 |
| 44 | +```java |
| 45 | +public ListNode mergeTwoLists(ListNode l1, ListNode l2) { |
| 46 | + ListNode preHead = new ListNode(-1); |
| 47 | + ListNode pre = preHead; |
| 48 | + while (l1 != null && l2 != null) { |
| 49 | + if (l1.val < l2.val) { |
| 50 | + pre.next = l1; |
| 51 | + l1 = l1.next; |
| 52 | + } else { |
| 53 | + pre.next = l2; |
| 54 | + l2 = l2.next; |
| 55 | + } |
| 56 | + pre = pre.next; |
| 57 | + } |
| 58 | + pre.next = l1 == null ? l2 : l1; |
| 59 | + return preHead.next; |
| 60 | +} |
| 61 | +``` |
| 62 | +<br/> |
| 63 | + |
| 64 | +### 合并两个有序数组 |
| 65 | +```java |
| 66 | +public void merge(int[] nums1, int m, int[] nums2, int n) { |
| 67 | + while (m > 0 && n > 0) { |
| 68 | + nums1[m + n - 1] = nums1[m - 1] > nums2[n - 1] ? nums1[--m] : nums2[--n]; |
| 69 | + } |
| 70 | + for (int i = 0; i < n; i++) { |
| 71 | + nums1[i] = nums2[i]; |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | +<br/> |
| 76 | + |
| 77 | +### 两数之和 |
| 78 | +```java |
| 79 | +public int[] twoSum(int[] nums, int target) { |
| 80 | + // 循环遍历数组,num[i]存在于map中,直接返回map中的value和i |
| 81 | + // 否则,将key:target - nums[i] value:i 存入map中 |
| 82 | + int length = nums.length; |
| 83 | + Map<Integer, Integer> map = new HashMap<>(length); |
| 84 | + for (int i = 0; i < length; i++) { |
| 85 | + if (map.containsKey(nums[i])) { |
| 86 | + return new int[]{map.get(nums[i]), i}; |
| 87 | + } |
| 88 | + map.put(target - nums[i], i); |
| 89 | + } |
| 90 | + return null; |
| 91 | +} |
| 92 | +``` |
| 93 | +<br/> |
| 94 | + |
| 95 | +### 移动零 |
| 96 | +```java |
| 97 | +public void moveZeroes(int[] nums) { |
| 98 | + if (nums == null) { |
| 99 | + return; |
| 100 | + } |
| 101 | + int index = 0; |
| 102 | + for (int i = 0; i < nums.length; i++) { |
| 103 | + if (nums[i] != 0) { |
| 104 | + if (index != i) { |
| 105 | + nums[index] = nums[i]; |
| 106 | + nums[i] = 0; |
| 107 | + } |
| 108 | + index++; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +### 加一 |
| 115 | +```java |
| 116 | +public int[] plusOne(int[] digits) { |
| 117 | + // digits数组中所有位全进1,则新new一个digits.length + 1长度的数组,并把首位置为1 |
| 118 | + for (int i = digits.length - 1; i >= 0; i--) { |
| 119 | + digits[i] += 1; |
| 120 | + if (digits[i] % 10 != 0) { |
| 121 | + return digits; |
| 122 | + } |
| 123 | + digits[i] = 0; |
| 124 | + } |
| 125 | + digits = new int[digits.length + 1]; |
| 126 | + digits[0] = 1; |
| 127 | + return digits; |
| 128 | +} |
| 129 | +``` |
0 commit comments