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 ...
随机推荐
- MacOS & dock 工具栏 & 外接显示器 & 主屏
MacOS & dock 工具栏 & 外接显示器 & 主屏 macos 如何将 dock工具栏从外接显示器拖回主屏 https://support.apple.com/zh-c ...
- 【Python】Python简介
Python是一种既使用简单又功能强大的高级编程语言,同时支持面向过程的编程和面向对象的编程. 官方对python的介绍:Python 是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简 ...
- DPDK环境搭建
一.环境要求 1.内核版本 >= 2.6.34: 2.glibc版本 >= 2.7 (ldd --version 查看glibc版本): 3.gcc版本 >= 4.9,一些gc ...
- JVM中各种变量保存位置
Java中变量分为静态变量,实例变量,临时变量.那么各种变量具体保存在JVM中的何处呢? 1 静态变量:位于方法区. 2 实例变量:作为对象的一部分,保存在堆中. 3 临时变量:保存于栈中,栈随线程的 ...
- thread 学习
#include <thread> #include <cstdio> #include <utility> #include <iostream> v ...
- 使用msbuild.exe绕过应用程序白名单(多种方法)
一.MSbuild.exe简介 Microsoft Build Engine是一个用于构建应用程序的平台.此引擎也被称为msbuild,它为项目文件提供一个XML模式,该模式控制构建平台如何处理和 ...
- 如何修改Windows程序的权限?
修改程序的权限需要用到3个函数: 1. 获取进程的令牌句柄: OpenProcessToken 2. 查找特权类型的ID: LookupPrivilegeValue 3. 修改进程的特权:Adjust ...
- python小专题——urllib2模块
Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节描述的并不清楚,比如 urllib2 这个 HTTP 客户端库.这里总结了一些 urllib2 库的使用细节. 1 P ...
- unity生成Android apk
前提:本文默认你安装了unity5.6版本,不是这个版本的没有Gradle(new)选项,也默认你安装了Android Studio并配置好了环境变量. Gradle(new):打包Android S ...
- RF parameter
There are primarily 3 features which can be tuned to improve the predictive power of the model : 说明: ...