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

边栏推荐
- Arthas watch grabs a field / attribute of the input parameter
- Matplotlib -- save graph
- How to choose cross-border e-commerce multi merchant system
- stm32逆向入门
- Preliminary cognition of C language pointer
- Factor stock selection scoring model
- 2.14 summary
- After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
- 7. Integrated learning
- 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
猜你喜欢

P35-P41 fourth_ context

X-ray normal based contour rendering

Joint search set: the number of points in connected blocks (the number of points in a set)

Function introduction of member points mall system
![[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius](/img/01/c118725f74e39742df021b5dbcc33b.jpg)
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius

Basic use of continuous integration server Jenkins
![[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries](/img/56/386f0fd6553b8b9711e14c54705ae3.jpg)
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries

FFMpeg filter

How to choose cross-border e-commerce multi merchant system

Introduction of pointer variables in function parameters
随机推荐
Function introduction of member points mall system
Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
金仓数据库KingbaseES 插件kdb_date_function
4 years of experience to interview test development, 10 minutes to end, ask too
Feature_selection
[untitled] 2022 safety production supervisor examination question bank and simulated safety production supervisor examination questions
C Primer Plus Chapter 10, question 14 3 × 5 array
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
FISCO bcos zero knowledge proof Fiat Shamir instance source code
JS multidimensional array to one-dimensional array
2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units
MySQL field userid comma separated save by userid query
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
MC Layer Target
Preliminary cognition of C language pointer
Reptile exercise 03
会员积分商城系统的功能介绍
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
Two drawing interfaces - 1 Matlab style interface