当前位置:网站首页>[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"));
}
}
边栏推荐
猜你喜欢

Run menu analysis

Matlab tips (28) fuzzy comprehensive evaluation

Example 002: the bonus paid by the "individual income tax calculation" enterprise is based on the profit commission. When the profit (I) is less than or equal to 100000 yuan, the bonus can be increase

319. Bulb switch

Classification of plastic surgery: short in long long long
![[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)](/img/3b/c94b8466370f4461875c85b4f66860.png)
[nas1] (2021cvpr) attentivenas: improving neural architecture search via attentive sampling (unfinished)

Negative pressure generation of buck-boost circuit

Business modeling of software model | stakeholders

实例010:给人看的时间

实例003:完全平方数 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
随机推荐
暑假第一周
Business modeling of software model | object modeling
Run菜单解析
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
图解八道经典指针笔试题
Business modeling | process of software model
Arduino burning program and Arduino burning bootloader
STM32 --- NVIC interrupt
Run menu analysis
99 multiplication table (C language)
Installation and use of libjpeg and ligpng
STM32 virtualization environment of QEMU
Void* C is a carrier for realizing polymorphism
How to manage the performance of R & D team?
696. 计数二进制子串
Lori remote control commissioning record
GEO数据库中搜索数据
猜谜语啦(2)
每日一题——输入一个日期,输出它是该年的第几天
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。