leetcode Length of Last Word

题目连接

https://leetcode.com/problems/length-of-last-word/  

Length of Last Word

Description

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.

class Solution {
public:
	int lengthOfLastWord(string s) {
		size_t n = s.length();
		size_t lb = 0, ub = n - 1;
		reverse(s.begin(), s.end());
		while (lb < n)  { if (s[lb] == ' ') lb++; else break; }
		while (ub >= 0) { if (s[ub] == ' ') ub--; else break; }
		if (lb == n && ub == -1) return 0;
		int ans = 0;
		for (size_t i = lb; i <= ub; i++) {
			if (s[i] != ' ') ans++;
			else break;
		}
		return ans;
	}
};

转载于:https://www.cnblogs.com/GadyPu/p/5040115.html

Reactor 响应式编程(第一篇:Reactor核心) 响应式:响应式编程: 全异步、消息、事件回调默认还是用当前线程,生成整个流、发布流、流操作.log();//只要不指定线程池,默认发布者用的线程就是订阅者的线程; 阅读详情

相关推荐

(UE4 4.27) HISM性能优化:从空间剔除到合批渲染的实战解析

本文深入解析了UE4 4.27中HISM(UHierarchicalInstancedStaticMesh)组件的性能优化原理与实战技巧。核心在于利用其N叉树空间结构实现高效的CPU视锥剔除,并结合GPU实例化进行合批渲染。文章详细探讨了从控制叶子节点粒度、平衡剔除精度与渲染效率,到结合LOD系统等关键参数的调优方法,为开放世界或大场景的植被性能瓶颈提供了系统解决方案。

weixin_29015051的博客 334

LeetCode-Length of Last Word

作者:disappearedgod 文章出处:http://blog.csdn.net/disappearedgod/article/details/23621903 时间:2014-8-27

上帝已逝,自求多福 775

C# CAD2016 多边形顶点按方向重新排序

这样,通过以上步骤,我们找到了一个多边形的一个候选起始顶点(可能位于西北方向),并且已经获取了该多边形的边界框信息,这些信息对于后续判断顶点排列顺序及处理其他相关问题具有重要意义。完整地处理八个方向(东北、东南、西南、西北、北、南、东、西),我们可以创建一个方法来获取多边形相对于给定点的最近顶点及其对应的方向。对象(或类似的多边形表示方式),其中包含了多边形的顶点。//附送AI 图片无版权 随意用 龙年大吉大利。

转眼之间 一路有你 1369

LeetCode Algorithm 0058 - Length of Last Word (Easy)

0058 - Length of Last Word (Easy),Related Topics: String

DarkRabbit的专栏 315

基于 Cognex VisionPro 的工业级图像匹配技术全解析

通过本文的深度解析,开发者可以系统掌握 Cognex VisionPro 图像匹配技术的核心原理、开发流程、优化策略及工业级应用。实际项目中,需结合具体场景灵活调整参数,综合运用预处理、特征提取和匹配算法,构建稳定可靠的视觉系统。

品质生活,极致编程。 1602

Python自动化生成HyperMesh Tcl命令流:从几何建模到有限元分析

本文详细介绍了如何利用Python自动化生成HyperMesh的Tcl命令流,实现从几何建模到有限元分析的全流程二次开发。通过封装常用操作函数,工程师可以高效完成参数化建模、自动网格划分及求解器文件输出,显著提升CAE前处理效率与标准化水平。

weixin_29041767的博客 432

leetcode-58. Length of Last Word最后一单词的长度 python

Length of Last Word题目python split分割遍历 题目 Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string. If the la

。 229

本地部署gradle

在部署CAS构建执行包时,如果按照CAS的官方命令gradlew clean build,如果系统没安装过gradle,这个步骤将会从网上下载gradle,但是进度非常慢,耗时很长。为了更快地完成项目构建,我们可以直接在本地部署好gradle,节省一点时间。

益添的博客 793

LeetCode——58. Length of Last Word

标题58. Length of Last Word 题目描述 给定字符串s由大写/小写字母和空格字符’'组成,返回字符串中最后一个单词的长度。 如果最后一个单词不存在,则返回0。 注意:单词定义为字符序列仅由非空格字符组成。 例子: Input: “Hello World” Output: 5 解题思路 1.将字符串转化位字符数组 2.对字符数组遍历 3.如果元素不是空格时,计数器加...

jmu_sxc的博客 170

数字IC后端实战:Innovus与ICC2中如何巧妙规避Filler1缺失引发的DRC问题?

本文针对数字IC后端设计中工艺厂不提供最小填充单元Filler1的常见挑战,深入剖析了由此引发的DRC问题根源。详细对比了在Cadence Innovus和Synopsys ICC2两大主流PR工具中,如何通过设置单元间距约束来系统性规避物理缝隙,确保电源网络完整性与制造可靠性,为工程师提供了一套稳健的实战解决方案。

weixin_29303701的博客 319

MySQL 分库分表及其平滑扩容方案

MyCAT不仅仅可以用作读写分离,以及分表分库、容灾管理,而且可以用于多租户应用开发、云平台基础设施,让你的架构具备很强的适应性和灵活性。分表对于一张多行(记录)多列(字段)的二维数据表,又分两种情形(1)垂直分表竖向切分,不同分表存储不同的字段,可以把不常用或者大容量、或者不同业务的字段拆分出去;扩容前每个节点的数据,有一半要迁移至一个新增节点中,对应关系比较简单。此时,四个节点的数据都已完整,只是有冗余(多存了和自己配对的节点的那部分数据),择机清除即可(过后随时进行,不影响业务)。...

m0_71777195的博客 1935

【Leetcode】Length of Last Word

题目链接:https://leetcode.com/problems/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 st

业精于勤,荒于嬉 1622

FOC实战笔记:RC吸收电路的设计考量与参数调优

本文深入探讨了FOC驱动器中RC吸收电路的设计与调优。针对MOS管开关过程中的电压尖峰问题,详细阐述了RC电路的钳位与能量吸收原理,并提供了从参数计算、实验调试到布局布线的完整实战指南,旨在帮助工程师有效提升系统可靠性与稳定性。

weixin_29219539的博客 294

python写算法题:leetcode: 58. Length of Last Word

class Solution(object): def lengthOfLastWord(self, s): """ :type s: str :rtype: int """ ret=0 lastch = len(s)-1 for _ in xrange(len(s)): ...

244

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

【LeetCode with Python】 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 charact

XNERV SURVEYS 9910

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

[LeetCode] Length of Last Word

[Problem] 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

1.02^365的成长裂变 3635

【leetcode c++】58 Length of Last Word

Length of Last Word Given a string s consists ofupper/lower-case alphabets and empty space characters ' ', return the length oflast word in the string. If the last word does not exist, return 0.

lif的博客 443

LeetCode——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 ...

sinat_22994595的博客 268

Length of Last Word -- LeetCode

原题链接: http://oj.leetcode.com/problems/length-of-last-word/  这道题比较简单,就是进行一下字符串操作。唯一的细节就是要去掉尾部的空格,然后读到下一个空格,记录下长度。时间复杂度是O(n),n是字符串的长度,空间复杂度是O(1)。代码如下:public int lengthOfLastWord(String s) { if(s==n

Code Ganker 8238

[LeetCode] Length of last word

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, ret

coder 进阶的专栏 750

LeetCode58 Length of Last Word

详细见:leetcode.com/problems/length-of-last-word Java Solution: github package leetcode; public class P058_LengthOfLastWord { public static void main(String[] args) { System.out.println(new S

zxwtry的博客 359

leetcode:Length of Last Word

一、 题目 给定一个字符串s由大/小写字母和空的空间字符' ',返回字符串的最后一个单词的长度。 如果最后一个单词不存在,则返回0。 注:一个单词被定义为一个不带空格的字符序列。 例如, 给出s =“Hello World”, 返回5。 二、 分析 没有什么复杂的算法,按照规则考虑所有情况就行,有下面的几种情况: 1、“   ” 2、“abc” 3、“   abc” 4、“

恰同学少年 1079

Leetcode之Length of Last Word

题目: Given a stringsconsists 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 ...

qq_35455503的博客 147

leetcode || 58、Length of Last Word

problem: 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

hustyangju的足迹 723

LeetCode之Length of Last Word

http://oj.leetcode.com/problems/length-of-last-word/ 返回最后一个单词的长度,使用java的split方法即可 public class Solution { public int lengthOfLastWord(String s) { String[] arr = s.split

叠搭宝箱 913
上一篇: leetcode Plus One
下一篇: leetcode Spiral Matrix
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值