当前位置:网站首页>[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"));
}
}
边栏推荐
- 2022.7.4-----leetcode. one thousand and two hundred
- Chapter 18 using work queue manager (1)
- 2022.7.4-----leetcode.1200
- 剑指 Offer 05. 替换空格
- Guess riddles (7)
- STM32 --- serial port communication
- Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
- 实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
- Affected tree (tree DP)
- Business modeling of software model | overview
猜你喜欢
STM32 lights up the 1.8-inch screen under Arduino IDE
99 multiplication table (C language)
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
Guess riddles (4)
Daily question - input a date and output the day of the year
Business modeling | process of software model
Business modeling of software model | object modeling
MySQL之MHA高可用集群
剑指 Offer 09. 用两个栈实现队列
Example 010: time to show
随机推荐
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
Business modeling of software model | vision
每日一题——替换空格
Classification of plastic surgery: short in long long long
猜谜语啦(3)
Example 006: Fibonacci series
STM32 single chip microcomputer - bit band operation
STM32 outputs 1PPS with adjustable phase
QEMU demo makefile analysis
STM32 virtualization environment of QEMU
Business modeling of software model | overview
【三层架构】
猜谜语啦(8)
实例009:暂停一秒输出
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
猜谜语啦(11)
Matlab tips (28) fuzzy comprehensive evaluation
Go dependency injection -- Google open source library wire
How can fresh students write resumes to attract HR and interviewers
猜谜语啦(4)