当前位置:网站首页>[daily training -- Tencent selected 50] 557 Reverse word III in string
[daily training -- Tencent selected 50] 557 Reverse word III in string
2022-07-05 08:37:00 【Puppet__】
subject
Given a string s , You need to reverse the character order of each word in the string , Keep the initial order of spaces and words .
Example 1:
Input :s = “Let’s take LeetCode contest”
Output :“s’teL ekat edoCteeL tsetnoc”
Example 2:
Input : s = “God Ding”
Output :“doG gniD”
Tips :
1 <= s.length <= 5 * 104
s Include printable ASCII character .
s Does not contain any beginning or ending spaces .
s in At least There's a word .
s All words in the are separated by a space
Code
package tencent50;
public class leetcode557 {
// Use extra space , And through the spaces in the original string, it will be divided into multiple segments , Flip each segment
public String reverseWords(String s) {
StringBuffer sb = new StringBuffer();
int len = s.length();
int i = 0;
while( i < len){
int start = i;
// First find out where the blank is
while (i < len && s.charAt(i) != ' '){
i++;
}
// Flip word
for (int j = i - 1; j >= start; j--){
sb.append(s.charAt(j));
}
// Skip spaces after
while (i < len && s.charAt(i) == ' '){
i++;
sb.append(' ');
}
}
return sb.toString();
}
public static void main(String[] args) {
leetcode557 obj = new leetcode557();
System.out.println(obj.reverseWords("Let's take LeetCode contest"));
}
}
边栏推荐
- Speech recognition learning summary
- STM32 lights up the 1.8-inch screen under Arduino IDE
- 猜谜语啦(4)
- STM32 virtualization environment of QEMU
- 猜谜语啦(9)
- Business modeling of software model | stakeholders
- Buildroot system for making raspberry pie cm3
- Cmder of win artifact
- Is the security account given by Yixue school safe? Where can I open an account
- STM32 --- serial port communication
猜你喜欢
Various types of questions judged by prime numbers within 100 (C language)
Sword finger offer 09 Implementing queues with two stacks
Array integration initialization (C language)
Guess riddles (10)
TypeScript手把手教程,简单易懂
Typical low code apaas manufacturer cases
MySQL MHA high availability cluster
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
Run菜单解析
Example 010: time to show
随机推荐
Guess riddles (3)
PIP installation
Sword finger offer 06 Print linked list from end to end
轮子1:QCustomPlot初始化模板
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)
实例010:给人看的时间
关于线性稳压器的五个设计细节
Guess riddles (2)
Guess riddles (6)
go依赖注入--google开源库wire
Chapter 18 using work queue manager (1)
Briefly talk about the identification protocol of mobile port -bc1.2
Brief discussion on Buck buck circuit
Sword finger offer 05 Replace spaces
Agile project management of project management
Guess riddles (11)
Bluebridge cup internet of things basic graphic tutorial - GPIO input key control LD5 on and off
Apaas platform of TOP10 abroad
Business modeling of software model | stakeholders
STM32 single chip microcomputer - external interrupt