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. 半夜思考,为什么 String 具有不变性

    学习 Java 的数据类型时,特殊关照了 String 类型,具有不变性. 当时没有深入思考为什么 String 有不变性.而且我只碰到了 String 才有不变性.Java这样设计的缘由何在. 突然 ...

  2. win xp安装

    Windows XP with sp3 VOL 微软原版下载: zh-hans_windows_xp_professional_with_service_pack_3_x86_cd_vl_x14-74 ...

  3. MT【152】不患寡而患不均

    ((清华2017.4.29标准学术能力测试1) $a_1,a_2,\cdots,a_9$ 是数字$1$到$9$ 的一个排列,则 $a_1a_2a_3+a_4a_5a_6+a_7a_8a_9$ 的最小值 ...

  4. 【刷题】UOJ #274 【清华集训2016】温暖会指引我们前行

    寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 "冻死宝宝了!" 这时 远处的天边出现了一位火焰之神 "我将赐予你们温暖和希望! ...

  5. 用Python实现的数据结构与算法:快速排序

    一.概述 快速排序(quick sort)是一种分治排序算法.该算法首先 选取 一个划分元素(partition element,有时又称为pivot):接着重排列表将其 划分 为三个部分:left( ...

  6. Android 65535 问题与 MultiDex分包

    Android Multidex 遇到的问题 http://blog.csdn.net/wangbaochu/article/details/51178881 Android 使用android-su ...

  7. android studio 怎么做屏幕适配?

    一.关于布局适配建议1.不要使用绝对布局2.尽量使用match_parent 而不是fill_parent .3.能够使用权重的地方尽量使用权重(android:layout_weight)4.如果是 ...

  8. 解题:TJOI 2015 组合数学

    题面 通过这个题理解了一下反链的概念,更新在图论知识点里了 每个点向右和下连边可以建出一张图,这个题事实上是让我们求图的最小链覆盖.Dilworth定理告诉我们,最小链覆盖等于最长反链(反链:DAG中 ...

  9. CF17E Palisection——优秀的综合计数题

    题意翻译 给定一个长度为n的小写字母串.问你有多少对相交的回文子 串(包含也算相交) . 输入格式 第一行是字符串长度n(1<=n<=2*10^6),第二行字符串 输出格式 相交的回文子串 ...

  10. 使用nginx的ngx_upstream_jdomain模块实现k8s容器的负载均衡

    使用背景最近一直在准备k8s上线事宜,目前已经在测试环境中全面部署并通过压力测试环境检验.离正式上线基本只剩下时间问题.我们目前测试环境中的容器负载均衡大量使用到了nginx,就是借助了ngx_ups ...