当前位置:网站首页>Truncated sentences of leetcode simple questions
Truncated sentences of leetcode simple questions
2022-07-03 04:31:00 【·Starry Sea】
subject
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 , Make the truncated sentence contain only front k Word . return truncation s After the sentence .
Example 1:
Input :s = “Hello how are you Contestant”, k = 4
Output :“Hello how are you”
explain :
s The word in is [“Hello”, “how” “are”, “you”, “Contestant”]
front 4 The first word is [“Hello”, “how”, “are”, “you”]
therefore , Should return “Hello how are you”
Example 2:
Input :s = “What is the solution to this problem”, k = 4
Output :“What is the solution”
explain :
s The word in is [“What”, “is” “the”, “solution”, “to”, “this”, “problem”]
front 4 The first word is [“What”, “is”, “the”, “solution”]
therefore , Should return “What is the solution”
Example 3:
Input :s = “chopper is not a tanuki”, k = 5
Output :“chopper is not a tanuki”
Tips :
1 <= s.length <= 500
k The range of phi is zero [1, s Number of words in ]
s It's only made up of uppercase and lowercase letters and spaces
s Words in are separated by a single space
There are no leading or trailing spaces
source : Power button (LeetCode)
Their thinking
Traversal sentence , Then calculate the number of spaces , If the number of spaces is equal to k, You can cut sentences directly , otherwise k Greater than the number of words in the sentence , Return to the original sentence .
class Solution:
def truncateSentence(self, s: str, k: int) -> str:
count=0
for i in range(len(s)):
if s[i]==' ':
count+=1
if count==k:
break
return s[0:i] if i!=len(s)-1 else s
边栏推荐
- 7. Integrated learning
- Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
- BMZCTF simple_ pop
- Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg
- [set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
- GFS分布式文件系统(光是遇见已经很美好了)
- Xrandr modifier la résolution et le taux de rafraîchissement
- Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
- Kingbasees plug-in KDB of Jincang database_ date_ function
- sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
猜你喜欢
vulnhub HA: Natraj
Pdf editing tool movavi pdfchef 2022 direct download
Smart contract security audit company selection analysis and audit report resources download - domestic article
[dynamic programming] subsequence problem
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully
SSM based campus part-time platform for College Students
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
C language series - Section 3 - functions
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
随机推荐
4 years of experience to interview test development, 10 minutes to end, ask too
Preliminary cognition of C language pointer
[set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
vulnhub HA: Natraj
Basic types of data in TS
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
[Thesis Writing] how to write the overall design of JSP tourism network
[set theory] set concept and relationship (set family | set family examples | multiple sets)
2022-02-13 (347. Top k high frequency elements)
有道云笔记
Redis persistence principle
多板块轮动策略编写技巧----策略编写学习教材
怎么用Kotlin去提高生产力:Kotlin Tips
JVM原理简介
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
A outsourcing boy's mid-2022 summary
Joint set search: merge intervals and ask whether two numbers are in the same set
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills