力扣题-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;
}
};