Length of Last Word
来源:互联网 发布:亨利常数 知乎 编辑:程序博客网 时间:2024/05/02 19:20
java String api很强大O(∩_∩)O~
public class Solution { public int lengthOfLastWord(String s) { if(s == null){ return 0; } String[] arrayStrings = s.split(" "); if(arrayStrings.length == 0){ return 0; } return arrayStrings[arrayStrings.length - 1].length(); }}
分析,注意s=" a b "这种情况,从后遍历单词,遇到空格退出
class Solution {public: int lengthOfLastWord(const char *s) { int len=strlen(s); int sum=0; while(s[len-1]==' ') len--; for(int i=len-1;i>=0;i--) { if(s[i]!=' ') sum++; else break; } return sum; }};
0 0
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Java+Servlet投票系统
- IAR 工程名修改
- 【深入JAVA】Java中文件操作
- JFreechart学习
- 合并两个排序的链表
- Length of Last Word
- 【4.15】求二进制数中1的个数【每日一题】
- 社説 20150416 大学の国旗国歌 要請で自治が脅かされるのか
- c++模板类学习
- UIView转场动画接口
- 高斯、中值、均值、双边滤波的效果
- django中view配置的几种方式
- HEVC插值滤波器
- linux平台下防火墙iptables原理