当前位置:网站首页>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();
}
}
边栏推荐
- Rce code and Command Execution Vulnerability
- idea一键导包
- 03. 开发博客项目之登录
- Note the various data set acquisition methods of jvxetable
- Safe mode on Windows
- Codeforces Round #804 (Div. 2) Editorial(A-B)
- 【OSPF 和 ISIS 在多路访问网络中对掩码的要求】
- Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
- Oracle query table index, unique constraint, field
- MySQL time processing
猜你喜欢
Yyds dry inventory SSH Remote Connection introduction
剑指 Offer II 039. 直方图最大矩形面积
Hyperledger Fabric2. Some basic concepts of X (1)
Imperial cms7.5 imitation "D9 download station" software application download website source code
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
05. Security of blog project
Cuda11.1 online installation
GAMES202-WebGL中shader的編譯和連接(了解向)
Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
Implementing fuzzy query with dataframe
随机推荐
关于Unity Inspector上的一些常用技巧,一般用于编辑器扩展或者其他
[detailed explanation of Huawei machine test] statistics of shooting competition results
[effective Objective-C] - memory management
UCF (2022 summer team competition I)
Rce code and Command Execution Vulnerability
關於Unity Inspector上的一些常用技巧,一般用於編輯器擴展或者其他
02. 开发博客项目之数据存储
HAC cluster modifying administrator user password
JS quick start (II)
Golang -- TCP implements concurrency (server and client)
pix2pix:使用条件对抗网络的图像到图像转换
Oracle deletes duplicate data, leaving only one
Nacos TC setup of highly available Seata (02)
Modbus protocol communication exception
Fluent implements a loadingbutton with loading animation
Summary of redis basic knowledge points
Please wait while Jenkins is getting ready to work
First acquaintance with CDN
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
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)