当前位置:网站首页><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])
边栏推荐
- [data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
- 竟然有一半的人不知道 for 与 foreach 的区别???
- [question] Compilation Principle
- Use references
- 禁锢自己的因素,原来有这么多
- The running kubernetes cluster wants to adjust the network segment address of pod
- 千人规模互联网公司研发效能成功之路
- sql里,我想设置外键,为什么出现这个问题
- 通过环境变量将 Pod 信息呈现给容器
- Solve the problem that vscode can only open two tabs
猜你喜欢
Complete collection of common error handling in MySQL installation
[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]
一起探索云服务之云数据库
相机标定(1): 单目相机标定及张正友标定基本原理
The road to success in R & D efficiency of 1000 person Internet companies
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
核舟记(一):当“男妈妈”走进现实,生物科技革命能解放女性吗?
In my limited software testing experience, a full-time summary of automation testing experience
超标量处理器设计 姚永斌 第8章 指令发射 摘录
《通信软件开发与应用》课程结业报告
随机推荐
[full stack plan - programming language C] basic introductory knowledge
Have you ever met flick Oracle CDC, read a table without update operation, and read it repeatedly every ten seconds
超标量处理器设计 姚永斌 第8章 指令发射 摘录
R language uses image of magick package_ Mosaic functions and images_ The flatten function stacks multiple pictures together to form a stack layers on top of each other
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
Automated testing framework
正在運行的Kubernetes集群想要調整Pod的網段地址
About how to install mysql8.0 on the cloud server (Tencent cloud here) and enable local remote connection
深度学习秋招面试题集锦(一)
[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]
Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
Electron adding SQLite database
Programming examples of stm32f1 and stm32subeide -315m super regenerative wireless remote control module drive
[filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation
Use references
sink 消费 到 MySQL, 数据库表里面已经设置了 自增主键, flink 里面,如何 操作?
The Oracle message permission under the local Navicat connection liunx is insufficient
【紋理特征提取】基於matlab局部二值模式LBP圖像紋理特征提取【含Matlab源碼 1931期】
SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
5V串口接3.3V单片机串口怎么搞?