当前位置:网站首页>[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
边栏推荐
- 物联网开发零基础教程
- Win 10 mstsc connect RemoteApp
- 物联网应用技术专业是属于什么类
- flutter Unable to load asset: assets/images/888.png
- [micro service sentinel] @sentinelresource details
- 每日三题 6.30(2)
- 【小程序】通过scroll-view组件实现左右【滑动】列表
- VIM color the catalogue
- 2021 RoboCom 世界机器人开发者大赛-本科组初赛
- What is the difference between memory leak and memory overflow?
猜你喜欢
mt管理器测试滑雪大冒险
2022年R1快开门式压力容器操作考题及答案
What is mosaic?
Distance measurement - Hamming distance
Three development trends of enterprise application from the perspective of the third technological revolution
字典、哈希表、数组的概念
物联网应用技术专业是属于什么类
Commemorate becoming the first dayus200 tripartite demo contributor
软件架构的本质
【小程序】通过scroll-view组件实现左右【滑动】列表
随机推荐
Huisheng Huiying 2022 intelligent, fast and simple video editing software
每日三题 6.30(2)
Development trend and future direction of neural network Internet of things
从第三次技术革命看企业应用三大开发趋势
notBlank 和 notEmpty
神经网络物联网的发展趋势和未来方向
Glass mosaic
Applet form verification encapsulation
Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
通过Go语言创建CA与签发证书
dat. GUI
2022年最佳智能家居开源系统:Alexa、Home Assistant、HomeKit生态系统介绍
纪念成为首个DAYUs200三方demo贡献者
Daily three questions 6.29
云信小课堂 | IM及音视频中常见的认知误区
Daily three questions 6.28
Practical application and extension of plain framework
硅谷产品实战学习感触
Postgresql源码(57)HOT更新为什么性能差距那么大?
Wechat personal small store one click opening assistant applet development