当前位置:网站首页>[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边栏推荐
- 2022年起重机司机(限桥式起重机)考试试题及模拟考试
- Typescript enumeration
- Material Design组件 - 使用BottomSheet展现扩展内容(一)
- 上海炒股开户选择手机办理安全吗?
- 【微服务|Sentinel】SentinelResourceAspect详解
- Daily three questions 6.30
- mt管理器测试滑雪大冒险
- 字典、哈希表、数组的概念
- Redis~02 cache: how to ensure data consistency in MySQL and redis when updating data?
- 想请教股票开户要认识谁?在线开户是安全么?
猜你喜欢
![[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing](/img/08/9ecfd53a04e147022dde3449aec132.png)
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing

Redis RDB快照

Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term

Matplotlib常用設置

from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘

MySQL binlog cleanup

深度学习 | 三个概念:Epoch, Batch, Iteration

2022年R1快开门式压力容器操作考题及答案

Stm32f030f4 drives tim1637 nixie tube chip

物联网应用技术专业是属于什么类
随机推荐
Depth first search and breadth first search of graph traversal
Airserver latest win64 bit personal screen projection software
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
What is the relationship between modeling and later film and television?
URL introduction
Development trend and future direction of neural network Internet of things
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
[must] bm41 output the right view of the binary tree [medium +]
MySQL binlog cleanup
[LeetCode] 最后一个单词的长度【58】
Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry
问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
物联网开发零基础教程
Redis 主从同步
问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
Create Ca and issue certificate through go language
2022年危险化学品经营单位安全管理人员考试题及在线模拟考试
MT manager test skiing Adventure
How to display real-time 2D map after rviz is opened
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘