当前位置:网站首页>LeetCode_ String inversion_ Simple_ 557. Reverse word III in string
LeetCode_ String inversion_ Simple_ 557. Reverse word III in string
2022-07-06 05:25:00 【Old street of small town】
1. 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 There is at least one word in it .
s All words in the are separated by a space .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/reverse-words-in-a-string-iii
2. Ideas
(1) String inversion
3. Code implementation (Java)
// Ideas 1———— String inversion
class Solution {
public String reverseWords(String s) {
StringBuffer res = new StringBuffer();
int length = s.length();
int i = 0;
while (i < length) {
// Record the starting subscript of the current word
int index = i;
// Find the ending subscript of the current word
while (i < length && s.charAt(i) != ' ') {
i++;
}
// Reverse the current word
for (int j = index; j < i; j++) {
res.append(s.charAt(i - j - 1 + index));
}
// Keep spaces between words
while (i < length && s.charAt(i) == ' ') {
i++;
res.append(' ');
}
}
return res.toString();
}
}
边栏推荐
- Graduation design game mall
- 组播和广播的知识点梳理
- 【torch】|torch. nn. utils. clip_ grad_ norm_
- Imperial cms7.5 imitation "D9 download station" software application download website source code
- Golang -- TCP implements concurrency (server and client)
- 01. Project introduction of blog development project
- Vulhub vulnerability recurrence 73_ Webmin
- 趋势前沿 | 达摩院语音 AI 最新技术大全
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- SQLite queries the maximum value and returns the whole row of data
猜你喜欢

JS quick start (II)

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example

Simple understanding of interpreters and compilers

Questions d'examen écrit classiques du pointeur

TCP three handshakes you need to know

Ora-01779: the column corresponding to the non key value saving table cannot be modified

Steady, 35K, byte business data analysis post

Self built DNS server, the client opens the web page slowly, the solution

Pointer classic written test questions
随机推荐
UCF(2022暑期团队赛一)
UCF (2022 summer team competition I)
Solution of QT TCP packet sticking
05. Security of blog project
TCP three handshakes you need to know
04. 项目博客之日志
Summary of deep learning tuning tricks
01. Project introduction of blog development project
C进阶-数据的存储(上)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
C# AES对字符串进行加密
Please wait while Jenkins is getting ready to work
2022半年总结
Nacos TC setup of highly available Seata (02)
指针经典笔试题
[leetcode daily question] number of enclaves
Some common skills on unity inspector are generally used for editor extension or others
flutter 实现一个有加载动画的按钮(loadingButton)
毕业设计游戏商城
nacos-高可用seata之TC搭建(02)