LeetCode Length of Last Word

WPF+MVVM案例实战(一)- 设备状态LED灯变化实现 采用WPF+MVVM框架实现,MVVM框架使用CommunityToolkit.Mvvm 依赖包,代码案例部署了三个LED灯,按照红黄绿三色后台自动刷新状态进行变化,根据后台数据不同,UI层显示不同颜色。 阅读详情

原题链接在这里:https://leetcode.com/problems/length-of-last-word/

从最后一个字符开始数,最后一个字符是 '  ', 就一直向前移动指针直到出现第一个字符。

开始计数,然后继续扫面直到出现下一个 '  ', 返回计数结果即可。

Note:  1. while loop加 i--, 这里已经犯过很多次类似的错误了。

2. 在使用s.charAt(i)时 一定要先确保 i>=0, 否则就会有out of index的错误。

AC Java:

public class Solution {
    public int lengthOfLastWord(String s) {
        if(s == null || s.length() == 0){
            return 0;
        }
        int res = 0;
        int i = s.length()-1;
        while(i>=0 && s.charAt(i) == ' '){  //error
            i--;
        }
        
        while(i>=0){
            if(s.charAt(i) != ' '){
                res++;
            }else{
                break;
            }
            i--;                            //error
        }
        return res;
    }
}


SAP-ABAP:SAP IDOC 全面学习指南 阅读详情

相关推荐

一种高超声速飞行器弹道的仿真方法

设计并实现了一个高超声速飞行器弹道仿真算法,并应用到基于OSGEarth的态势中。

eyepeak的博客 6541

LeetCode-Length of Last Word

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

上帝已逝,自求多福 774

乐鑫WIFI芯片开发流程梳理-工具链、编译和下载

最近开始基于乐鑫ESP32的WIFI模组的软件开发,首先要做整套流程的梳理,乐鑫官网以及安信可上已经有了较为系统的资料,但是在实现过程中也遇到了一些问题,花了几天去解决,所以为了总结,也为了给其他人提供一些借鉴,把流程分享一下,官网上相对比较完善的,就不在赘述。 1. 开发方案选择 目前网上有两套较为成熟的方案: (1)基于乐鑫官网推荐的开发方式,原网页:https://docs.espress...

一名嵌入式软件工程师的自我成长 3925

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

。 228

iOS反编译-hook微信之艾特所有人

all。

2501_90243249的博客 759

单相PWM整流器搭建训练——(一)拓扑原理介绍

单相整流器是一种将交流电转换为直流电的设备,主要由变压器、整流元件和滤波电路组成。变压器将输入的交流电压转换为适合整流的较低电压,整流元件(如二极管)利用其单向导电性将交流电转换为脉动直流电,最后滤波电路去除脉动直流电中的纹波,输出较为平滑的直流电,从而实现交流到直流的转换,广泛应用于各类电源设备中。

DikouNass的博客 1587

LeetCode Algorithm 0058 - Length of Last Word (Easy)

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

DarkRabbit的专栏 313

树莓派 SPI操作

扩展板配备一款0.96inch,128864分辨率的oled显示屏,通过SPI控制,先上显示效果图。由于程序过长,在这里我就不把代码全部贴出来了,只是简单讲解一下spi的操作函数。管脚如下如所有,左边方框的管脚分别为MOSI MISO SCLK,左边CE0,CE1为两个片选管脚,分别对应上图中的spidev0.0,spidev0.1两个设备。下载spidev库,复制到树莓派,并行运行如下命令解压安装。spi设备,此处设备为/dev/spi-decv0.0。在/dev路径下面,我们可以发现两个spi设备。

weixin_46529898的博客 914

LeetCode——58. Length of Last Word

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

jmu_sxc的博客 168

基于YOLOv8的人群聚集检测系统:含UI界面设计与数据集应用

人群聚集检测是智慧城市、公共安全和疫情防控等领域的重要任务。本文结合YOLOv8最新目标检测技术,设计并实现了一个高效的人群聚集检测系统,支持可视化UI界面操作,方便用户实时检测和分析视频中的人群密度。本文详细介绍了系统设计思路、使用的数据集、YOLOv8模型训练和推理过程,以及UI界面的开发与整合。最后通过实验验证了该系统的实用性和性能表现。更灵活的模型架构强化的特征融合网络(PAN++)自动混合精度训练支持支持多任务学习(目标检测+分类+分割)预训练模型丰富,适应多种应用场景。

YOLO 432

家用智能监控摄像机如何使用NAS进行存储/备份(小米智能摄像机如何解决群晖 / 飞牛 NAS 识别失败、文件目录不显示问题)

小米摄像头NAS存储总出问题?🤯 别慌!本文揭秘NAS底层逻辑,群晖、飞牛实战指南。手把手教你排查解决所有难题,轻松搞定监控录像安全备份!🔒

XiaoqiangClub的博客 2693

【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

业精于勤,荒于嬉 1618

一文掌握 MongoDB 详细安装与配置(Windows / Linux / macOS 全平台)

本文详细介绍了MongoDB社区版7.0+在Windows、Linux和macOS三大操作系统上的完整安装、配置与安全加固流程。主要内容包括:各平台推荐安装方式(Windows使用MSI安装包、Linux通过官方仓库、macOS通过Homebrew)、基本配置参数说明、服务启动方法以及关键安全措施(如创建管理员用户和启用身份验证)。文章还提供了系统最低要求、下载地址、数据目录设置建议以及生产环境使用注意事项,适合开发、测试及生产环境部署参考。

数据知道的博客 6708

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)): ...

243

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

【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 9908

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

[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的成长裂变 3634

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

【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的博客 441

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 8236

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的博客 267

[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 进阶的专栏 749

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的博客 358

leetcode:Length of Last Word

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

恰同学少年 1076

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 le...

dong12276的博客 150

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的足迹 721

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的博客 146

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

叠搭宝箱 912
上一篇: LeetCode Count and Say
下一篇: LeetCode ZigZag Conversion
Dylan_Java_NYC
博客等级 码龄11年 1粉丝 134原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值