当前位置:网站首页><No. 8> 1816. Truncate sentences (simple)
<No. 8> 1816. Truncate sentences (simple)
2022-07-07 12:00:00 【Smoked Luoting purple Pavilion】
Catalog
Title Description :
The sentence It's a list of words , Words in the list are separated by a single space , And there are no leading or trailing spaces . Each word is made up of English letters in upper and lower case only ( No punctuation ).
for example ,"Hello World"
、"HELLO"
and "hello world hello world"
It's all sentences .
Here is a sentence for you s And an integer k , Would you please s truncation , send truncation The following sentence contains only front k Word . return truncation s After the sentence .
Python Realization :
class Solution(object):
def truncateSentence(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
list_num = []
list_num = s.split(" ") # Split the original string by spaces
list_final = []
for i in range (k):
list_final.append(list_num[i])
return " ".join(list_final) # take list convert to str
In one line :
class Solution(object):
def truncateSentence(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
return " ".join(s.split(" ")[:k])
边栏推荐
- 【滤波跟踪】捷联惯导纯惯导解算matlab实现
- The Oracle message permission under the local Navicat connection liunx is insufficient
- powershell cs-UTF-16LE编码上线
- How to connect 5V serial port to 3.3V MCU serial port?
- Common locking table processing methods in Oracle
- Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on SPI)
- Matlab implementation of Huffman coding and decoding with GUI interface
- In my limited software testing experience, a full-time summary of automation testing experience
- How to write test cases for test coupons?
- Flet tutorial 17 basic introduction to card components (tutorial includes source code)
猜你喜欢
【最短路】Acwing1128信使:floyd最短路
Suggestions on one-stop development of testing life
[extraction des caractéristiques de texture] extraction des caractéristiques de texture de l'image LBP basée sur le mode binaire local de Matlab [y compris le code source de Matlab 1931]
Flet tutorial 17 basic introduction to card components (tutorial includes source code)
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
Sonar:Cognitive Complexity认知复杂度
《通信软件开发与应用》课程结业报告
OneDNS助力高校行业网络安全
人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
浙江大学周亚金:“又破又立”的顶尖安全学者,好奇心驱动的行动派
随机推荐
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
R语言使用magick包的image_mosaic函数和image_flatten函数把多张图片堆叠在一起形成堆叠组合图像(Stack layers on top of each other)
Nuclear boat (I): when "male mothers" come into reality, can the biotechnology revolution liberate women?
Excel公式知多少?
【最短路】Acwing1128信使:floyd最短路
MySQL安装常见报错处理大全
清华姚班程序员,网上征婚被骂?
Camera calibration (2): summary of monocular camera calibration
Enclosed please find. Net Maui's latest learning resources
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
Technology sharing | packet capturing analysis TCP protocol
La voie du succès de la R & D des entreprises Internet à l’échelle des milliers de personnes
STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动
一起探索云服务之云数据库
STM32 entry development NEC infrared protocol decoding (ultra low cost wireless transmission scheme)
[filter tracking] strapdown inertial navigation simulation based on MATLAB [including Matlab source code 1935]
MATLAB实现Huffman编码译码含GUI界面
powershell cs-UTF-16LE编码上线
What is high cohesion and low coupling?
【紋理特征提取】基於matlab局部二值模式LBP圖像紋理特征提取【含Matlab源碼 1931期】