当前位置:网站首页>[LeetCode]186. Flip word II in string
[LeetCode]186. Flip word II in string
2022-06-27 21:50:00 【A Fei algorithm】
subject
Title Description
Given a string , Flip each word in the string one by one .
Example :
Input : ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
Output : ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]
Be careful :
A word is defined as a series of characters that do not contain spaces
The input string will not contain leading or trailing spaces
Words are always separated from each other by a single space
Advanced :
Use O(1) In situ solution of extra space complexity
author : Jun Wang is not in his early days
link :https://www.jianshu.com/p/9d624882d786
source : Simple books
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
Method 1: Flip
public void reverseWords(char[] str) {
int i = 0;
for (int j = 0; j < str.length; j++) {
if (str[j] == ' ') {
reverse(str, i, j);
i = j + 1;
}
}
reverse(str, i, str.length);
System.out.println(String.valueOf(str));
reverse(str, 0, str.length);
}
/** * take ch Of [l, r] Flip , * Be careful ,[l,r) It's left and right * * @param ch * @param l * @param r */
private void reverse(char[] ch, int l, int r) {
for (int k = l; k < (l + r) / 2; k++) {
char tmp = ch[k]; // Location k The elements of
int g = r - 1 - k + l; // Location k The symmetrical position of
ch[k] = ch[g];
ch[g] = tmp;
}
}
边栏推荐
猜你喜欢
随机推荐
[LeetCode]186. 翻转字符串里的单词 II
GBase 8a OLAP分析函数cume_dist的使用样例
Method of reading file contents by Excel
Go从入门到实战——依赖管理(笔记)
动态刷新mapper看过来
Common methods of string class
[LeetCode]动态规划解拆分整数I[Silver Fox]
Go from entry to practice - multiple selection and timeout control (notes)
Codeforces Round #717 (Div. 2)
The difference between scrum and Kanban
100 important knowledge points that SQL must master: sorting and retrieving data
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
ABC-Teleporter Setting-(思维+最短路)
ICML2022 | 可扩展深度高斯马尔可夫随机场
SQL必需掌握的100个重要知识点:组合 WHERE 子句
Process control task
Go从入门到实战——任务的取消(笔记)
Go从入门到实战—— 多路选择和超时控制(笔记)
SQL必需掌握的100个重要知识点:创建计算字段
Codeforces Round #719 (Div. 3)









