当前位置:网站首页><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])
边栏推荐
- Camera calibration (2): summary of monocular camera calibration
- Common SQL statement collation: MySQL
- Common locking table processing methods in Oracle
- Solve the problem that vscode can only open two tabs
- UP Meta—Web3.0世界创新型元宇宙金融协议
- Onedns helps college industry network security
- Detailed explanation of debezium architecture of debezium synchronization
- La voie du succès de la R & D des entreprises Internet à l’échelle des milliers de personnes
- 问下flinkcdc2.2.0的版本,支持并发,这个并发是指多并行度吗,现在发现,mysqlcdc全
- 【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
猜你喜欢
清华姚班程序员,网上征婚被骂?
How to write test cases for test coupons?
powershell cs-UTF-16LE编码上线
Half of the people don't know the difference between for and foreach???
【系统设计】指标监控和告警系统
Programming examples of stm32f1 and stm32subeide -315m super regenerative wireless remote control module drive
超标量处理器设计 姚永斌 第8章 指令发射 摘录
Flet教程之 19 VerticalDivider 分隔符组件 基础入门(教程含源码)
How to connect 5V serial port to 3.3V MCU serial port?
Explore cloud database of cloud services together
随机推荐
OneDNS助力高校行业网络安全
Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt
The Oracle message permission under the local Navicat connection liunx is insufficient
Solve the problem that vscode can only open two tabs
R language uses the quantile function to calculate the quantile of the score value (20%, 40%, 60%, 80%), uses the logical operator to encode the corresponding quantile interval (quantile) into the cla
In depth learning autumn recruitment interview questions collection (1)
禁锢自己的因素,原来有这么多
Matlab implementation of Huffman coding and decoding with GUI interface
《通信软件开发与应用》课程结业报告
Design intelligent weighing system based on Huawei cloud IOT (STM32)
千人規模互聯網公司研發效能成功之路
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
Electron adding SQLite database
[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]
问下flinkcdc2.2.0的版本,支持并发,这个并发是指多并行度吗,现在发现,mysqlcdc全
Tsinghua Yaoban programmers, online marriage was scolded?
There are so many factors that imprison you
Swiftui swift internal skill: five skills of using opaque type in swift
R语言使用magick包的image_mosaic函数和image_flatten函数把多张图片堆叠在一起形成堆叠组合图像(Stack layers on top of each other)
Swiftui swift internal skill how to perform automatic trigonometric function calculation in swift