当前位置:网站首页>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();
}
}
边栏推荐
- SQLite queries the maximum value and returns the whole row of data
- [QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
- Drive development - the first helloddk
- Leetcode dynamic planning day 16
- Unity Vector3. Use and calculation principle of reflect
- GAMES202-WebGL中shader的编译和连接(了解向)
- Huawei od computer test question 2
- 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
- Note the various data set acquisition methods of jvxetable
- Vulhub vulnerability recurrence 68_ ThinkPHP
猜你喜欢

Three methods of Oracle two table Association update

图数据库ONgDB Release v-1.0.3

Configuration file converted from Excel to Lua

Talking about the type and function of lens filter

【OSPF 和 ISIS 在多路访问网络中对掩码的要求】

flutter 实现一个有加载动画的按钮(loadingButton)

Check the useful photo lossless magnification software on Apple computer

Fuzzy -- basic application method of AFL

Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?

GAMES202-WebGL中shader的編譯和連接(了解向)
随机推荐
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
Golang -- TCP implements concurrency (server and client)
Vite configures the development environment and production environment
Compilation and connection of shader in games202 webgl (learn from)
Modbus协议通信异常
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
03. 开发博客项目之登录
JS array list actual use summary
HAC集群修改管理员用户密码
剑指 Offer II 039. 直方图最大矩形面积
Pickle and savez_ Compressed compressed volume comparison
图数据库ONgDB Release v-1.0.3
In 2022, we must enter the big factory as soon as possible
[effective Objective-C] - memory management
[mask requirements of OSPF and Isis in multi access network]
C AES encrypts strings
Codeforces Round #804 (Div. 2) Editorial(A-B)
05. 博客项目之安全
Sorting out the knowledge points of multicast and broadcasting
【OSPF 和 ISIS 在多路访问网络中对掩码的要求】