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的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【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 ...

  4. 【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 ' ' ...

  5. 【LeetCode每天一题】Length of Last Word(字符串中最后一个单词的长度)

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  6. 【leetcode刷题笔记】Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  7. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  8. 【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 ...

  9. 【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 ...

  10. 【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 ...

随机推荐

  1. mysql 命令连接

    远程登陆MySQL,同时指定对应的端口和ip. 假设远程的ip为:10.154.0.43 端口为:1341 输入如下命令: mysql -h 10.154.0.43 -P 1341 -u root - ...

  2. adb自救指南

    以下只能自救专用 adb.exe root adb remount adb.exe connect <Ip> adb.exe install [-r] [-d] <apkPath&g ...

  3. scraoy之日志等级处理

    一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. - 日志信息的种类: ERROR : 一般错误 ...

  4. java se系列(四) 函数、数组、排序算法、二分法、二维数组

    1 函数 1.1  数的概述 发现不断进行加法运算,为了提高代码的复用性,就把该功能独立封装成一段独立的小程序,当下次需要执行加法运算的时候,就可以直接调用这个段小程序即可,那么这种封装形形式的具体表 ...

  5. Lucene常用类

    1.1 IndexWriter: 充当  创造/在索引过程中更新指标的   核心组成部分 1.2 Lucene目录 Directory: 索引的存储位置: 通常是文件的列表: 这些文件被称为索引文件. ...

  6. 转载收藏(js数组方法大全)

    js数组方法大全 JavaScript中创建数组有两种方式 (一)使用 Array 构造函数: var arr1 = new Array(); //创建一个空数组var arr2 = new Arra ...

  7. oracle 单实例DG(配置篇二)

    一,DG搭建实例--主库配置 one : 归档配置 01,查看归档  1 select log_mode,force_logging from v$database; 02,开启归档 关闭数据库重新启 ...

  8. MySQL搭建Amoeba_读写分离

    一.背景知识 Amoeba(变形虫)项目,专注 分布式数据库 proxy 开发.座落与Client.DB Server(s)之间.对客户端透明.具有负载均衡.高可用性.sql过滤.读写分离.可路由相关 ...

  9. pat1011. World Cup Betting (20)

    1011. World Cup Betting (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Wit ...

  10. 诠释JavaScript中的this

    文章首发:http://www.cnblogs.com/sprying/p/3573456.html 使用this的几种场合 1. 执行函数时,判断函数是对象方法还是一个单独的函数?单独的函数this ...