leetcode--------------Length of Last Word
来源:互联网 发布:手机淘宝到货退款流程 编辑:程序博客网 时间:2024/06/05 08:48
Given a string s consists of upper/lower-case alphabets and empty space characters ' '
, return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s = "Hello World"
,
return 5
.
程序代码如下:
import java.util.Scanner;
public class Length_of_LastWord {
public static void main(String[] args) {
Scanner inputString = new Scanner(System.in);
String s = inputString.nextLine();
System.out.println(lengthOfLastWord(s));
}
public static int lengthOfLastWord(String s) {
int sLength = s.length() - 1;
int count = 0;
for(;sLength >= 0;sLength--){
if(s.charAt(sLength) != ' '){
break;
}
}
for(;sLength >= 0;sLength--){
if(s.charAt(sLength) != ' '){
count++;
}else
break;
}
return count;
}
}
- LeetCode: Length of Last Word
- LeetCode Length of Last Word
- LeetCode : Length of Last Word
- [Leetcode] Length of Last Word
- [LeetCode] Length of Last Word
- [LeetCode]Length of Last Word
- [leetcode]Length of Last Word
- Leetcode:Length of Last Word
- LeetCode-Length of Last Word
- [LeetCode] Length of Last Word
- [leetcode] Length of Last Word
- leetcode length of last word
- LeetCode - Length of Last Word
- leetcode:Length of Last Word
- LeetCode:Length of Last Word
- LeetCode | Length of Last Word
- LeetCode - Length of Last Word
- Leetcode: Length of Last Word
- scala实现设计模式之迭代器模式
- Java
- 1002. A+B for Polynomials (25)
- VB+DX游戏编程教程 第一话:初识DX
- Mysql 优化
- leetcode--------------Length of Last Word
- hibernate第一个例子,保存一个对象
- 位向量法排序
- LeetCode_DP_Unique Paths
- 一些优秀的实时开发社区
- 如何配置 MongoDB 副本集
- 条款02 尽量以const enum inline 替换#define
- 总结自己的Git常用命令
- GPU并行计算入门1——背景知识