【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 ...
随机推荐
- POJ 2182 Lost Cows (求序列第k大)
题解 二分+树状数组 显然最和一个数的值就是rank 那么其它数有什么规律? 从后往前匹配rank,我们可以发现第i个数的rank为还没有匹配的rank第(a[i]+1)大的数 这可以用 树状数组+二 ...
- python-is,==
在讲is和==这两种运算符区别之前,首先要知道Python中对象包含的三个基本要素,分别是:id(身份标识).python type()(数据类型)和value(值).is和==都是对对象进行比较判断 ...
- Tensorlflow-神经网络解决非线性回归问题
import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt #使用numpy生成200个随机点,范围从-0.5到0 ...
- Jquery 循环内间隔执行 异步执行
<script type="text/javascript"> var arr = new Array(); var arrk = 0; jQuery(function ...
- PIE SDK图层树右键菜单与命令绑定
1. 功能简介 上一节已经介绍过图层树如何和地图和制图关联,图层树右键菜单主要是基于TocControl控件进行对菜单节点进行控制,TocControl主要作用是显示当前加载的图层有哪些.采用什么 ...
- mysql大数据表删除操作锁表,导致其他线程等待锁超时(Lock wait timeout exceeded; try restarting transaction;)
背景: 1.有一个定时任务,每10分钟入一批统计数据: 2.另一个定时任务,每天定时清理7天前数据,此定时任务每天01:18:00执行: 现象: 每天01:20:00的统计数据入库失败,异常信息如下, ...
- cotex_A7/A9:
A7与A9的比较:A7计算性能(DMIPS)不如A9(编号越大计算能力越强),但A7功耗更先进,作为A15的协处理器.A8是单核.
- 利用paramiko的demo_simple.py进行日志记录时遇到的特殊字符
特殊字符列表: 回车 "\r" "\x13" 响铃 "\x07" 换行 "\n" "\x10" &q ...
- Oracle 客户端、服务器、数据库、数据库对象(表、视图等)的关系
1.数据库服务器 所谓数据库服务器,只是在机器上安装了一个数据库管理软件,这个软件可以管理多个数据库.一般开发人员会针对每一个应用创建一个数据库 2.单实例数据库模式下的数据库服务器.数据库.数据库实 ...
- dpkg: error: dpkg status database is locked by another process 解决方法
使用dpkg -i/apt命令安装,报错: ------------------------------------------------------------- dpkg: error: dpk ...