Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc21a45

Browse files
rotate array
1 parent 30a80e1 commit dc21a45

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

‎Kangli/README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
### How I Practice ###
12
Keeping a record of thoughts behind solving certain problems
23

34
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.
17+
*
18+
[Submission](https://github.com/kanglicheng/python-leetcode/blob/mySolutions/Kangli/Arrays/rotateArray.py)
19+
420

521
**387: First Unique Character in a String-**
622
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",
1127
return 2.
1228
Note: You may assume the string contain only lowercase letters.
1329

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.*
1531
[Submission](https://github.com/kanglicheng/python-leetcode/blob/mySolutions/Kangli/Strings/firstUniqueChar.py)
1632

1733
**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",
2440
Explanation:
2541
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".
2642

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. *
2945
[Submission](https://github.com/kanglicheng/python-leetcode/blob/mySolutions/Kangli/Strings/stringCompression.py)
3046

3147
10/27/17
3248

3349
**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:
3551
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. *
3753

3854
Implementation: did not pass on first 2 tries, failed for [1, 0, 1, 1, ], 1 (true got false). Need to update
3955
index if value already in dictionary but i - d[n] > k.
@@ -47,7 +63,7 @@ Return:
4763
["nat","tan"],
4864
["bat"]
4965
]
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). *
5167
[Submission](https://github.com/kanglicheng/python-leetcode/blob/mySolutions/Kangli/Hash%20Table/group_anagrams.py)
5268

5369
11/7/17

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /