Skip to content

Commit ed2cf4d

Browse files
committed
feat: update lc problems
1 parent 54f98b3 commit ed2cf4d

File tree

25 files changed

+278
-215
lines changed

25 files changed

+278
-215
lines changed

solution/0000-0099/0055.Jump Game/README.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,33 @@ func max(a, b int) int {
135135
}
136136
```
137137

138+
### **JavaScript**
139+
140+
```js
141+
/**
142+
* @param {number[]} nums
143+
* @return {boolean}
144+
*/
145+
var canJump = function (nums) {
146+
let mx = 0;
147+
for (let i = 0; i < nums.length; ++i) {
148+
if (i > mx) {
149+
return false;
150+
}
151+
mx = Math.max(mx, i + nums[i]);
152+
}
153+
return true;
154+
};
155+
```
156+
138157
### **C#**
139158

140159
```cs
141160
public class Solution {
142161
public bool CanJump(int[] nums) {
143162
int mx = 0;
144-
for (int i = 0; i < nums.Length; ++i)
145-
{
146-
if (i > mx)
147-
{
163+
for (int i = 0; i < nums.Length; ++i) {
164+
if (i > mx) {
148165
return false;
149166
}
150167
mx = Math.Max(mx, i + nums[i]);

solution/0000-0099/0055.Jump Game/README_EN.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,33 @@ func max(a, b int) int {
107107
}
108108
```
109109

110+
### **JavaScript**
111+
112+
```js
113+
/**
114+
* @param {number[]} nums
115+
* @return {boolean}
116+
*/
117+
var canJump = function (nums) {
118+
let mx = 0;
119+
for (let i = 0; i < nums.length; ++i) {
120+
if (i > mx) {
121+
return false;
122+
}
123+
mx = Math.max(mx, i + nums[i]);
124+
}
125+
return true;
126+
};
127+
```
128+
110129
### **C#**
111130

112131
```cs
113132
public class Solution {
114133
public bool CanJump(int[] nums) {
115134
int mx = 0;
116-
for (int i = 0; i < nums.Length; ++i)
117-
{
118-
if (i > mx)
119-
{
135+
for (int i = 0; i < nums.Length; ++i) {
136+
if (i > mx) {
120137
return false;
121138
}
122139
mx = Math.Max(mx, i + nums[i]);

solution/0000-0099/0055.Jump Game/Solution.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
public class Solution {
22
public bool CanJump(int[] nums) {
33
int mx = 0;
4-
for (int i = 0; i < nums.Length; ++i)
5-
{
6-
if (i > mx)
7-
{
4+
for (int i = 0; i < nums.Length; ++i) {
5+
if (i > mx) {
86
return false;
97
}
108
mx = Math.Max(mx, i + nums[i]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var canJump = function (nums) {
6+
let mx = 0;
7+
for (let i = 0; i < nums.length; ++i) {
8+
if (i > mx) {
9+
return false;
10+
}
11+
mx = Math.max(mx, i + nums[i]);
12+
}
13+
return true;
14+
};

solution/0000-0099/0058.Length of Last Word/README.md

+55-49
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:逆向遍历 + 双指针**
54+
55+
从字符串 $s$ 末尾开始遍历,找到第一个不为空格的字符,即为最后一个单词的最后一个字符,下标记为 $i$。然后继续向前遍历,找到第一个为空格的字符,即为最后一个单词的第一个字符的前一个字符,记为 $j$。那么最后一个单词的长度即为 $i - j$。
56+
57+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 长度。
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**
@@ -59,16 +65,13 @@
5965
```python
6066
class Solution:
6167
def lengthOfLastWord(self, s: str) -> int:
62-
last_word_length = 0
63-
meet_word = False
64-
for i in range(len(s) - 1, -1, -1):
65-
ch = ord(s[i])
66-
if ch >= 65 and ch <= 122:
67-
meet_word = True
68-
last_word_length += 1
69-
elif meet_word:
70-
break
71-
return last_word_length
68+
i = len(s) - 1
69+
while i >= 0 and s[i] == ' ':
70+
i -= 1
71+
j = i
72+
while j >= 0 and s[j] != ' ':
73+
j -= 1
74+
return i - j
7275
```
7376

7477
### **Java**
@@ -78,64 +81,67 @@ class Solution:
7881
```java
7982
class Solution {
8083
public int lengthOfLastWord(String s) {
81-
int n = s.length();
82-
int lastWordLength = 0;
83-
boolean meetWord = false;
84-
for (int i = n - 1; i >= 0; --i) {
85-
char ch = s.charAt(i);
86-
if (ch >= 'A' && ch <= 'z') {
87-
meetWord = true;
88-
++lastWordLength;
89-
} else if (meetWord) {
90-
break;
91-
}
84+
int i = s.length() - 1;
85+
while (i >= 0 && s.charAt(i) == ' ') {
86+
--i;
9287
}
93-
return lastWordLength;
88+
int j = i;
89+
while (j >= 0 && s.charAt(j) != ' ') {
90+
--j;
91+
}
92+
return i - j;
9493
}
9594
}
9695
```
9796

97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int lengthOfLastWord(string s) {
103+
int i = s.length() - 1;
104+
while (i >= 0 && s[i] == ' ') --i;
105+
int j = i;
106+
while (j >= 0 && s[j] != ' ') --j;
107+
return i - j;
108+
}
109+
};
110+
```
111+
98112
### **Go**
99113
100114
```go
101115
func lengthOfLastWord(s string) int {
102-
if len(s) == 0 {
103-
return 0
104-
}
105-
space := []byte(" ")[0]
106-
for len(s) != 0 && s[len(s)-1] == space {
107-
s = s[:len(s)-1]
116+
i := len(s) - 1
117+
for i >= 0 && s[i] == ' ' {
118+
i--
108119
}
109-
ret := 0
110-
for i := len(s) - 1; i >= 0; i-- {
111-
if s[i] != space {
112-
ret++
113-
} else {
114-
return ret
115-
}
120+
j := i
121+
for j >= 0 && s[j] != ' ' {
122+
j--
116123
}
117-
return ret
124+
return i - j
118125
}
119126
```
120127

121128
### **JavaScript**
122129

123130
```js
131+
/**
132+
* @param {string} s
133+
* @return {number}
134+
*/
124135
var lengthOfLastWord = function (s) {
125-
s = s.trim();
126-
return s.length - s.lastIndexOf(' ') - 1;
127-
};
128-
129-
var lengthOfLastWord2 = function (s) {
130-
let res = 0;
131-
for (let i = 0; i < s.length; i++) {
132-
if (s[i] !== ' ' && (i === 0 || s[i - 1] === ' ')) {
133-
res = 1;
134-
} else if (s[i] !== ' ') {
135-
res++;
136-
}
136+
let i = s.length - 1;
137+
while (i >= 0 && s[i] === ' ') {
138+
--i;
139+
}
140+
let j = i;
141+
while (j >= 0 && s[j] !== ' ') {
142+
--j;
137143
}
138-
return res;
144+
return i - j;
139145
};
140146
```
141147

solution/0000-0099/0058.Length of Last Word/README_EN.md

+49-49
Original file line numberDiff line numberDiff line change
@@ -51,81 +51,81 @@
5151
```python
5252
class Solution:
5353
def lengthOfLastWord(self, s: str) -> int:
54-
last_word_length = 0
55-
meet_word = False
56-
for i in range(len(s) - 1, -1, -1):
57-
ch = ord(s[i])
58-
if ch >= 65 and ch <= 122:
59-
meet_word = True
60-
last_word_length += 1
61-
elif meet_word:
62-
break
63-
return last_word_length
54+
i = len(s) - 1
55+
while i >= 0 and s[i] == ' ':
56+
i -= 1
57+
j = i
58+
while j >= 0 and s[j] != ' ':
59+
j -= 1
60+
return i - j
6461
```
6562

6663
### **Java**
6764

6865
```java
6966
class Solution {
7067
public int lengthOfLastWord(String s) {
71-
int n = s.length();
72-
int lastWordLength = 0;
73-
boolean meetWord = false;
74-
for (int i = n - 1; i >= 0; --i) {
75-
char ch = s.charAt(i);
76-
if (ch >= 'A' && ch <= 'z') {
77-
meetWord = true;
78-
++lastWordLength;
79-
} else if (meetWord) {
80-
break;
81-
}
68+
int i = s.length() - 1;
69+
while (i >= 0 && s.charAt(i) == ' ') {
70+
--i;
71+
}
72+
int j = i;
73+
while (j >= 0 && s.charAt(j) != ' ') {
74+
--j;
8275
}
83-
return lastWordLength;
76+
return i - j;
8477
}
8578
}
8679
```
8780

81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int lengthOfLastWord(string s) {
87+
int i = s.length() - 1;
88+
while (i >= 0 && s[i] == ' ') --i;
89+
int j = i;
90+
while (j >= 0 && s[j] != ' ') --j;
91+
return i - j;
92+
}
93+
};
94+
```
95+
8896
### **Go**
8997
9098
```go
9199
func lengthOfLastWord(s string) int {
92-
if len(s) == 0 {
93-
return 0
94-
}
95-
space := []byte(" ")[0]
96-
for len(s) != 0 && s[len(s)-1] == space {
97-
s = s[:len(s)-1]
100+
i := len(s) - 1
101+
for i >= 0 && s[i] == ' ' {
102+
i--
98103
}
99-
ret := 0
100-
for i := len(s) - 1; i >= 0; i-- {
101-
if s[i] != space {
102-
ret++
103-
} else {
104-
return ret
105-
}
104+
j := i
105+
for j >= 0 && s[j] != ' ' {
106+
j--
106107
}
107-
return ret
108+
return i - j
108109
}
109110
```
110111

111112
### **JavaScript**
112113

113114
```js
115+
/**
116+
* @param {string} s
117+
* @return {number}
118+
*/
114119
var lengthOfLastWord = function (s) {
115-
s = s.trim();
116-
return s.length - s.lastIndexOf(' ') - 1;
117-
};
118-
119-
var lengthOfLastWord2 = function (s) {
120-
let res = 0;
121-
for (let i = 0; i < s.length; i++) {
122-
if (s[i] !== ' ' && (i === 0 || s[i - 1] === ' ')) {
123-
res = 1;
124-
} else if (s[i] !== ' ') {
125-
res++;
126-
}
120+
let i = s.length - 1;
121+
while (i >= 0 && s[i] === ' ') {
122+
--i;
123+
}
124+
let j = i;
125+
while (j >= 0 && s[j] !== ' ') {
126+
--j;
127127
}
128-
return res;
128+
return i - j;
129129
};
130130
```
131131

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int lengthOfLastWord(string s) {
4+
int i = s.length() - 1;
5+
while (i >= 0 && s[i] == ' ') --i;
6+
int j = i;
7+
while (j >= 0 && s[j] != ' ') --j;
8+
return i - j;
9+
}
10+
};

0 commit comments

Comments
 (0)