当前位置:网站首页><No. 8> 1816. 截断句子 (简单)
<No. 8> 1816. 截断句子 (简单)
2022-07-07 10:02:00 【薰珞婷紫小亭子】
目录
题目描述:
句子 是一个单词列表,列表中的单词之间用单个空格隔开,且不存在前导或尾随空格。每个单词仅由大小写英文字母组成(不含标点符号)。
例如,"Hello World"、"HELLO" 和 "hello world hello world" 都是句子。
给你一个句子 s 和一个整数 k ,请你将 s 截断 ,使截断后的句子仅含 前 k 个单词。返回 截断 s 后得到的句子。

Python 实现:
class Solution(object):
def truncateSentence(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
list_num = []
list_num = s.split(" ") #按照空格将原字符串拆分
list_final = []
for i in range (k):
list_final.append(list_num[i])
return " ".join(list_final) #将list转换成str
一行实现:
class Solution(object):
def truncateSentence(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
return " ".join(s.split(" ")[:k])边栏推荐
- 千人规模互联网公司研发效能成功之路
- 正在运行的Kubernetes集群想要调整Pod的网段地址
- Fleet tutorial 14 basic introduction to listtile (tutorial includes source code)
- Mastering the new functions of swiftui 4 weatherkit and swift charts
- Enclosed please find. Net Maui's latest learning resources
- Use references
- VIM command mode and input mode switching
- [system design] index monitoring and alarm system
- Electron adding SQLite database
- Network protocol concept
猜你喜欢

相机标定(2): 单目相机标定总结
![110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]](/img/62/1ec8885aaa2d4dca0e764b73a1e2df.png)
110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]

Fleet tutorial 14 basic introduction to listtile (tutorial includes source code)

竟然有一半的人不知道 for 与 foreach 的区别???

【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码

【最短路】Acwing1128信使:floyd最短路

Talk about SOC startup (VI) uboot startup process II

核舟记(一):当“男妈妈”走进现实,生物科技革命能解放女性吗?

《通信软件开发与应用》课程结业报告

The running kubernetes cluster wants to adjust the network segment address of pod
随机推荐
【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
Detailed explanation of debezium architecture of debezium synchronization
Flet教程之 17 Card卡片组件 基础入门(教程含源码)
[system design] index monitoring and alarm system
千人規模互聯網公司研發效能成功之路
How much do you know about excel formula?
Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB
La voie du succès de la R & D des entreprises Internet à l’échelle des milliers de personnes
【系统设计】指标监控和告警系统
There are so many factors that imprison you
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
超标量处理器设计 姚永斌 第8章 指令发射 摘录
超标量处理器设计 姚永斌 第10章 指令提交 摘录
5V串口接3.3V单片机串口怎么搞?
R語言使用magick包的image_mosaic函數和image_flatten函數把多張圖片堆疊在一起形成堆疊組合圖像(Stack layers on top of each other)
The running kubernetes cluster wants to adjust the network segment address of pod
Flet教程之 15 GridView 基础入门(教程含源码)
[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
Cmu15445 (fall 2019) project 2 - hash table details
【全栈计划 —— 编程语言之C#】基础入门知识一文懂