You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Kangli/README.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ Hint: Related problem: Reverse Words in a String II. An "easy" problem but with
14
14
*Thoughts: Really struggled with the constraint of doing it in place. So tried solving it with extra space first.
15
15
Just append the elements in the correct positions in the new results array. But coming up with the correct positions requires thinking about two conditions. As we iterate through the input nums, we simply put put nums[i] at res[i+k] if i+k is a valid index (i + k < len(nums) ). But if i + k exceeds len(nums), what to do? Also k can be any positive number, meaning it could be several times len(nums). I worked out a few examples arr = [1, 2, 3], k=1, k= 2, k = 5 etc. Then I found the pattern that the correct position corresponds to (i + k) % len(nums), for any i and k. This is also correct mathematically because we are really looking for the number (i + k) that "wraps around" len(nums), which is exactly what modulo arithmetic can be used for.*
16
16
17
-
*I later looked up an in-place solution that pops the last item of the array and inserts it to the beginning, repeating for k % len(nums) times.*
17
+
*I later looked up an in-place solution that pops the last item of the array and inserts it to the beginning, repeating for k % len(nums) times.*
*Thoughts: Using a dictionary (hash), insert each sorted string into the dictionary as keys, using the original string as the value. Need the values to be an array, so initialize the value of each key as [] and add the original string directly to [] each time. Could also use defaultdict (python specific).*
66
+
*Thoughts: Using a dictionary (hash), insert each sorted string into the dictionary as keys, using the original string as the value. Need the values to be an array, so initialize the value of each key as [] and add the original string directly to [] each time. Could also use defaultdict (python specific).*
0 commit comments