当前位置:网站首页>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

边栏推荐
- Ffmpeg tanscoding transcoding
- 消息队列(MQ)介绍
- Priv app permission exception
- 金仓数据库KingbaseES 插件kdb_exists_expand
- [pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
- 2.14 summary
- SSM based campus part-time platform for College Students
- [no title] 2022 chlorination process examination content and free chlorination process examination questions
- 跨境电商多商户系统怎么选
- How to use kotlin to improve productivity: kotlin tips
猜你喜欢

X-ray normal based contour rendering

540. Single element in ordered array

Pyqt control part (II)

Two points -leetcode-540 A single element in an ordered array

When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error

Integration of Android high-frequency interview questions (including reference answers)

Preliminary cognition of C language pointer
![[Thesis Writing] how to write the overall design of JSP tourism network](/img/02/841e8870c2ef871c182b9bb8252a83.jpg)
[Thesis Writing] how to write the overall design of JSP tourism network
![[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN](/img/7e/50fa6f65b5a4f0bb60909f57daff56.png)
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN

Asp access teaching management system design finished product
随机推荐
使用BENCHMARKSQL工具对kingbaseES执行灌数据提示无法找到JDBC driver
C primre plus Chapter 10 question 6 inverted array
Kingbasees plug-in KDB of Jincang database_ exists_ expand
Jincang KFS data bidirectional synchronization scenario deployment
Library management system based on SSM
Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg
A outsourcing boy's mid-2022 summary
How to use kotlin to improve productivity: kotlin tips
C Primer Plus Chapter 10, question 14 3 × 5 array
[set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended
How do you use lodash linking function- How do you chain functions using lodash?
After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
Matplotlib -- save graph
7. Integrated learning
X-ray normal based contour rendering
xrandr修改分辨率與刷新率
Xrandr modify resolution and refresh rate
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional