[leetcode] 18. 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
.
我的思路就是先把字符串反转,然后找到第一个' '后弹出,这个需要注意的大概就是很可能最后不是字符,而是一个标点,所以要判断一下。题解如下:
class Solution {
public:
int lengthOfLastWord(const char *s)
{
string tmp = s;
reverse(tmp.begin(), tmp.end()); int sum, i, len;
sum = i = 0;
len = tmp.length();
while (i < len)
{
if (isalpha(tmp[i]))
{
sum++;
i++;
break;
}
else
{
i++;
}
} while (tmp[i] != ' ' && i < len)
{
sum++;
i++;
} return sum;
}
};
如上,不难。
[leetcode] 18. Length of Last Word的更多相关文章
- 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 求末尾单词的长度
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 ...
- Java for LeetCode 058 Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- LeetCode(48)-Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- [leetcode]58. 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 ' ', retu ...
随机推荐
- LocalDateTime json格式化
参考https://www.cnblogs.com/xiaozhang9/p/jackson.html?utm_source=itdadao&utm_medium=referral <d ...
- java web 常用正则
什么是 RegExp? RegExp 是正则表达式(Regular expression)的缩写,作用是对字符串执行模式匹配. 通常用于格式验证.正则替换.查找子串等 各种编程语言的正则表达式基本相同 ...
- 在IE10下,DropDownList的AutoPostBack不能触发
Default.aspx 文件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...
- OC 线程操作3 - NSOperation 实现线程间通信
#import "ViewController.h" @interface ViewController () /** 图片 */ @property (weak, nonatom ...
- oracle基本查询入门(一)
一.基本select语句 SELECT *|{[DISTINCT] column|expression [alias], ...} FROM table; 例如: --查询所有数据 select * ...
- jquery正则判断字符串有几个逗号
var angelweb="我,你,ta,";var re=/[,,]/g;if(re.test(angelweb)){ var n=angelweb.match(re).leng ...
- sql优化常用命令总结
1.显示执行计划的详细步骤 SET SHOWPLAN_ALL ON; SET SHOWPLAN_ALL OFF; 2. 显示执行语句的IO成本,时间成本 SET STATISTICS IO ON SE ...
- 获取url路径中的参数
简介 运用js的时候,我们有时可能会有这样的需求,就是想要获取浏览器地址栏指定的一项参数,形如:https://i.cnblogs.com/EditPosts.aspx?postid=8628413& ...
- python中的日志模块logging
1.日志级别5个: 警告Warning 一般信息Info 调试 Debug 错误Error 致命Critical 2.禁用日志方法 logging.disable(logging.DEBUG) 3 ...
- UNIX和类UNIX操作系统