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

边栏推荐
- Xrandr modify resolution and refresh rate
- Joint search set: the number of points in connected blocks (the number of points in a set)
- Crazy scientist
- [fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
- Interface in TS
- C primre plus Chapter 10 question 6 inverted array
- JS multidimensional array to one-dimensional array
- 使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
- [set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
- [free completion] development of course guidance platform (source code +lunwen)
猜你喜欢

跨境电商多商户系统怎么选

data2vec! New milestone of unified mode

2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills

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

Php+mysql registration landing page development complete code
![[fxcg] market analysis today](/img/ac/294368e3496a5b808b38833053ee81.jpg)
[fxcg] market analysis today

Prefix and (continuously updated)

What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets

Fcpx template: sweet memory electronic photo album photo display animation beautiful memory

stm32逆向入门
随机推荐
220214c language learning diary
How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
Xrandr modifier la résolution et le taux de rafraîchissement
[set theory] inclusion exclusion principle (including examples of exclusion principle)
Two points -leetcode-540 A single element in an ordered array
Kubernetes source code analysis (I)
Prefix and (continuously updated)
Square root of X
Kubernetes源码分析(一)
After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
Dive Into Deep Learning——2.1数据操作&&练习
2022-02-14 (394. String decoding)
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
BMZCTF simple_ pop
Which code editor is easy to use? Code editing software recommendation
多板块轮动策略编写技巧----策略编写学习教材
redis 持久化原理
Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
A outsourcing boy's mid-2022 summary