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 ...
随机推荐
- jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值
—.获取dropdownlist的text(ddlList为服务器端dropdownlist的ID,生成name属性等于ddlList的select标签) $("#ddlList optio ...
- MVC过滤大法(过滤静态文件)
参考文章:https://prerakkaushik.wordpress.com/2014/02/12/routing-request-for-static-files-with-or-without ...
- jquery对象访问
jquery对象访问 方法名 说明 语法 (callback 执行的函数,object指定元素的对象.) each() 用于以当前jQuery对象匹配到的每个元素作为上下文来遍历执行指定的函数 jQu ...
- OpenStack虚机状态变化图解
对官网上内容的一个翻译,方便自己以后查找资料用 The following diagrams and tables show the required virtual machine (VM) sta ...
- 如何使用ros命令行显示图片
rosrun image_view image_view image:=[TOPIC] 注意:每次只能显示一个UI.不能在一条命令中订阅多个节点.
- 浅谈UML——九种图(一)
前言 学UML将近两个星期了,对UML有了一定的了解,学过的没学过的都知道UML中最最最核心的部分要数那九个图了.浅谈UML九种图. 实例 1.用例图: 什么是用例?描绘一个系统外在可见的需求情况,是 ...
- cf785D(组合数学)
题目链接: http://codeforces.com/problemset/problem/785/D 题意: 左边全为 '(' 右边全为 ')' 且两者数量想等的字符串称为 RSBS. 给出一个由 ...
- [Xcode 实际操作]四、常用控件-(18)MKMapView地图,将地理坐标转换为实际地名
目录:[Swift]Xcode实际操作 本文将演示将地理坐标转换为实际地名. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先往视 ...
- Python中list作为默认参数的陷阱
在Python中,作为默认参数的一定要是不可变对象,如果是可变对象,就会出现问题,稍不注意,就会调入陷阱,尤其是初学者,比如我(┬_┬). 我们来看一个例子. def add(L=[]): L.app ...
- Maven下把父项目下的子项目导出到myeclipse中
第一种在父项目下已有子项目:右击空白------import 第二步Maven4MyEclipse-----------Existing Maven Projects 第三部选择父项目下面的子项目 ...