当前位置:网站首页>[leetcode] length of the last word [58]
[leetcode] length of the last word [58]
2022-07-01 23:31:00 【When camellia flowers bloom.】
problem : Give you a string s, It consists of several words , Words are separated by some space characters . Returns the length of the last word in a string ( A word is made up of only letters 、 The largest substring that does not contain any space characters )
Example 1
Input : s = "Hello World"
Output : 5
explain : The last word is “World”, The length is 5
Example 2
Input : s = " fly me to the moon "
Output : 4
explain : The last word is “moon”, The length is 4
Example 3
Input : s = "luffy is still joyboy"
Output : 6
explain : The last word is... In length 6 Of “joyboy”
# solution 1
def lengthOfLastWord(s):
# Remove spaces at both ends of the string
s = s.strip(" ")
arr = s.split(" ")
lastWord = arr[-1]
res = len(lastWord)
return res
# solution 2
def lengthOfLastWord(s):
res = []
new_s = s.strip()
new_s = new_s[::-1]
for i in new_s:
if i != ' ':
res.append(i)
else:
break
return len(res)
lengthOfLastWord("Hello World") # 5
lengthOfLastWord(" fly me to the moon ") # 4
lengthOfLastWord("luffy is still joyboy") # 6边栏推荐
- Understanding threads
- 神经网络物联网的未来趋势与发展
- MySQL -- convert rownum in Oracle to MySQL
- MySQL binlog cleanup
- RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"
- 力扣今日题-241. 为运算表达式设计优先级
- Daily three questions 6.28
- Three development trends of enterprise application from the perspective of the third technological revolution
- 证券开户选哪个证券公司比较好,哪个更安全
- 13 MySQL-约束
猜你喜欢

Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four

字典、哈希表、数组的概念

Redis 主从同步

Practical application and extension of plain framework

MT manager test skiing Adventure

Win 10 mstsc connect RemoteApp

有没有一段代码,让你为人类的智慧所折服

2022年危险化学品经营单位安全管理人员考试题及在线模拟考试

物联网技术应用属于什么专业分类

The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
随机推荐
硅谷产品实战学习感触
物联网应用技术专业是属于什么类
【微服务|Sentinel】@SentinelResource详解
What are the common types of points mall games?
Concepts of dictionary, hash table and array
What is the difference between memory leak and memory overflow?
ShanDong Multi-University Training #3
VIM color the catalogue
云信小课堂 | IM及音视频中常见的认知误区
CKS CKA ckad change terminal to remote desktop
SWT/ANR问题--SWT 导致 kernel fuse deadlock
Is there a piece of code that makes you convinced by human wisdom
Applet form verification encapsulation
【微服务|Sentinel】SentinelResourceAspect详解
Material Design组件 - 使用BottomSheet展现扩展内容(一)
AirServer最新Win64位个人版投屏软件
Practical application and extension of plain framework
认识--Matplotlib
每日三题 6.28
Daily three questions 6.28