当前位置:网站首页>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();
}
}
边栏推荐
- C AES encrypts strings
- Oracle deletes duplicate data, leaving only one
- JS quick start (II)
- Algorithm -- climbing stairs (kotlin)
- Modbus协议通信异常
- [detailed explanation of Huawei machine test] statistics of shooting competition results
- Review of double pointer problems
- First acquaintance with CDN
- 2022半年总结
- Zoom and pan image in Photoshop 2022
猜你喜欢

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

【LeetCode】18、四数之和

指针经典笔试题

01. 开发博客项目之项目介绍

Yyds dry inventory SSH Remote Connection introduction

初识CDN

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

Vite configures the development environment and production environment

F12 solve the problem that web pages cannot be copied

Fluent implements a loadingbutton with loading animation
随机推荐
Quelques conseils communs sur l'inspecteur de l'unit é, généralement pour les extensions d'éditeur ou d'autres
【云原生】3.1 Kubernetes平台安装KubeSpher
指針經典筆試題
Configuration file converted from Excel to Lua
Collection + interview questions
Oracle deletes duplicate data, leaving only one
[leetcode daily question] number of enclaves
Unity Vector3. Use and calculation principle of reflect
Codeforces Round #804 (Div. 2) Editorial(A-B)
flutter 实现一个有加载动画的按钮(loadingButton)
算法-- 爬楼梯(Kotlin)
Rce code and Command Execution Vulnerability
Summary of deep learning tuning tricks
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
RT thread analysis log system RT_ Kprintf analysis
Leetcode dynamic planning day 16
[detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
注释、接续、转义等符号
05. 博客项目之安全
Promotion hung up! The leader said it wasn't my poor skills