We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a8e1909 commit 0f79e2cCopy full SHA for 0f79e2c
zh-hans/string/length_of_last_word.md
@@ -117,23 +117,23 @@ public class Solution {
117
public int lengthOfLastWord(String s) {
118
if (s == null || s.isEmpty()) return 0;
119
120
- int cnt = 0;
+ int len = 0;
121
for (int i = s.length() - 1; i >= 0; i--) {
122
if (s.charAt(i) == ' ') {
123
- if (cnt > 0) break;
+ if (len > 0) return len;
124
} else {
125
- cnt++;
+ len++;
126
}
127
128
129
- return cnt;
+ return len;
130
131
132
```
133
134
### 源码分析
135
136
-注意检查输入参数和索引即可。
+注意检查输入参数和索引即可,当前长度信息和当前索引字符是否为空格这两种信息可以结合使用避免硬标记。
137
138
### 复杂度分析
139
0 commit comments