当前位置:网站首页>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();
}
}
边栏推荐
- Nacos - TC Construction of High available seata (02)
- [detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
- Self built DNS server, the client opens the web page slowly, the solution
- 01. 开发博客项目之项目介绍
- C Advanced - data storage (Part 1)
- Modbus协议通信异常
- Three. JS learning - light and shadow (understanding)
- Algorithm -- climbing stairs (kotlin)
- 指針經典筆試題
- 關於Unity Inspector上的一些常用技巧,一般用於編輯器擴展或者其他
猜你喜欢
【torch】|torch. nn. utils. clip_ grad_ norm_
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
Vulhub vulnerability recurrence 67_ Supervisor
05. 博客项目之安全
Configuration file converted from Excel to Lua
C进阶-数据的存储(上)
02. Develop data storage of blog project
03. Login of development blog project
注释、接续、转义等符号
剑指 Offer II 039. 直方图最大矩形面积
随机推荐
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
UCF(暑期团队赛二)
EditorUtility. The role and application of setdirty in untiy
[mask requirements of OSPF and Isis in multi access network]
趋势前沿 | 达摩院语音 AI 最新技术大全
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
Figure database ongdb release v-1.0.3
First acquaintance with CDN
2022半年总结
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
UCF (summer team competition II)
05. 博客项目之安全
F12 solve the problem that web pages cannot be copied
Fiddler installed the certificate, or prompted that the certificate is invalid
Vulhub vulnerability recurrence 67_ Supervisor
Jvxetable implant j-popup with slot
[leetcode] 18. Sum of four numbers
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
【torch】|torch. nn. utils. clip_ grad_ norm_
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code