GeezerVison is showing github.com — Refine 1304_Find_N_Unique_Integers_Sum_up_to_Zero · pengaoao/leetcode@a99c1d0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -227,6 +227,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal | 227 | 227 | | 1108 | [Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1108_Defanging_an_IP_Address.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1108_Defanging_an_IP_Address.java) | String manipulate (split, replace and join), O(n) and O(n) | | 228 | 228 | | 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1260_Shift_2D_Grid.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1260_Shift_2D_Grid.java) | Final position of each element can be computed according to k, m and n, e.g., k == mn, then don't move, O(mn) and O(mn) | | 229 | 229 | | 1290 | [Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1290_Convert_Binary_Number_in_a_Linked_List_to_Integer.py) | Take 2 to the power digit position from right (starting from 0) and multiply it with the digit | | | 230 | +| 1304 | [Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1304_Find_N_Unique_Integers_Sum_up_to_Zero.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1304_Find_N_Unique_Integers_Sum_up_to_Zero.java) | [1,n-1] and its negative sum | | 230 | 231 | | 1323 | [Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1323_Maximum_69_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1323_Maximum_69_Number.java) | 9 is greater than 6, so change first 6 to 9 from left if exist, O(n) and O(1) | | 231 | 232 | | 1337 | [The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1337_The_K_Weakest_Rows_in_a_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1337_The_K_Weakest_Rows_in_a_Matrix.java) | Check by row, from left to right, until encount first zero, O(mn) and O(1) | | 232 | 233 | | 1342 | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1342_Number_of_Steps_to_Reduce_a_Number_to_Zero.py) | If number is divisible by 2, divide the number by 2, else subtract 1 from the number, and output the number of steps, O(logn) and O(1) | | |
Collapse file Copy file name to clipboard +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +public int[] sumZero(int n) { | | 2 | + int[] res = new int[n]; | | 3 | + // place negative sum(from 1 to n-1) in 0 | | 4 | + for (int i = 1; i < n; i++) { | | 5 | + res[i] = i; | | 6 | + res[0] -= i; | | 7 | + } | | 8 | + return res; | | 9 | +} |
Collapse file Copy file name to clipboardExpand all lines: python/1304_Find_N_Unique_Integers_Sum_up_to_Zero -11Lines changed: 0 additions & 11 deletions Collapse file Copy file name to clipboard +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +class Solution: | | 2 | + def sumZero(self, n: int) -> List[int]: | | 3 | + prefix_sum = 0 | | 4 | + res = [] | | 5 | + # 1, n-1 | | 6 | + for i in range(1, n): | | 7 | + res.append(i) | | 8 | + prefix_sum = prefix_sum + i | | 9 | + # sum(from 1 to n-1) | | 10 | + res.append(-prefix_sum) | | 11 | + return res | | 12 | + | | 13 | + # def sumZero(self, n: int) -> List[int]: | | 14 | + # # 1,n-1 | | 15 | + # prefix = list(range(1, n)) | | 16 | + # # sum(from 1 to n-1) | | 17 | + # return prefix + [-sum(prefix)] |
|