【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-十大经典排序算法
随机推荐
- 基数排序模板[luogu 1177]
#include<bits/stdc++.h> #define LL long long using namespace std; ,bas=; ]; LL idx(LL k,LL w) ...
- [LightOJ 1265] Island of Survival
Island of Survival You are in a reality show, and the show is way too real that they threw into an i ...
- 一、I/O操作(缓存流,数据流,对象流)
一.缓存流 以介质是硬盘为例子说明,字节流和字符流的缺点: 每次读写的时候,都会访问硬盘,如果读写频率比较高的时候,性能不佳.为了解决问题,采用缓存流. 缓存流在读取的时候,会一次性读较多的数据到缓存 ...
- NOSQL -- Mongodb的简单操作与使用(win10)
NOSQL -- Mongodb的简单操作与使用(wins) MongoDB 创建集合: db.createCollection(name, options) use huhu db.createCo ...
- InnoDB存储引擎表的主键
在InnoDB存储引擎中,表是按照主键顺序组织存放的.在InnoDB存储引擎表中,每张表都有主键(primary key),如果在创建表时没有显式地定义主键,则InnoDB存储引擎会按如下方式选择或创 ...
- sql group by max
SELECT * , REPLACE(TDFG.xdfd,'doc_111','') GBFROM ( SELECT * FROM ...
- 强制ubuntu登陆用户退出
#skill -KILL -u user1 杀死并注销user1. #skill -CONT -u user1 恢复user1. 在Windows 2003默认情况下,三个以上就远程不了,必须强制登录 ...
- Centos 7.4 源码 Nginx 安装
一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 二.首先要安装 PCRE ...
- pyhton-函数初级
f = open("司马光砸缸", mode="r+", encoding="utf-8") f.seek(12) f.truncate() ...
- RabbitMQ 队列、消息持久化
RabbitMQ的消息队列的持久化是一个很不错的功能,设置也非常简单.如下代码: 1.设置队列持久化(在声明队列的时候设置) channel.QueueDeclare(queue: "q.l ...