Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

186. Reverse Words in a String II

Posted on 2020-05-24 | Edited on 2021-01-22

https://leetcode.com/problems/reverse-words-in-a-string-ii/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
private void reverse(char[] s,int l,int r){
while(l<r){
char tmp=s[r];
s[r--]=s[l];
s[l++]=tmp;
// l++;
// r--;
}
}
public void reverseWords(char[] s) {
if(s==null||s.length==0) return;
int l=0,r=s.length-1;
reverse(s,l,r);

int j=0;
for(int i=0;i<s.length;i=j+1){
j=i;
while(j<s.length&&s[j]!=' ') j++;
reverse(s,i,j-1);
}
}
}
<1…194195196…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%