【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 ...
随机推荐
- Luogu P1273 有线电视网 树形DP
又重构了一下...当然当初的题一看就看懂了QAQ 设f[i][j]表示以i为根的子树,有j个客户的最大收益 方程:f[u][j+k]=max(f[u][j+k],f[u][j]+f[v][k]-w(u ...
- 114th LeetCode Weekly Contest Array of Doubled Pairs
Given an array of integers A with even length, return true if and only if it is possible to reorder ...
- ACM自己之前寒假的基础总结
1.const double pi = acos(-1.0); acos:反余弦函数,需要#include<math.h>函数库,acos(-1.0)的意思就是求π的值 2.算法竞赛中,不 ...
- self_vue@1.0.0 dev: `webpack-dev-server --inline --progress --config build/webpack.dev.conf.js`
解决方案: 1.试一下 卸载npm uninstall webpack-dev-server,在安装这个npm i webpack-dev-server@2.9.7 2.删除node_modules目 ...
- ndoejs解析req,伪造http请求
require("./m3m4") var http = require('http'); var server = http.createServer(); server.lis ...
- Python爬虫常用之HtmlParser
HtmlParser,顾名思义,是解析Html的一个工具.python自带的. 一.常用属性和方法介绍 HtmlParser是一个类,在使用时一般继承它然后重载它的方法,来达到解析出需要的数据的目的. ...
- Android so 库按需打包
Fresco 大部分的代码是由Java写的,但是里面也有很多C++的代码.C++代码必须根据Android 设备的CPU类型(通常称为”ABIs”)进行编译.目前Fresco支持五种 ABI: arm ...
- PIE SDK矢量数据简单渲染
1. 功能简介 PIE SDK对数据的符号化分为矢量数据渲染和栅格数据渲染两大类. 目前PIE SDK中针对矢量数据渲染包括简单渲染.唯一值渲染.分级渲染.自定义渲染:针对栅格数据渲染包括拉伸渲染.R ...
- PIE SDK影像格式转换
1.算法功能简介 影像格式转换可以实现通用栅格数据格式之间的自由转换.可自由转换的格式包括: GeoTIFF. ERDAS img. ENVI img. PIE支持算法功能的执行,下面对影像格式转 ...
- Oracle 查询单挑语句运行时间
pl/sql 不考虑网络IO等影响 declare guidname ):='255fc3db-815a-46c1-9187-ec0256305335'; timespa timestamp:=CUR ...