当前位置:网站首页><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])
边栏推荐
- How to write test cases for test coupons?
- . Net Maui performance improvement
- 软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
- 《论文阅读》Neural Approaches to Conversational AI(1)
- Talk about SOC startup (x) kernel startup pilot knowledge
- 《通信软件开发与应用》课程结业报告
- Cmu15445 (fall 2019) project 2 - hash table details
- 【滤波跟踪】捷联惯导纯惯导解算matlab实现
- Visual Studio 2019 (LocalDB)\MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782版及更低版本
- SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
猜你喜欢
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
从工具升级为解决方案,有赞的新站位指向新价值
Fleet tutorial 19 introduction to verticaldivider separator component Foundation (tutorial includes source code)
Flet教程之 14 ListTile 基础入门(教程含源码)
About how to install mysql8.0 on the cloud server (Tencent cloud here) and enable local remote connection
[system design] index monitoring and alarm system
Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
Explore cloud database of cloud services together
How to connect 5V serial port to 3.3V MCU serial port?
随机推荐
Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration
sink 消费 到 MySQL, 数据库表里面已经设置了 自增主键, flink 里面,如何 操作?
[full stack plan - programming language C] basic introductory knowledge
Matlab implementation of Huffman coding and decoding with GUI interface
In my limited software testing experience, a full-time summary of automation testing experience
R语言使用magick包的image_mosaic函数和image_flatten函数把多张图片堆叠在一起形成堆叠组合图像(Stack layers on top of each other)
STM32 entry development NEC infrared protocol decoding (ultra low cost wireless transmission scheme)
HCIA复习整理
VIM command mode and input mode switching
Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on SPI)
Suggestions on one-stop development of testing life
Talk about SOC startup (VII) uboot startup process III
R語言使用magick包的image_mosaic函數和image_flatten函數把多張圖片堆疊在一起形成堆疊組合圖像(Stack layers on top of each other)
《通信软件开发与应用》课程结业报告
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
Reasons for the failure of web side automation test
There are so many factors that imprison you
108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]