当前位置:网站首页><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])边栏推荐
- Visual Studio 2019 (LocalDB)\MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782版及更低版本
- Various uses of vim are very practical. I learned and summarized them in my work
- 110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
- 相机标定(1): 单目相机标定及张正友标定基本原理
- Cmu15445 (fall 2019) project 2 - hash table details
- SwiftUI 教程之如何在 2 秒内实现自动滚动功能
- sink 消费 到 MySQL, 数据库表里面已经设置了 自增主键, flink 里面,如何 操作?
- STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动
- Test the foundation of development, and teach you to prepare for a fully functional web platform environment
- [shortest circuit] acwing1128 Messenger: Floyd shortest circuit
猜你喜欢

相机标定(2): 单目相机标定总结

Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on SPI)

【紋理特征提取】基於matlab局部二值模式LBP圖像紋理特征提取【含Matlab源碼 1931期】

NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference

In my limited software testing experience, a full-time summary of automation testing experience

Solve the problem that vscode can only open two tabs

Automated testing framework
![[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code](/img/83/0652e3138b87a4741dd8261a24d1e8.png)
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code

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

【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
随机推荐
Flet教程之 17 Card卡片组件 基础入门(教程含源码)
Internet Protocol
Visual Studio 2019 (LocalDB)\MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782版及更低版本
Talk about SOC startup (IX) adding a new board to uboot
超标量处理器设计 姚永斌 第8章 指令发射 摘录
通过环境变量将 Pod 信息呈现给容器
【滤波跟踪】基于matlab扩展卡尔曼滤波EKF和无迹卡尔曼滤波UKF比较【含Matlab源码 1933期】
powershell cs-UTF-16LE编码上线
【最短路】ACwing 1127. 香甜的黄油(堆优化的dijsktra或spfa)
Flet教程之 18 Divider 分隔符组件 基础入门(教程含源码)
Flet教程之 19 VerticalDivider 分隔符组件 基础入门(教程含源码)
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
Easyui学习整理笔记
Automated testing framework
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
VIM command mode and input mode switching
SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod
Swiftui tutorial how to realize automatic scrolling function in 2 seconds
《通信软件开发与应用》课程结业报告