LeetCode58. 最后一个单词的长度
牛客网标记为华为笔试题
其实比较简单
稍微记录下就好
package com.java.huawei;
import java.util.Scanner;
/**
* @author dengtiantian
*/
public class Solution58{
/**
* @author dengtiantian
*/
static class Solution {
public int lengthOfLastWord(String s) {
s = s.trim();
int temp =0;
for(int i = s.length()-1;i >=0;i--){
if(s.charAt(i) ==' ') {
return temp;
}
temp++;
}
return s.length();
}
}
public static void main(String[] args) {
Solution solution = new Solution();
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(solution.lengthOfLastWord(str));
}
}