-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
图解字节&leetcode151:翻转字符串里的单词 #18
Comments
function reverseStr(str) {
return str.match(/[\w!,]+/g).reverse().join(" ");
} |
方法一
方法二
|
解法一:正则 + JS APIvar reverseWords = function(s) {
return s.trim().replace(/\s+/g, ' ').split(' ').reverse().join(' ')
}; 解法二:双端队列(不使用 API)双端队列,故名思义就是两端都可以进队的队列 解题思路:
画图理解: 代码实现: var reverseWords = function(s) {
let left = 0
let right = s.length - 1
let queue = []
let word = ''
while (s.charAt(left) === ' ') left ++
while (s.charAt(right) === ' ') right --
while (left <= right) {
let char = s.charAt(left)
if (char === ' ' && word) {
queue.unshift(word)
word = ''
} else if (char !== ' '){
word += char
}
left++
}
queue.unshift(word)
return queue.join(' ')
}; |
@WeCheung: function reverseStr(str) {
return str.match(/[\w!,]+/g).reverse().join(" ");
} 你这种写法有问题,一是匹配的字符有限( |
|
function reverseString(params) { |
希望增加inplace的做法 |
|
/**
* 翻转字符串里的单词
* @param {*} str
* @returns
*/
function reverseWords(str) {
// 方法1:reverse
// return str.split(' ').reverse().filter(Boolean).join(' ');
// 方法2:手动翻转
const strArr = str.split(' ').filter(Boolean);
const newArr = [];
let len = strArr.length;
while (len-- > 0) {
newArr.push(strArr[len]);
}
return newArr.join(' ');
}
console.log(reverseWords('hello word!')); // word! hello
console.log(reverseWords('the sky is blue')); // blue is sky the |
官方题解,他不香吗? var reverseWords = function(s) {
return s.trim().split(/\s+/).reverse().join(' ');
}; |
如果在第一种方法执行前进行一个判断会更加高效 |
const reverseWords = str => str.split(' ').filter(Boolean).reverse().join(' ') |
str.replace(/ +/g,' ').trim().split(' ').reverse().join(' ') |
解法一确实可以优化一下
var reverseWords = function(s) {
return s.trim().replace(/\s+/g, ' ').split(' ').reverse().join(' ')
};
to
var reverseWords = function(s) {
return s.trim().split(/\s+/).reverse().join(' ')
}; |
const reverseWords = str => str.split(/\s+/).filter(Boolean).reverse().join(' ') |
|
// 需要处理单词间多个空格
const reverseWords = str => str.trim().split(/\s+/).reverse().join(' ') |
// 使用正则表达式,将连续空格替换为单个空格
var reverseWords = function(s) {
return s.trim().replace(/\s+/g, ' ').split(' ').reverse().join(' ')
// return s.trim().split(/\s+/g).reverse().join(' ')
};
// 使用双端队列
var reverseWords = function(s) {
let left = 0, right = s.length - 1
const deque = []
let word = ''
while (s[left] === ' ') left++
while (s[right] === ' ') right--
while (left <= right) {
const c = s[left]
if (c === ' ' && word) {
deque.unshift(word)
word = ''
} else if (c !== ' ') {
word += c
}
left++
}
deque.unshift(word)
return deque.join(' ')
};
// 不使用双端队列
var reverseWords = function(s) {
let res = ''
let word = ''
s = s.trim()
for (let i = s.length - 1; i >= 0; i--) {
if (s[i] !== ' ') {
word = s[i] + word
} else if (s[i] === ' ' && word) {
res += word + ' '
word = ''
}
}
return res + word
}; |
var reverseWords = function (s) {
let trimStr = s.trim();
let word = '';
let strQueue = [];
for (let i = 0; i < trimStr.length; i++) {
if (trimStr[i] === ' ' && word) {
strQueue.unshift(word);
word = '';
} else {
word += trimStr[i];
}
word = word.trim();
}
word && strQueue.unshift(word);
return strQueue.join(' ');
}; |
var reverse = function (sts) {
return sts.trim().split(" ").reverse().join(" ")
} |
给定一个字符串,逐个翻转字符串中的每个单词。
示例 1:
示例 2:
示例 3:
说明:
附赠姚老板的 :JavaScript正则表达式迷你书(1.1版).pdf
leetcode
The text was updated successfully, but these errors were encountered: