Leetcode Length of Last Word (java)
来源:互联网 发布:mysql 存储过程 性能 编辑:程序博客网 时间:2024/06/17 22:10
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
.
solution 1:
public class Solution { public int lengthOfLastWord(String s) { if(s == null || s.length() ==0) return 0; int result = 0; int length = s.length(); boolean flag = false; for(int i=length-1; i>=0; i--) { char c = s.charAt(i); if((c>='A' && c<='Z') ||( c>='a' && c<='z')) { flag = true; result++; }else { if(flag) { return result; } } } return result; }}
soulution 2:
public class Solution { public int lengthOfLastWord(String s) { if(s == null || s.length() ==0 || s.trim().equals("")) return 0; String[] strs = s.split(" "); String str = strs[strs.length-1]; return str.length(); }}
0 0
- [Leetcode] Length of Last Word (Java)
- leetcode Length of Last Word java实现
- [leetcode]Length of Last Word [java]
- 【Leetcode】Length of Last Word JAVA
- 【leetcode Java】Length of Last Word
- Leetcode Length of Last Word (java)
- [LeetCode][Java] Length of Last Word
- Length of last word leetcode java
- LeetCode 58 : Length of Last Word (Java)
- leetcode:Length of Last Word 【Java】
- leetcode-java-58. Length of Last Word
- LeetCode 58. Length of Last Word Java
- (Java)LeetCode-58. Length of Last Word
- [LeetCode] 58. Length of Last Word java
- [leetcode]58. Length of Last Word@Java
- LeetCode: Length of Last Word
- LeetCode Length of Last Word
- LeetCode : Length of Last Word
- 深入理解 Session 与 Cookie
- nyoj-14会场安排问题[dp]
- Unity shader随笔记(一)SurfaceShader
- 旋转数组的最小数字
- 李晨和张馨予,谁是最大的输家?
- Leetcode Length of Last Word (java)
- HYSBZ 1146 网络管理Network Treap+线段树+树链
- 关于serialVersionUID的解释
- XTU 1183 Factors
- Math.random 重复随机,不重复随机
- centos7安装java1.8
- 发牌程序(一)
- 发牌程序(二)
- Windows线程池