Length of Last Word | Leetcode
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
.
两个下标,head搜索非空格元素,last搜索head后的第一个空格或者字符串结尾。检测到单词后保存至lastlen里。
- class Solution {
- public:
- int lengthOfLastWord(string s) {
- const unsigned int len = s.size();
- unsigned int head,last,lastlen;
- head = ;
- last = ;
- lastlen = ;
- if (!len)
- ;
- ) {
- // search for the first non-space character
- while ((s[head] == ' ') && (head != len))
- head ++;
- if (head == len)
- return lastlen;
- last = head;
- while ((last != len) && (s[last] != ' '))
- last ++;
- if (last == len)
- return last - head;
- else {
- lastlen = last - head;
- head = last;
- }
- }
- }
- };
Length of Last Word | Leetcode的更多相关文章
- [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
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- 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
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
- [leetcode]Length of Last Word @ Python
原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...
- Leetcode(58)题解:Length of Last Word
https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【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】58. Length of Last Word 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...
随机推荐
- java08 Set
Set: 无序不可重复,重复则覆盖,判断是不是重复也是通过equals方法判断的.HashSet和TreeSet,HashSet底层是HashMap. public static void main( ...
- android学习资料
在线查看android源码 1. https://github.com/android 2. http://grepcode.com/project/repository.grepcode.com ...
- Activity 与ListActivity的区别
转载自 http://www.cnblogs.com/bravestarrhu/archive/2012/05/06/2486703.html
- [转]Form Builder:app_field.clear_dependent_fields和APP_FIELD.set_dependent_field的用法
转自:http://www.cnblogs.com/toowang/p/3668070.html 可以调用APP_FIELD.clear_dependent_fields和APP_FIELD.set_ ...
- mysql数据库时间、字符串类型互转
时间格式转换: select DATE_FORMAT(NOW(),"%y-%m-%d %H:%i:%s") 字符串转时间: select STR_TO_DATE("201 ...
- C++ 全排列函数 nyoj 366
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
- com.android.builder.packaging.DuplicateFile
解决方法: packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' ...
- c语言学习之基础知识点介绍(十九):内存操作函数
一.malloc函数 /* 首先需要导入头文件 #include <stdlib.h> malloc void* malloc(n); n是字节大小 开辟堆空间,开辟的字节数以n为准 返回 ...
- ThinkPad E40无线网卡驱动安装 FOR CENTOS6.3
1.看一下咱们用的本本的无线是咋子无线网卡,如下: [root@liaohg Downloads]# lspci | grep Wireless 03:00.0 Network controller: ...
- 关于使用用友华表Cell控件按需打印行的方法
分享下只需一个cll文件按需打印行的觉得最好的方式:1.cell文件要打印行的地方最好不要全删了,留一行,设置好单元格样式(字体.对齐方式.折行自适应等),后面会省一些代码: 2.使用CopyRang ...