当前位置:网站首页>leetcode之最后一个单词的长度
leetcode之最后一个单词的长度
2020-11-09 23:48:00 【go4it】
序
本文主要记录一下leetcode之最后一个单词的长度
题目
给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度。如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词。
如果不存在最后一个单词,请返回 0 。
说明:一个单词是指仅由字母组成、不包含任何空格字符的 最大子字符串。
示例:
输入: "Hello World"
输出: 5
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/length-of-last-word
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
class Solution {
public int lengthOfLastWord(String s) {
int result = 0;
char[] chars = s.toCharArray();
for (int i= s.length()-1; i >=0; i--) {
if (chars[i] != ' ') {
result++;
continue;
}
if (result != 0) {
return result;
}
}
return result;
}
}
小结
这里从后往前遍历字符串数组,遇到非空格的累加长度,遇到空格则判断结果是否为0,不为0则返回结果。
doc
版权声明
本文为[go4it]所创,转载请带上原文链接,感谢
https://my.oschina.net/go4it/blog/4710388
边栏推荐
- 表单验证,为避免全局污染,少定义全局变量写法
- Error running app: default activity not found solution
- Visit 2020 PG Technology Conference
- ES6, ES7, es8 Learning Guide
- Important components of Apache Hadoop
- Just graduated will be confused, I after 7 years of summary, give programmers your 7 suggestions
- 函数计算进阶-IP查询工具开发
- The kth smallest node in the print binary search tree of offer
- How much is the cost of CRM system?
- C + + game development
猜你喜欢
SSL证书和公网IP地址如何影响SEO
商品后台系统优化
公网IP地址和SSL证书可以提升SEO吗?
From master of Engineering Physics of Tsinghua University to open source entrepreneur of cloud computing
Fear of reconstruction? I'm too late to tell you how to refactor. Now I'm here
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王健
C / C + + Programming Notes: C language development tank war! In memory of our lost little overlord game
So what should investors do with the current market? Now a new investment outlet is coming!
Apache Hadoop的重要组成
随机推荐
Functional guide for temporary users and novices of PL / SQL developer
Day84: Luffy: preferential activity strategy & User Authentication & checking / settlement of shopping cart goods
日常页码样式问题
The problem of looting by leetcode
Kubernetes-18: installation and use of dashboard
Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法
Make a home page
[best practice] learn how eolinker helps Telecommuting
手把手教你使用容器服务 TKE 集群审计排查问题
Important components of Apache Hadoop
What is the architecture of a live broadcast system worth more than $1 billion?
Traditional purchasing mode has changed! How to innovate automobile purchasing function?
剑指offer之打印二叉搜索树中第k小的结点
会展云技术解读 | 面对突发事故,APP 如何做好崩溃分析与性能监控?
CUDA_ Memory model
So what should investors do with the current market? Now a new investment outlet is coming!
LeetCode 50 Pow(x,n)
没有磁盘空间 No space left on device
Come and learn! Development Guide for personalized recommendation system (with internet disk link)
November 09, 2020: talk about the similarities and differences between the bulon filter and the cuckoo filter?