当前位置:网站首页>[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边栏推荐
- STM32F030F4驱动TIM1637数码管芯片
- Postgresql随手记(10)动态执行EXECUTING语法解析过程
- 常见的积分商城游戏类型有哪些?
- Linux基础 —— CentOS7 离线安装 MySQL
- Linux foundation - centos7 offline installation of MySQL
- Win 10 mstsc connect RemoteApp
- Istio, ebpf and rsocket Broker: in depth study of service grid
- 神经网络物联网的未来趋势与发展
- Typescript enumeration
- "35 years old, the boss of the company, with a monthly salary of 20000, give away takeout": the times abandoned you, not even saying goodbye
猜你喜欢

MT manager test skiing Adventure

Redis AOF log

学成在线案例实战

Future trend and development of neural network Internet of things

物联网开发零基础教程

Is there a piece of code that makes you convinced by human wisdom

What is the mosaic tailgate?
![[micro service sentinel] sentinel integrates openfeign](/img/8b/46156255fd980eb422c7e05d5af7ee.png)
[micro service sentinel] sentinel integrates openfeign

flutter Unable to load asset: assets/images/888.png

Huisheng Huiying 2022 intelligent, fast and simple video editing software
随机推荐
Zhao Fuquan: to ensure supply in the short term, we should build a safe, efficient and resilient supply chain in the long term
Win 10 mstsc connect RemoteApp
问题随记 —— file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 c
力扣今日题-241. 为运算表达式设计优先级
2022 R1 fast opening pressure vessel operation test questions and answers
Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
RPA: Bank digitalization, business process automation "a small step", and loan review efficiency "a big step"
mt管理器测试滑雪大冒险
Redis AOF日志
MySQL binlog cleanup
Linux基础 —— CentOS7 离线安装 MySQL
Daily three questions 6.29
CKS CKA ckad change terminal to remote desktop
Postgresql源码(58)元组拼接heap_form_tuple剖析
Future trend and development of neural network Internet of things
The online beggar function of Japanese shopping websites
ARP报文头部格式和请求流程
Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
内存泄露和内存溢出的区别是什么?