Leetcode题解(5):L58/Length of Last Word
L58: Length of Last Word
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.
解题思路:遇到非’ ‘字符,用一个符号表示word開始,遇到’ ‘表示word结束,注意边界情况处理
优化:能够从字符串末尾開始处理
class Solution {
public:
int lengthOfLastWord(string s) {
int strLen = s.length();
if(strLen == 0)
return 0;
int begin = 0;
int end = 0;
int pos = 0;
bool word_start = false;
while(pos < strLen)
{
if(s[pos] != ' ' && !word_start)
{
begin = pos;
word_start = true;
}
else if(s[pos] == ' ' && word_start)
{
end = pos;
word_start = false;
}
pos++;
}
if (word_start && pos == strLen)
end = strLen;
return end - begin;
}
};
Leetcode题解(5):L58/Length of Last Word的更多相关文章
- <LeetCode OJ> 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- LeetCode(59)Length of Last Word
题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return th ...
- LeetCode练题——58. Length of Last Word
1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...
- [LeetCode]题解(python):079 Word Search
题目来源 https://leetcode.com/problems/word-search/ Given a 2D board and a word, find if the word exists ...
- 【leetcode❤python】 58. Length of Last Word
#-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object): ...
- leetcode 题解: Length of Last Word
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- Leetcode(58)题解:Length of Last Word
https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- Firebug
Firebug是网页浏览器火狐下的一款开发类插件,它集HTML查看和编辑.JavaScript控制台.网络状态监视器于一体,是开发JavaScript.CSS.HTML和Ajax的得力助手.F12打开 ...
- FTP初始化文件.netrc使用技巧[转发]
FTP初始化文件.netrc使用技巧 FTP(文件传输)和E-mail(电子邮件).Telnet(远程登录)一样,是 Internet的三大主要功能之一.因为使用频繁,用户往往会遇到各种 各样的问题, ...
- SQL Server的安装笔记
SQL安装笔记 安装SQL Server 2008 打开SQL Server 2008中的setup.exe,显示SQL安装程序的对话框. 提示必须安装相关组件Microsoft.NET Framew ...
- 【译】x86程序员手册28-7.7任务地址空间
7.7 Task Address Space 任务地址空间 The LDT selector and PDBR fields of the TSS give software systems desi ...
- day14-二分法、匿名函数、内置函数以及面向过程编程
目录 二分法 匿名函数 内置函数 面向过程编程 二分法 二分法查找适用于数据量较大时,但是数据需要先排好顺序.主要思想是:(设查找的数组区间为array[low, high]) (1)确定该区间的中间 ...
- DeepCloneObjects 和 DeepClone
ARX AcDbDatabase 中的方法 deepCloneObjects() 和 wblock() 区别以及和 AcDbObject 方法 clone() 和 deepClone() 的关系 Ac ...
- MySQL:INSERT ... UPDATE
在 INSERT 语句末尾指定ON DUPLICATE KEY UPDATE时,如果插入的数据会导致表中的 UNIQUE 索引或 PRIMARY KEY 出现重复值,则会对导致重复的数据执行 UPDA ...
- vue中websoket的使用
首先安装npm install --save websocket-heartbeat-js@^1.0.7 在main.js中 引入并挂载全局方法 import WebsocketHeartbeat ...
- PHP--选择排序
<?php /** * 选择排序(从小到大)的思想:每一次从待排序的数据中选出最小的,放在待排序的起始位置. */ $arr = array(23, 42, 21, 8, 4, 2, 3, 1) ...
- block相关归纳
经过今天的Block的学习.上网查询相关文章归纳了一下 一.一个使用Block的好处有: Block可以用在许多不同的环境中,这样可以让代码更加简单,以及减少函数声明的数量,不用实现代理协议. 简单性 ...