当前位置:网站首页>[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
边栏推荐
- What category does the Internet of things application technology major belong to
- Daily three questions 6.28
- Why is PHP called hypertext preprocessor
- Daily three questions 6.29
- Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
- Typescript enumeration
- 2022 safety officer-c certificate examination question simulation examination question bank and simulation examination
- [micro service sentinel] sentinel integrates openfeign
- 常见的积分商城游戏类型有哪些?
- ShanDong Multi-University Training #3
猜你喜欢
2022年起重机司机(限桥式起重机)考试试题及模拟考试
Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry
Wechat personal small store one click opening assistant applet development
【小程序】通过scroll-view组件实现左右【滑动】列表
深度学习 | 三个概念:Epoch, Batch, Iteration
为什么PHP叫超文本预处理器
Future trend and development of neural network Internet of things
神经网络物联网的发展趋势和未来方向
会声会影2022智能、快速、简单的视频剪辑软件
认识--Matplotlib
随机推荐
Experience of practical learning of Silicon Valley products
图的遍历之深度优先搜索和广度优先搜索
【必会】BM41 输出二叉树的右视图【中等+】
What is the relationship between modeling and later film and television?
字典、哈希表、数组的概念
Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
[applet] realize the left and right [sliding] list through the scroll view component
Redis~02 cache: how to ensure data consistency in MySQL and redis when updating data?
Is there a piece of code that makes you convinced by human wisdom
Practical application and extension of plain framework
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
Typescript enumeration
学成在线案例实战
Future trend and development of neural network Internet of things
Concepts of dictionary, hash table and array
2022 safety officer-c certificate examination question simulation examination question bank and simulation examination
【微服务|Sentinel】@SentinelResource详解
物联网开发零基础教程
Three development trends of enterprise application from the perspective of the third technological revolution
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee