[LeetCode] Length of Last Word
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
.
这题看似很简单,本来想睡前切一道怡情,结果切到一点钟,东改西改还是没AC....
因为edge case 很多,考虑一不周全就是错的,比如 "", " ", " a", " a", "a b " 这几个case。
最后我AC的思路是用 i 来记录当前的词长,如果遇到空格则说明此词完毕,把 j = i; i = 0 然后继续直到串尾巴'\0'
最后判断 i 是否为0,如果是则返回j,否则返回i
int lengthOfLastWord(const char *s) {
int i=, j=;
while (*s != '\0') {
if (*s == ' ') {
if (i != ){
j = i;
i = ;
}
}
else {
i++;
}
s++;
}
if (i == ) return j;
return i;
}
囧
[LeetCode] Length of Last Word的更多相关文章
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- LeetCode——Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode]Length of Last Word @ Python
原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...
- Leetcode: Length of Last Word in python
Length of Last Word Total Accepted: 47690 Total Submissions: 168587 Given a string s consists of ...
- [Leetcode] Length of last word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- [LeetCode] Length of Last Word 字符串查找
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- LeetCode Length of Last Word 最后一个字的长度
class Solution { public: int lengthOfLastWord(const char *s) { ; string snew=s; ,len=strlen(s); ]; ) ...
- leetcode 题解: Length of Last Word
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- 【一天一道LeetCode】#58. Length of Last Word
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
随机推荐
- 前端之js的常用用法
js生成标签 // 将标签添加到i1里面 var tag = document.createElement('input'); tag.setAttribute('type', 'text'); ta ...
- ajax给全局变量赋值问题解决
$.ajax({ type: "post", //以post方式与后台沟通 url: "./php/chartAjax.php", //与此php页面沟通 da ...
- spring ext 跨域
read方法中调用的response对象是父类BaseController的一个成员变量. spring默认bean的生命周期Score为singleton单例模式. 当多线程并发使用同一bean, ...
- PHP检测每一段代码执行时间
<?php // 实例1 /** * @start time */ function proStartTime() { global $startTime; $mtime1 = explode( ...
- php foreach循环中unset后续的键值问题
实例: $arr=array('a','b','c','d','e','f'); foreach($arr as $index=>$tmp){ echo $index.'=>'.$tmp. ...
- Java Dao设计模式
一.信息系统的开发架构 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层就是客户端,简单的来说就是浏览器. 2.显示层:JSP/S ...
- C#之串口
1.字符发送 string strSend = "00 01 02 03"; serialPort1.Write(strSend); 2.字符接收 ReadDataFromSeri ...
- POJ 3414
http://poj.org/problem?id=3414 这是一个广搜的题目,不难,就是有些许麻烦.对于练习还是个不错的题目. 题意就是给你两个杯子,这两个杯子的容量分别为a和b,要你通过一些操作 ...
- centos7时间同步和时区设置
centos7时间同步和时区设置 安装ntp服务的软件包 sudo yum install ntp 将ntp服务设置为缺省启动 systemctl enable ntpd 修改启动参数,增加-g -x ...
- Zookeeper WINDOWS 安装配置
下载:zookeeper:http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz 解压zo ...