2016.6.19——Length of Last Word
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的更多相关文章
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【leetcode】Length of Last Word
题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- 【读书笔记】2016.11.19 北航 《GDG 谷歌开发者大会》整理
2016.11.19 周六,我们在 北航参加了<GDG 谷歌开发者大会>,在web专场,聆听了谷歌公司的与会专家的技术分享. 中午免费的午餐,下午精美的下午茶,还有精湛的技术,都是我们队谷 ...
- 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 ...
- 学习图像算法阶段性总结 (附一键修图Demo) 2016.04.19更新demo
今天特别感慨,自己从决定研究图像处理,势必要做出一键修图算法. 经历了,三个多月的书籍积累,三个多月的算法调整以及优化. 人是一种奇怪的动物,当你做不到的时候,你以为做到了,自己会感觉很爽,很有成就感 ...
- [LeetCode] Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [LintCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 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 ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- C++解析命令行参数(仿C语言args)
说好不造轮子的,就管不住这手 #include <cstdio> #include <string> #include <vector> bool ParseCha ...
- kafka-connect-hive Sink插件入门指南
kafka-connect-hive是基于kafka-connect平台实现的hive数据读取和写入插件,主要由source.sink两部分组成,source部分完成hive表数据的读取任务,kafk ...
- 最新wireshark抓包教程
http://jingyan.baidu.com/article/d71306350f213b13fdf475b9.html 大家都知道,sniffer是一款收费产品, 要真正的学会使用,因为有许多的 ...
- Mybatis笔记八:MyBatis中#{}和${}的区别
先给大家介绍下MyBatis中#{}和${}的区别,具体介绍如下: 1. $将传入的数据直接显示生成在sql中 2. #方式能够很大程度防止sql注入. 3.$方式无法防止Sql注入. 4.$方式一般 ...
- 【BZOJ4247】挂饰(动态规划)
[BZOJ4247]挂饰(动态规划) 题面 BZOJ 题解 设\(f[i][j]\)表示前\(i\)个物品中还剩下\(j\)个挂钩时的最大答案. 转移显然是一个\(01\)背包,要么不选:\(f[i] ...
- Linux内核分析实验五
一.给MenuOS增加time和time-asm命令 1. 克隆并自动编译MenuOS rm menu -rf 强制删除原menu文件 git clone http: cd menumake root ...
- ---web模型 --mvc和模型--struts2 入门
关于web模型: 早期的web 应用主要是静态页丽的浏览〈如新闻的制监),随着Internet的发展,web应用也变得越来越复杂,不仅要 和数据库进行交互 ,还要和用户进行交互,由此衍生了各种服务器端 ...
- shell 中的流程控制关键字
if...else if [ $1x == "ab"x ]; then echo "you had enter ab" elif [ $1x == " ...
- shell参数代表什么,如何调试shell?
$0就是该bash文件名$?是上一指令的返回值$*所有位置参数的内容:就是调用调用本bash shell的参数.$@基本上与上面相同.只不过是“$*”返回的是一个字符串,字符串中存在多外空格.“$@” ...
- HOJ 13102 Super Shuttle (圆的反演变换)
HOJ 13102 Super Shuttle 链接:http://49.123.82.55/online/?action=problem&type=show&id=13102 题意: ...