LeetCode刷题 Day08
344. Reverse String
Write a function that reverses a string. The input string is given as an array of characters s.
You must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
思路:
- 两侧双指针
- 交换之后 左右指针各移动一步
代码:
/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
let l = 0;
let r = s.length - 1;
while (l < r) {
let temp = s[l];
s[l] = s[r];
s[r] = temp;
l++;
r--;
}
return s;
};
时间复杂度: O(n) 空间复杂度: O(1)
541. Reverse String II
Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.
If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.
Example 1:
Input: s = "abcdefg", k = 2
Output: "bacdfeg"
Example 2:
Input: s = "abcd", k = 2
Output: "bacd"
思路:
- 类似于上一题 (344), 只是外部套上一个for loop, for loop的 step为 2k。
代码:
/**
* @param {string} s
* @param {number} k
* @return {string}
*/
var reverseStr = function(s, k) {
s = s.split("");
for (let i = 0; i < s.length;) {
let l = i;
let r = i + k - 1;
while (l < r) {
const temp = s[l];
s[l] = s[r];
s[r] = temp;
l++;
r--;
}
i += 2*k
}
return s.join("");
};
时间复杂度: O(n2) 空间复杂度: O(n)
剑指 Offer 05. 替换空格
难度简单357收藏分享切换为英文接收动态反馈
请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
示例 1:
输入: s = "We are happy."
输出: "We%20are%20happy."
代码:
/**
* @param {string} s
* @return {string}
*/
var replaceSpace = function(s) {
s = s.split("");
for (let i = 0; i < s.length; i++) {
s[i] = s[i] === " " ? "%20" : s[i];
}
return s.join("");
};
时间复杂度: O(n) 空间复杂度: O(n)
151. Reverse Words in a String
Given an input string 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 at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
Example 1:
Input: s = "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: s = " hello world "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: s = "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
思路:
- 转换为数组并滤除空格
- 两侧双指针交换法
/**
* @param {string} s
* @return {string}
*/
var reverseWords = function(s) {
let l = 0;
let r = s.length - 1;
s = s.split(" ").filter(s => s.length > 0);
while (l < r) {
const temp = s[l];
s[l] = s[r];
s[r] = temp;
l++;
r--;
}
return s.join(" ");
};
时间复杂度: O(n) 空间复杂度: O(n)
剑指 Offer 58 - II. 左旋转字符串
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。
示例 1:
输入: s = "abcdefg", k = 2
输出: "cdefgab"
示例 2:
输入: s = "lrloseumgh", k = 6
输出: "umghlrlose"
思路1: 利用js的slice直接整体翻转,这样就等于copy 2两次,并返回一个新字符串
代码:
/**
* @param {string} s
* @param {number} n
* @return {string}
*/
var reverseLeftWords = function(s, n) {
return s.slice(n) + s.slice(0, n);
};
时间复杂度:O(n) 空间复杂度:O(n)
思路2:
- 首先前半段翻转
- 然后后半段翻转
- 最后整体翻转
代码:
/**
* @param {string} s
* @param {number} n
* @return {string}
*/
var reverseLeftWords = function(s, n) {
s = s.split("");
reverse(0, n - 1, s);
reverse(n, s.length - 1, s);
reverse(0, s.length - 1, s);
return s.join("");
};
var reverse = function(l, r, arr) {
while (l < r) {
const temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
l++;
r--;
}
return arr;
}
时间复杂度:O(n) 空间复杂度:O(n),因为split另allocate了空间