Length of Last Word

本题收获:

1.str.size()为负

2.size_t引发的死循环

3.题目思路,有时在写代码时很不清楚边界条件的输出值是什么,若为面试,一定要问清楚。

  题目:

  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. " "  return 0

  2."a  "  return 1 (我开始错以为这种情况返回0 )

  思路:

    我的思路:直接找,for循环

    leetcode:直接找,while循环     

  正确代码:

 class Solution {
public:
int lengthOfLastWord(string s) {
int len = , tail = s.length() - ;
while (tail >= && s[tail] == ' ') tail--;
while (tail >= && s[tail] != ' ') {
len++;
tail--;
}
return len;
}
};

  我写的:

 class MyClass
{
public:
int lengthOfLastWord(string str)
{
if (str.size() == ) return ;
int n = str.size() - ;
int j = ; while (n >= && str[n] == ' ')
{
n--;
}
while (n >= && str[n] != ' ')
{
j++;
n--;
}
return j;
}
};

  出现死循环的代码:

  

 class MyClass
{
public:
int lengthOfLastWord(string str)
{
if (str.size() == ) return ;
int j = ;
int n = str.size() - ;
for (size_t i = n; i >= ; i--) //从size_t换到 int就不会出现i = 4294967295
{
cout << i << endl;
if (str[n] == ' ') return ;
else
{
j++;
continue;
} if (str[i] == ' ') break;
else j++;
}
return j;
}
};

  因为size_t是unsigned int/unsigned long 型 ,好吧 具体我也不太清楚是怎么样的!

  测试全代码:

  

 #include "stdafx.h"
#include "iostream"
#include "string"
using namespace std; class MyClass
{
public:
int lengthOfLastWord(string str)
{
if (str.size() == ) return ;
int n = str.size() - ;
int j = ; while (n >= && str[n] == ' ')
{
n--;
}
while (n >= && str[n] != ' ')
{
j++;
n--;
}
return j;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
string str;
str = "a";
MyClass solution;
int m = ;
m = solution.lengthOfLastWord(str);
cout << m << endl;
system("pause");
return ;
}

2016.6.19——Length of Last Word的更多相关文章

  1. [LeetCode] Length of Last Word 求末尾单词的长度

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

  2. 【leetcode】Length of Last Word

    题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  3. 【读书笔记】2016.11.19 北航 《GDG 谷歌开发者大会》整理

    2016.11.19 周六,我们在 北航参加了<GDG 谷歌开发者大会>,在web专场,聆听了谷歌公司的与会专家的技术分享. 中午免费的午餐,下午精美的下午茶,还有精湛的技术,都是我们队谷 ...

  4. U3D笔记11:47 2016/11/30-15:15 2016/12/19

    11:47 2016/11/30Before you can load a level you have to add it to the list of levels used in the gam ...

  5. 学习图像算法阶段性总结 (附一键修图Demo) 2016.04.19更新demo

    今天特别感慨,自己从决定研究图像处理,势必要做出一键修图算法. 经历了,三个多月的书籍积累,三个多月的算法调整以及优化. 人是一种奇怪的动物,当你做不到的时候,你以为做到了,自己会感觉很爽,很有成就感 ...

  6. [LeetCode] Length of Last Word

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

  7. [LintCode] Length of Last Word 求末尾单词的长度

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

  8. Java for LeetCode 058 Length of Last Word

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

  9. LeetCode 58. Length of Last Word

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

随机推荐

  1. docker中间件安装记录

    rabbitmq docker pull rabbitmq docker run --restart=on-failure: --name rabbitmq -d -p : -p : docker.i ...

  2. List、Set、Map

    List:1.可以允许重复的对象.  2.可以插入多个null元素. 3.是一个有序容器,保持了每个元素的插入顺序,输出的顺序就是插入的顺序. 4.常用的实现类有 ArrayList.LinkedLi ...

  3. springmvc接收date类型参数

    springmvc在表单提交接收date类型参数的时候会报错:Cannot convert value of type [java.lang.String] to required type [jav ...

  4. BZOJ5099 POI2018Pionek

    假设确定了最终所得向量的方向,则应该选择所有在该方向上投影为正的向量.按极角序排序后这显然是一段连续区间.最终向量方向很难枚举,但对于某个向量,在其上投影为正的向量与其夹角范围是(-π/2,π/2), ...

  5. 让你的wordpress在新窗口打开链接

    在使用wordpress过程中笔者发现还有一些不太完善的地方,没有充分考虑到用户体验.所以,在使用wordpress建博之初,我们有必要对wordpress进行一次小改造,让wordpress更个性. ...

  6. 每个 JavaScript 工程师都应懂的33个概念

    简介 这个项目是为了帮助开发者掌握 JavaScript 概念而创立的.它不是必备,但在未来学习(JavaScript)中,可以作为一篇指南. 本篇文章是参照 @leonardomso 创立,英文版项 ...

  7. BZOJ 1283: 序列

    1283: 序列 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 272  Solved: 151[Submit][Status][Discuss] D ...

  8. 一种KEIL中定义过的变量在使用中提示未定义的情况

    [环境] > KEIL5.25 > win10 > @2018-4-23 [问题] 头文件互包含导致的错误(使用了另一文件的类型定义) 文件<fileA.h> <f ...

  9. [POI2015]WIL-Wilcze doły

    题目描述 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. 输入格式: 第一行包含三个整数 ...

  10. BZOJ-3509 母函数+分块+暴力+FFT

    题目描述 给定一个长度为N的数组A[],求有多少对i, j, k(1<=i<j<k<=N)满足A[k]-A[j]=A[j]-A[i]. 输入格式 第一行一个整数N(N<= ...