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. 半夜思考之查漏补缺, Spring 中的容器后处理器

    之前学 Spring 的时候 , 还没听过容器后处理器 , 但是一旦写出来 , 就会觉得似曾相识 . 容器配置器通常用于对 Spring 容器进行处理 , 并且总是在容器实例化任何其他 Bean 之前 ...

  2. 基本数据类型用== 比较数值 引用类型用==比较的是jvm中的地址 比较数值用equal

    基本数据类型用== 比较数值     引用类型用==比较的是jvm中的地址 比较数值用equal

  3. 【开发工具IDE】eclipse的SVN提交忽略target等多余文件

    这个build失败的解决方案就是不要把你项目的 target目录放在src repository 里面,还有 .project 和 .classpath最好也别放到src repository 里. ...

  4. AtCoder Grand Contest 004

    AtCoder Grand Contest 004 A - Divide a Cuboid 翻译 给定一个\(A*B*C\)的立方体,现在要把它分成两个立方体,求出他们的最小体积差. 题解 如果有一条 ...

  5. 【BZOJ4205】卡牌配对

    Description 现在有一种卡牌游戏,每张卡牌上有三个属性值:A,B,C.把卡牌分为X,Y两类,分别有n1,n2张. 两张卡牌能够配对,当且仅当,存在至多一项属性值使得两张卡牌该项属性值互质,且 ...

  6. 解题:POI 2007 Driving Exam

    题面 有点意思的题 从一个位置$i$出发可以到达每一个位置即是从$1,n$出发可以到达$i$.然后有了一个做法:把图上下反转后建反图,这样就可以求从一个点$i$到达左右两侧的花费$dp[i][0/1] ...

  7. SDOI 2019 Round1 游记

    \(SDOI~2019 ~ Round1\) 游记 \(Day ~0\) 报道.骑车子去的,好热.到了之后看到好几个同校神仙,还从那里莫名其妙的等了一会,然后交了钱签了名就拿挂牌走人了.现在居然还有受 ...

  8. CentOS 6.6搭建LNMP环境

    一.安装前 1.关闭linux的安全机制 vim /etc/selinux/config SELINUX=enforcing  改为  SELINUX=disabled 2.关闭iptables防火墙 ...

  9. 关于表单中Readonly和Disabled

    Readonly和Disabled是用在表单中的两个属性,它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,总结如下: Readonly只针对input(text / pass ...

  10. 线性判别分析 LDA

    点到判决面的距离 点\(x_0\)到决策面\(g(x)= w^Tx+w_0\)的距离:\(r={g(x)\over \|w\|}\) 广义线性判别函数 因任何非线性函数都可以通过级数展开转化为多项式函 ...