🌐
AlgoMonster
algo.monster › liteproblems › 186
186. Reverse Words in a String II - In-Depth Explanation
In-depth solution and explanation for LeetCode 186. Reverse Words in a String II in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
🌐
LeetCode
leetcode.com › problems › reverse-words-in-a-string-ii
Reverse Words in a String II - LeetCode
Can you solve this real interview question? Reverse Words in a String II - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
🌐
Medium
medium.com › @kanghui-liu › leetcode-186-reverse-words-in-a-string-ii-m-17bc726be94c
Leetcode 186: Reverse Words in a String II (M) | by BigRabbitData ...
January 21, 2025 - ... Input: s = ["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"] ... If we reverse the orginal input, then reverse each word, we would be able to get the required ...
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 0100-0199 › 0186.Reverse Words in a String II › README_EN.md
leetcode/solution/0100-0199/0186.Reverse Words in a String II/README_EN.md at main · doocs/leetcode
Given a character array s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by a single space. Your code must solve the problem in-place, i.e. without allocating extra space.
Author   doocs
🌐
Gitbooks
just4once.gitbooks.io › leetcode-notes › content › leetcode › string › 186-reverse-words-in-a-string-ii.html
186-reverse-words-in-a-string-ii · Leetcode Notes
class Solution { public void reverseWords(char[] str) { int n = str.length; reverse(str, 0, n - 1); int start = 0, end = 0; while (end < n) { if (str[end] == ' ') { reverse(str, start, end - 1); start = end + 1; } end++; } reverse(str, start, end - 1); } private void reverse(char[] str, int i, int j) { while (i < j) { char tmp = str[i]; str[i] = str[j]; str[j] = tmp; i++; j--; } } }
🌐
GitBooks
cheonhyangzhang.gitbooks.io › leetcode-solutions › content › 186-reverse-words-in-a-string-ii-medium.html
186 Reverse Words in a String II – Medium · LeetCode solutions
The solution is a little tricky, the idea is to reverse the whole string first, so “the sky is blue” becomes “eulb si yks eht”. This will make all the space in this string to be in the expected position. Next we need to reverse each word, so that it becomes “blue is sky the”.
🌐
LeetCode
leetcode.ca › all › 186.html
Leetcode 186. Reverse Words in a String II
Given an input string , reverse the string word by word.
🌐
AlgoMap
algomap.io › question-bank › reverse-words-in-a-string-ii
Reverse Words in a String II
The key to solving the "Reverse Words in a String II" problem in-place is to use the two-step reverse technique: first reverse the whole array to get the words in the correct order, then reverse each word to restore their original spelling.
Find elsewhere
🌐
Leetcode
leetcode.ca › 2016-06-03-186-Reverse-Words-in-a-String-II
186 - Reverse Words in a String II | Leetcode
June 3, 2016 - All the words in s are guaranteed to be separated by a single space. ... class Solution { public void reverseWords(char[] s) { int n = s.length; for (int i = 0, j = 0; j < n; ++j) { if (s[j] == ' ') { reverse(s, i, j - 1); i = j + 1; } else if (j == n - 1) { reverse(s, i, j); } } reverse(s, 0, n - 1); } private void reverse(char[] s, int i, int j) { for (; i < j; ++i, --j) { char t = s[i]; s[i] = s[j]; s[j] = t; } } }
🌐
Spark Code Hub
sparkcodehub.com › leetcode › 186 › reverse-words-in-a-string-ii
LeetCode 186: Reverse Words in a String II Solution in Python Explained
Reversing words in a string in-place ... challenge that makes it intriguing! Given a character array s and its length, you need to reverse the order of words in-place, using only O(1) extra space, modifying the original array....
🌐
Blogger
massivealgorithms.blogspot.com › 2016 › 04 › leetcode-186-reverse-words-in-string-ii.html
Massive Algorithms: Leetcode 186 - Reverse Words in a String II
public void reverseWords(char[] s){ reverseWords(s,0,s.length-1); for(int i = 0, j = 0;i <= s.length;i++){ if(i==s.length || s[i] == ' '){ reverseWords(s,j,i-1); j = i+1; } } } private void reverseWords(char[] s, int begin, int end){ while(begin < end){ char c = s[begin]; s[begin] = s[end]; s[end] = c; begin++; end--; } } ... LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode -
🌐
Hbamithkumara
code-snippets.hbamithkumara.com › leetcode › problems › 101-200 › reverse-words-in-a-string-ii
186. Reverse Words in a String II | Code Snippets
*/ class Solution { public void reverseWords(char[] s) { if(s == null || s.length == 0) return; // reverse the whole string reverse(s, 0, s.length-1); // reverse each word reverseEachWord(s); } private void reverse(char[] chars, int left, int right) { while(left < right) { char tmp = chars[left]; chars[left++] = chars[right]; chars[right--] = tmp; } } private void reverseEachWord(char[] chars) { int n = chars.length; int start = 0, end = 0; while(start < n) { // go to the end of the word while(end < n && chars[end] != ' ') { end++; } // reverse the word reverse(chars, start, end-1); // move to the next word end++; start = end; } } }
🌐
Lixinchengdu
lixinchengdu.github.io › algorithmbook › leetcode › reverse-words-in-a-string-ii.html
186.Reverse Words in a String II · GitBook
The input string does not contain leading or trailing spaces. The words are always separated by a single space. Follow up: Could you do it in-place without allocating extra space? class Solution { public: void reverseWords(vector<char>& s) { int start = 0; int end = 0; while (start < s.size()) { end = start + 1; while (end < s.size() && s[end] != ' ') ++end; reverse(s, start, end - 1); start = end + 1; } reverse(s, 0, s.size() - 1); } void reverse(vector<char>& s, int start, int end) { while (start < end) { swap(s[start], s[end]); ++start; --end; } } };
🌐
Hello Interview
hellointerview.com › community › questions › reverse-words-2 › cm5eh7nrh04s6838o2jtsj6ri
Leetcode 186. Reverse Words in a String II | Hello Interview
Leetcode 186. Reverse Words in a String II coding interview question. Given a character array representing a sentence with words separated by spaces, reverse the order of...
🌐
GitHub
github.com › grandyang › leetcode › issues › 186
[LeetCode] 186. Reverse Words in a String II · Issue #186 · grandyang/leetcode
November 19, 2018 - Given an input string __ , reverse the string word by word. 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"] Note: A word is defined as a ...
Published   May 30, 2019
🌐
Baihuqian
baihuqian.github.io › 2018-07-03-reverse-words-in-a-string-ii
Leetcode 186: Reverse Words in a String II
July 4, 2018 - 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"] Note: A word is defined as a sequence of non-space characters.
🌐
Grandyang's Blogs
grandyang.com › leetcode › 186
186. Reverse Words in a String II | Grandyang's Blogs
October 28, 2023 - The input string does not contain leading or trailing spaces. The words are always separated by a single space. Follow up: Could you do it in-place without allocating extra space? 这道题让我们翻转一个字符串中的单词,跟之前那题 Reverse Words in a String 没有区别,由于之前那道题就是用 in-place 的方法做的,而这道题反而更简化了题目,因为不考虑首尾空格了和单词之间的多空格了,方法还是很简单,先把每个单词翻转一遍,再把整个字符串翻转一遍,或者也可以调换个顺序,先翻转整个字符串,再翻转每个单词,参见代码如下: