【Leetcode】【Easy】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.
解题思路1:
遍历字符串,设置整形变量n,记录当前遍历到的无空格子串的长度,如果子串后遇到空格,且空格后再次遇到新的子串,则更新n,否则返回n;
注意:
本题容易忽略的是,返回最后一个子串的长度,不能简单想做遇到空格就重新计数,因为可能空格后不再有子串了,也就是最后一个子串结束后,字符串没有完,还有空格存在。因此要注意对这种情况进行判断。
class Solution {
public:
int lengthOfLastWord(string s) {
int len = s.size();
int len_lw = ;
for (int i = ; i < len; ++i) {
if (i != && s[i-] == ' ' && s[i] != ' ')
len_lw = ;
if (s[i] != ' ')
len_lw++;
}
return len_lw;
}
};
解题思路2:
直接从后遍历字符串,先过滤尾部的空格,之后遍历的第一个子串长度就是要返回的结果。
class Solution {
public:
int lengthOfLastWord(const char *s) {
int len = strlen(s);
int sum = ;
while (s[len-] == ' ')
len--;
for (int i=len-; i>=; i--) {
if(s[i]!=' ')
sum++;
else
break;
}
return sum;
}
};
附录:
1、C语言中的char* char const char 和C++ string的关系
2、C++中对字符串操作的函数总结
3、灵活运用字符串指针,多用指针操作,少用数组操作:
class Solution {
public:
int lengthOfLastWord(const char* s) {
int len = ;
while (*s) {
if (*s++ != ' ')
++len;
else if (*s && *s != ' ')
len = ;
}
return len;
}
};
【Leetcode】【Easy】Length of Last Word的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode算法-58/66】Length of Last Word/Plus One
LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
随机推荐
- HDU - 1223 DP 分类
据说这个是经典问题 \(dp[i][j]=dp[i-1][j-1]*j+dp[i-1][j]*j\) \(dp[i][j]\)表示前i个数分为j个集合,[i-1][j-1]为插入小于号[i-1][j] ...
- Git的深入理解与GitHub托管服务的使用
[转自] http://www.cnblogs.com/cocowool/archive/2012/02/17/2356125.html 源代码管理系统(SCM)与版本控制 版本控制是一种记录若干文件 ...
- 使用Junit进行自动单元测试
软件工程第二次作业 选择开发工具 使用Eclipse进行java程序编写:安装过程如图: 练习自动单元测试技术 参考资料:[Junit入门使用教程][https://www.cnblogs.com/y ...
- 使用jenkins自动化构建android和ios应用
背景 随着业务需求的演进,工程的复杂度会逐渐增加,自动化的践行日益强烈.事实上,工程的自动化一直是我们努力的目标,能有效提高我们的生产效率,最大化减少人为出错的概率,实现一些复杂的业务需求应变.场景如 ...
- Ace教你一步一步做Android新闻客户端(五) 优化Listview
今天写存货了 调试一些动画参数花了些时间 ,嘿嘿存货不多了就没法做教程了,今天来教大家优化listview,等下我把代码编辑下 这次代码有些多 所以我把条理给大家理清楚.思路就是把加载图片的权利交给O ...
- 如何去除表单元素获得焦点时的外边框:outline (轮廓)
我们在做制作表单页面时,经常会需要消除表单元素带来的边框,这时候我们需要用到两个属性: 1.表单元素未激活状态下的边框,不实现边框: border:none; 2.表单元素获得焦点时的轮廓,隐藏轮廓: ...
- Caffe & Caffe2入门博客存档
caffe2 教程入门(python版) https://www.jianshu.com/p/5c0fd1c9fef9?from=timeline caffe入门学习 https://blog.csd ...
- linux服务器git pull/push时避免频繁输入账号密码
1.先cd到根目录,执行git config --global credential.helper store命令 [root@iZ25mi9h7ayZ ~]# git config --global ...
- (转载)C#获取当前应用程序所在路径及环境变量
一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...
- 网页生命周期-PageLoad事件
PageLoad事件的作用 l 页面载入是执行的处理命令 l 可以动态创建控件 l 可以设置现有控件的属性和状态 l 常用来设置数据库的连接 l 每次页面载入都会执行