力扣题-11.20
力扣题1:58. 最后一个单词的长度
解题思想:按空格划分,然后统计单词长度即可

class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
word_list = s.split()
return(len(word_list[-1]))
class Solution {
public:
int lengthOfLastWord(string s) {
int result = 0;
int temp = 0;
for(int i=0;i<s.size();i++){
if(s[i]!=' '){
temp +=1;
}
else{
if(temp!=0){
result = temp;
}
temp = 0;
}
}
if(temp!=0){
result = temp;
}
return result;
}
};
本文介绍了如何使用Python和C++解决LeetCode上58.最后一个单词的长度问题,通过按空格分割字符串并统计最后一个单词的长度。提供了PythonSolution类和C++代码实现。
4920

被折叠的 条评论
为什么被折叠?



