当前位置:网站首页>[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 of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
- What is mosaic?
- Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
- The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
- 2022 crane driver (limited to bridge crane) examination questions and simulation examination
- 物联网应用技术专业是属于什么类
- SWT / anr problem - SWT causes kernel fuse deadlock
- Paramètres communs de matplotlib
- Practical application and extension of plain framework
- CKS CKA ckad change terminal to remote desktop
猜你喜欢
随机推荐
Create Ca and issue certificate through go language
学成在线案例实战
Huisheng Huiying 2022 intelligent, fast and simple video editing software
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
物联网现状及未来发展趋势
[applet] realize the left and right [sliding] list through the scroll view component
Concepts of dictionary, hash table and array
SWT/ANR问题--SWT 导致 kernel fuse deadlock
硅谷产品实战学习感触
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
The digital summit is popular, and city chain technology has triggered a new round of business transformation
图的遍历之深度优先搜索和广度优先搜索
字典、哈希表、数组的概念
Redis 主从同步
Daily three questions 6.30
每日三题 6.30(2)
2021 RoboCom 世界机器人开发者大赛-高职组复赛
Why is PHP called hypertext preprocessor
Matplotlib常用设置
Redis RDB快照