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. 关于cin 与 cout 的加速

    在用cin 与 cout 的时候 可以使用 ios::sync_with_stdio(); cin.tie(); cout.tie(); 这样在输入大数据的时候可以加快许多

  2. 118th LeetCode Weekly Contest Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  3. UVALive - 3722 找规律

    题意:找规律 题解:找规律 结论是\(a^n(x-1)-\sum_{i=1}^{n-1}a^i \mod\ c\) #include<iostream> #include<algor ...

  4. vue 实现表单中password输入的显示与隐藏.

    实现效果: 点击 “眼睛” 的时候显示与隐藏 代码: <!DOCTYPE html> <html lang="en"> <head> <m ...

  5. 【Python】小括号过滤后的盲注

    0x00   环境搭建 sqli-labs第八关,简单修改下源代码,加入下面一行代码 $id=preg_replace('/\(|\)/', "",$id); //过滤小括号 0x ...

  6. JAVA WEB开发环境搭建

    JAVA WED开发环境搭建 JDK的安装和配置 到https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-21 ...

  7. vue 修饰符(转载)

    大佬写的很详细,直接转载过来,随时可以参考, 原博:https://www.w3cplus.com/vue/vue-methods-and-event-handling.html 事件处理 如果需要在 ...

  8. Oracle 的加减法函数

    原文:https://blog.csdn.net/chenghaibing2008/article/details/37873117 加法   select sysdate,add_months(sy ...

  9. git换行符问题

    from: http://www.cnblogs.com/flying_bat/archive/2013/09/16/3324769.html 一.AutoCRLF#提交时转换为LF,检出时转换为CR ...

  10. Column 'orders' in order clause is ambiguous

    今天报了这个错误 原因是.当使用sql查询语句,使用了join查表.但是这个orders没指定是哪张表的字段 ,发生在自关联情况