【leetcode】58-LengthofLastWord
problem
只有一个字符的情况;
最后一个word至字符串末尾之间有多个空格的情况;
code1
class Solution {
public:
int lengthOfLastWord(string s) {
if(s.size()==) return ;
int len = ;
int right = s.size()-;
while(right>= && s[right]==' ') right--;//
for(int i=right; i>=; i--)
{
if(s[i]==' ') break;
len++;
}
return len;
}
};
code2
class Solution {
public:
int lengthOfLastWord(string s) {
if(s.size()==) return ;
int len = ;
int right = s.size()-;
while(right>= && s[right]==' ') right--;//
while(right>= && s[right]!=' ')
{
right--;
len++;
}
return len;
}
};
发现while循环好像比for循环要快。。。
题目求解的是最后一个word的长度,所以还要考虑最后一个word之后还有空格的情况。
参考
1.leetcode;
完
【leetcode】58-LengthofLastWord的更多相关文章
- 【LeetCode】58. Length of Last Word 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...
- 【LeetCode】58 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【LeetCode】58.最后一个单词的长度
最后一个单词的长度 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例 ...
- 【LEETCODE】58、数组分类,适中级别,题目:238、78、287
package y2019.Algorithm.array.medium; import java.util.Arrays; /** * @ProjectName: cutter-point * @P ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- 二十、MVC的WEB框架(Spring MVC)
一.Spring MVC 运行原理:客户端请求提交到DispatcherServlet,由DispatcherServlet控制器查询HandlerMapping,找到并分发到指定的Controlle ...
- C++ leetcode Longest Palindromic Substring
明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...
- servlet/和/*匹配的区别
两者真正的区别是,两者的长度不同,根据最长路径匹配的优先级,/*比/更容易被选中,而/的真正含义是,缺省匹配.既所有的URL都无法被选中的时候,就一定会选中/,可见它的优先级是最低的,这就两者的区别.
- Qt贴图实现地图标记效果
#include "wgtmap.h" #include "ui_wgtmap.h" #include <QPainter> #define IMG ...
- Ubuntu 下matlab 查看memory函数
%Copyright (c) 2012, Michael Hirsch%All rights reserved.%%Redistribution and use in source and binar ...
- pickle file in matlab
Load pickle files in Matlab Posted on June 12, 2013 by xcorr http://xcorr.net/2013/06/12/load-pick ...
- HTTP上传数据 :表单,二进制数据(multipart/form-data application/octet-stream boundary)
使用WinINet 一个较简单的例子:上传头像 void CBackstageManager::UpdateAvatarThreadProc(LPVOID params) { stForThread* ...
- 2.python函数编程-filter函数
fileter功能主要使用在需要对数据进行多种操作,并对数据进行过滤的操作. 普通函数实现: movie = ['sb_alex', 'wupei', 'tiger', 'goosb','xxfd', ...
- unity中鼠标按下加速漫游,鼠标抬起减速漫游。
private bool IsMouseUpOrDown=true; //一开始默认是鼠标抬起状态 if (Input.GetMouseButtonDown(1)) //鼠标按下的瞬间状态 { IsM ...
- java新随笔
1.纯随机数发生器 Xn+1=(aXn + c)mod m Modulus=231-1=int.MaxValue Multiplier=75=16807 C=0 当显示过2^31-2个数之后,才可能重 ...