问题的描述:
python代码:
class Solution(object):def lengthOfLastWord(self, s):""":type s: str:rtype: int"""s = s.strip()words = s.split(" ")return len(words[-1])
java1:
class Solution {public int lengthOfLastWord(String s) {s = s.trim();String[] words = s.split(" ");return words[words.length - 1].length();}
}
java2:
public class Solution {public int lengthOfLastWord(String s) {int length = 0;int n = s.length();int i = n - 1;while (i >= 0 && s.charAt(i) == ' ') {i--;}while (i >= 0 && s.charAt(i) != ' ') {length++;i--;}return length;}
}