Skip to content

Commit 0f79e2c

Browse files
committed
update Java code
1 parent a8e1909 commit 0f79e2c

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

zh-hans/string/length_of_last_word.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,23 @@ public class Solution {
117117
public int lengthOfLastWord(String s) {
118118
if (s == null || s.isEmpty()) return 0;
119119

120-
int cnt = 0;
120+
int len = 0;
121121
for (int i = s.length() - 1; i >= 0; i--) {
122122
if (s.charAt(i) == ' ') {
123-
if (cnt > 0) break;
123+
if (len > 0) return len;
124124
} else {
125-
cnt++;
125+
len++;
126126
}
127127
}
128128

129-
return cnt;
129+
return len;
130130
}
131131
}
132132
```
133133

134134
### 源码分析
135135

136-
注意检查输入参数和索引即可。
136+
注意检查输入参数和索引即可,当前长度信息和当前索引字符是否为空格这两种信息可以结合使用避免硬标记
137137

138138
### 复杂度分析
139139

0 commit comments

Comments
 (0)