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
+22-6Lines changed: 22 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,22 @@
1
+
### How I Practice ###
1
2
Keeping a record of thoughts behind solving certain problems
2
3
3
4
1/4/18 Making some catch-up edits.
5
+
**189: Rotate Array-**
6
+
Rotate an array of n elements to the right by k steps.
7
+
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
8
+
9
+
Note:
10
+
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
11
+
Hint: Related problem: Reverse Words in a String II. An "easy" problem but with a low acceptance rate, only 25%, wonder why.
12
+
13
+
*Thoughts: Really struggled with the constraint of doing it in place. So tried solving it with extra space first.
14
+
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.
15
+
16
+
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.
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
@@ -11,7 +27,7 @@ s = "loveleetcode",
11
27
return 2.
12
28
Note: You may assume the string contain only lowercase letters.
13
29
14
-
Thoughts: Straightforward use of a hash to store the counts of each letter in the string. Then iterate the string again from the beginning and return the first letter with count of 1.
30
+
*Thoughts: Straightforward use of a hash to store the counts of each letter in the string. Then iterate the string again from the beginning and return the first letter with count of 1.*
**443: String Compression-** Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every element of the array should be a character (not int) of length 1. After you are done modifying the input array in-place, return the new length of the array.
@@ -24,16 +40,16 @@ Return 6, and the first 6 characters of the input array should be: ["a","2","b",
24
40
Explanation:
25
41
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".
26
42
27
-
Thoughts: Easy if not doing it in place. I made an extra array, added the character and added its count
28
-
if count > 1. Return the length of new array at the end.
43
+
*Thoughts: Easy if not doing it in place. I made an extra array, added the character and added its count
44
+
if count > 1. Return the length of new array at the end. *
**219: Contains Duplicate II-** Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
34
-
Thoughts:
50
+
*Thoughts:
35
51
Use a dictionary to store each integer and its index in nums as key, value pairs.
36
-
If a value is already in the dictionary, check whether index difference <= k.
52
+
If a value is already in the dictionary, check whether index difference <= k. *
37
53
38
54
Implementation: did not pass on first 2 tries, failed for [1, 0, 1, 1, ], 1 (true got false). Need to update
39
55
index if value already in dictionary but i - d[n] > k.
@@ -47,7 +63,7 @@ Return:
47
63
["nat","tan"],
48
64
["bat"]
49
65
]
50
-
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