当前位置:网站首页><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])
边栏推荐
- 总结了200道经典的机器学习面试题(附参考答案)
- Network protocol concept
- Present pod information to the container through environment variables
- [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]
- 《论文阅读》Neural Approaches to Conversational AI(1)
- What is high cohesion and low coupling?
- 千人規模互聯網公司研發效能成功之路
- 超标量处理器设计 姚永斌 第10章 指令提交 摘录
- Talk about SOC startup (VI) uboot startup process II
- R Language Using Image of magick package Mosaic Function and Image La fonction flatten empile plusieurs images ensemble pour former des couches empilées sur chaque autre
猜你喜欢
【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
The running kubernetes cluster wants to adjust the network segment address of pod
[system design] index monitoring and alarm system
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
Swiftui swift internal skill how to perform automatic trigonometric function calculation in swift
Flet教程之 17 Card卡片组件 基础入门(教程含源码)
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
Mastering the new functions of swiftui 4 weatherkit and swift charts
Camera calibration (2): summary of monocular camera calibration
Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
随机推荐
Reasons for the failure of web side automation test
5V串口接3.3V单片机串口怎么搞?
Fleet tutorial 19 introduction to verticaldivider separator component Foundation (tutorial includes source code)
sql里,我想设置外键,为什么出现这个问题
EasyUI learn to organize notes
2022年在启牛开华泰的账户安全吗?
Matlab implementation of Huffman coding and decoding with GUI interface
Internet Protocol
Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration
112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]
MATLAB實現Huffman編碼譯碼含GUI界面
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于SPI)
STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动
MySQL安装常见报错处理大全
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
How to connect 5V serial port to 3.3V MCU serial port?
Explore cloud database of cloud services together
正在运行的Kubernetes集群想要调整Pod的网段地址
Improve application security through nonce field of play integrity API
清华姚班程序员,网上征婚被骂?