Skip to content

Commit e872c65

Browse files
committed
note
1 parent 4320f72 commit e872c65

File tree

3 files changed

+146
-4
lines changed

3 files changed

+146
-4
lines changed

guangxu/202107/20210701.md

+80-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,89 @@
11
## Algorithm
22

3+
[151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/)
4+
35
### Description
46

5-
### Solution
7+
Given an input string s, reverse the order of the words.
8+
9+
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
10+
11+
Return a string of the words in reverse order concatenated by a single space.
12+
13+
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
14+
15+
16+
Example 1:
17+
18+
```
19+
Input: s = "the sky is blue"
20+
Output: "blue is sky the"
21+
```
22+
23+
Example 2:
24+
25+
```
26+
Input: s = " hello world "
27+
Output: "world hello"
28+
Explanation: Your reversed string should not contain leading or trailing spaces.
29+
```
30+
31+
Example 3:
632

7-
```java
33+
```
34+
Input: s = "a good example"
35+
Output: "example good a"
36+
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
37+
```
38+
39+
Example 4:
40+
41+
```
42+
Input: s = " Bob Loves Alice "
43+
Output: "Alice Loves Bob"
44+
```
45+
46+
Example 5:
47+
48+
```
49+
Input: s = "Alice does not even like bob"
50+
Output: "bob like even not does Alice"
51+
```
52+
53+
Constraints:
54+
55+
- 1 <= s.length <= 104
56+
- s contains English letters (upper-case and lower-case), digits, and spaces ' '.
57+
- There is at least one word in s.
58+
59+
Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?
60+
61+
### Solution
862

63+
```java
64+
class Solution {
65+
public String reverseWords(String s) {
66+
Stack<String> stack = new Stack<>();
67+
String temp = "";
68+
for(int i=0;i<s.length();i++){
69+
if(s.charAt(i)==' '&&temp.length()!=0){
70+
stack.push(temp);
71+
temp = "";
72+
}else if(s.charAt(i)!=' '){
73+
temp += s.charAt(i);
74+
}
75+
if(i==s.length()-1){
76+
stack.push(temp.trim());
77+
}
78+
}
79+
String result = "";
80+
while(!stack.isEmpty()){
81+
result += stack.pop();
82+
result += " ";
83+
}
84+
return result.strip();
85+
}
86+
}
987
```
1088

1189
### Discuss

guangxu/202107/20210702.md

+64-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,73 @@
11
## Algorithm
22

3+
[287. Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/)
4+
35
### Description
46

5-
### Solution
7+
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
8+
9+
There is only one repeated number in nums, return this repeated number.
10+
11+
You must solve the problem without modifying the array nums and uses only constant extra space.
12+
13+
14+
Example 1:
15+
16+
```
17+
Input: nums = [1,3,4,2,2]
18+
Output: 2
19+
```
20+
21+
Example 2:
22+
23+
```
24+
Input: nums = [3,1,3,4,2]
25+
Output: 3
26+
```
627

7-
```java
28+
Example 3:
29+
30+
```
31+
Input: nums = [1,1]
32+
Output: 1
33+
```
34+
35+
Example 4:
36+
37+
```
38+
Input: nums = [1,1,2]
39+
Output: 1
40+
```
41+
42+
Constraints:
43+
44+
- 1 <= n <= 105
45+
- nums.length == n + 1
46+
- 1 <= nums[i] <= n
47+
- All the integers in nums appear only once except for precisely one integer which appears two or more times.
48+
49+
Follow up:
50+
51+
- How can we prove that at least one duplicate number must exist in nums?
52+
- Can you solve the problem in linear runtime complexity?
53+
54+
### Solution
855

56+
```java
57+
class Solution {
58+
public int findDuplicate(int[] nums) {
59+
int[] duplicate = new int[nums.length];
60+
for(int i=0;i<nums.length;i++){
61+
duplicate[nums[i]]++;
62+
}
63+
for(int i=0;i<duplicate.length;i++){
64+
if(duplicate[i]>1){
65+
return i;
66+
}
67+
}
68+
return -1;
69+
}
70+
}
971
```
1072

1173
### Discuss

guangxu/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
| 标题 | Algorithm | Review | Tip | Share|
22
| - | - | - | - | - |
3+
| [20210701](./202107/20210702.md) |[287. Find the Duplicate Number(发现数组中重复的数字)](https://leetcode.com/problems/find-the-duplicate-number/)||||
4+
| [20210701](./202107/20210701.md) |[151. Reverse Words in a String(翻转字符串里的单词)](https://leetcode.com/problems/reverse-words-in-a-string/)||||
35
| [20210630](./202106/20210630.md) |[135. Candy(分发糖果)](https://leetcode.com/problems/candy/)||||
46
| [20210629](./202106/20210629.md) |[129. Sum Root to Leaf Numbers(求根到叶子节点数字之和)](https://leetcode.com/problems/sum-root-to-leaf-numbers/)||||
57
| [20210628](./202106/20210628.md) |[670. Maximum Swap(数组中两个数的最大异或值)](https://leetcode.com/problems/maximum-swap/)||||

0 commit comments

Comments
 (0)