当前位置:网站首页>[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"));
}
}
边栏推荐
- Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
- Search data in geo database
- [noi simulation] juice tree (tree DP)
- STM32 lights up the 1.8-inch screen under Arduino IDE
- Meizu Bluetooth remote control temperature and humidity access homeassistant
- 696. Count binary substring
- How to write cover letter?
- 第十八章 使用工作队列管理器(一)
- Pytorch entry record
- 【日常训练】1200. 最小绝对差
猜你喜欢
How to write cover letter?
Example 004: for the day of the day, enter a day of a month of a year to judge the day of the year?
Guess riddles (6)
Typical low code apaas manufacturer cases
Example 010: time to show
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
图解八道经典指针笔试题
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?
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
整形的分类:short in long longlong
随机推荐
【日常训练】1200. 最小绝对差
猜谜语啦(8)
Example 003: a complete square is an integer. It is a complete square after adding 100, and it is a complete square after adding 168. What is the number?
MySQL之MHA高可用集群
Affected tree (tree DP)
287. Looking for repeats - fast and slow pointer
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
剑指 Offer 09. 用两个栈实现队列
Guess riddles (5)
319. 灯泡开关
The first week of summer vacation
[three tier architecture]
猜谜语啦(6)
Example 005: three numbers sorting input three integers x, y, Z, please output these three numbers from small to large.
整形的分类:short in long longlong
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Arduino+a4988 control stepper motor
[NAS1](2021CVPR)AttentiveNAS: Improving Neural Architecture Search via Attentive Sampling (未完)
图解八道经典指针笔试题
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function