LeetCode Length of Last Word 最后一个字的长度
class Solution {
public:
int lengthOfLastWord(const char *s) {
if(s=="") return ;
string snew=s;
int n=,len=strlen(s);
char *p=&snew[len-];
while(*p==' '&&len!=){
*p--;
len--;
}
while(*p!=' '&&len!=){
n++;
p--;
len--;
}
return n;
}
};
题意:给一个数组,以空格来判断是否字的结束与开始。一个词的前后都是空格,中间无空格。返回最后一个字的长度。
思路:给的是一个不可修改的字符串,为了方便,创建另外的指针。若最后是空格,先将后面的空格过滤,从后开始寻找第一个不是空格的字符,开始计数,继续往前数,直到有空格或者已经扫完字符串为止。
吐槽:感觉snew可以省略的,直接利用s就行。但是语法忘了~
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
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 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 58.Length of Last Word (最后单词的长度) 解题思路和方法
Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space charact ...
- leetcode 题解: Length of Last Word
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
随机推荐
- [CentOS7] gzip, bzip2, xz 压缩与解压缩
声明:本文主要总结自:鸟哥的Linux私房菜-第八章.檔案與檔案系統的壓縮,打包與備份,如有侵权,请通知博主 gzip命令: 选项参数: -c :将压缩后的数据显示到屏幕上,可以用于重定向: -d : ...
- Java实例练习——基于UDP协议的多客户端通信
昨天学习了UDP协议通信,然后就想着做一个基于UDP的多客户端通信(一对多),但是半天没做出来,今天早上在参考了很多代码以后,修改了自己的代码,然后运行成功,在这里分享以下代码,也说一下自己的认识误区 ...
- 浅谈UML——九种图(一)
前言 学UML将近两个星期了,对UML有了一定的了解,学过的没学过的都知道UML中最最最核心的部分要数那九个图了.浅谈UML九种图. 实例 1.用例图: 什么是用例?描绘一个系统外在可见的需求情况,是 ...
- poj2689(素数区间筛法模板)
题目链接: http://poj.org/problem?id=2689 题意: 给出一个区间 [l, r] 求其中相邻的距离最近和最远的素数对 . 其中 1 <= l < r < ...
- hdu3078(lca / RMQ在线)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3078 题意: 给出一棵 n 个点的带点权值的树, 接下来有 q 组形如 k, x, y 的输入, 若 ...
- DNS解析工具--nslookup和dig使用
1.nslookup使用 [root@master ~]# nslookup> server 8.8.8.8 #指定域名服务器Default server: 8.8.8.8Address: ...
- POJ1034 The dog task
题目来源:http://poj.org/problem?id=1034 题目大意: 一个猎人在遛狗.猎人的路径由一些给定的点指定.狗跟随着猎人,要与主人同时到达那些指定的点.在丛林里有一些有趣的地方, ...
- HTTP的学习记录3--HTTPS和HTTP
一开始我所知道的只有HTTPS比HTTP更加安全,而且很多网站,如百度谷歌之流已经都是HTTPS了,博客园也是,你可以看到上方我们链接上那个绿色的小锁和绿色的安全二字. 另外吐槽一句,绿色还真是有趣, ...
- 开元java开发工具
开元java开发工具 http://www.hutool.cn/
- GUI的最终选择 Tkinter(二):Label和Button组件
Label组件 Lable组件是用于界面上输出描述的标签,例如提示用户“您下载的电影含有未成年人限制内容,请满18岁以后点击观看!”,先来上结果图: 在来看下它的代码: from tkinter im ...