反转长字符串中某些字符的算法
Algorithm for reversing certain characters in long strings
我目前正在做一道编程题:
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.
我创建了一个程序来解决 60 个测试用例中的前 45 个,但显然在处理非常长的字符串时会崩溃。当输入 999 个字符的字符串时,最后几个字符是无意义的。
我在我的代码中看不出任何可能导致这种情况的错误。任何反馈?简单的解决方案还是构建代码的更好方法?
function reverseArrayOfChars(sArray) {
const length = sArray.length;
let temp;
for (let s = 0; s < length / 2; s++) {
temp = sArray[s];
sArray[s] = sArray[length - 1 - s];
sArray[length - 1 - s] = temp;
}
return sArray;
}
function reverseStr(s, k) {
let sArray = s.split("");
let newArray = []; //Final array to be returned
let tempArray = []; //tempArray is used to store returns from reverseArrayOfChars function. These returns are then concatenated onto newArray.
let switchBoard = 1; //Used to 'switch' between two conditions. Changes automatically every iteration of the loop.
for (let counter = 0; counter < sArray.length; counter += k) {
switchBoard = switchBoard === 0 ? 1 : 0;
if (sArray.length - counter < k) {
tempArray = reverseArrayOfChars(sArray.slice(counter));
newArray = newArray.concat(tempArray);
break;
} else if (sArray.length - counter > k && sArray.length < k * 2) {
tempArray = reverseArrayOfChars(sArray.slice(counter, counter + k));
newArray = newArray.concat(tempArray);
tempArray = sArray.slice(counter + k);
newArray = newArray.concat(tempArray);
break;
} else if (switchBoard === 0) {
tempArray = reverseArrayOfChars(sArray.slice(counter, counter + k));
newArray = newArray.concat(tempArray);
} else if (switchBoard === 1) {
tempArray = sArray.slice(counter, counter + k);
newArray = newArray.concat(tempArray);
}
}
return newArray.join("");
或者,您可以试试这个示例代码:
var reverseStr = function(s, k) {
if (k > s.length)
return s.split('').reverse().join('');
const split = s.split('');
// reverse the seg. then join it back
for (let i = 0; i < s.length; i += 2*k) {
const reverse = split.splice(i, k).reverse();
split.splice(i, 0, ...reverse);
}
return split.join('');
};
我目前正在做一道编程题:
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.
我创建了一个程序来解决 60 个测试用例中的前 45 个,但显然在处理非常长的字符串时会崩溃。当输入 999 个字符的字符串时,最后几个字符是无意义的。
我在我的代码中看不出任何可能导致这种情况的错误。任何反馈?简单的解决方案还是构建代码的更好方法?
function reverseArrayOfChars(sArray) {
const length = sArray.length;
let temp;
for (let s = 0; s < length / 2; s++) {
temp = sArray[s];
sArray[s] = sArray[length - 1 - s];
sArray[length - 1 - s] = temp;
}
return sArray;
}
function reverseStr(s, k) {
let sArray = s.split("");
let newArray = []; //Final array to be returned
let tempArray = []; //tempArray is used to store returns from reverseArrayOfChars function. These returns are then concatenated onto newArray.
let switchBoard = 1; //Used to 'switch' between two conditions. Changes automatically every iteration of the loop.
for (let counter = 0; counter < sArray.length; counter += k) {
switchBoard = switchBoard === 0 ? 1 : 0;
if (sArray.length - counter < k) {
tempArray = reverseArrayOfChars(sArray.slice(counter));
newArray = newArray.concat(tempArray);
break;
} else if (sArray.length - counter > k && sArray.length < k * 2) {
tempArray = reverseArrayOfChars(sArray.slice(counter, counter + k));
newArray = newArray.concat(tempArray);
tempArray = sArray.slice(counter + k);
newArray = newArray.concat(tempArray);
break;
} else if (switchBoard === 0) {
tempArray = reverseArrayOfChars(sArray.slice(counter, counter + k));
newArray = newArray.concat(tempArray);
} else if (switchBoard === 1) {
tempArray = sArray.slice(counter, counter + k);
newArray = newArray.concat(tempArray);
}
}
return newArray.join("");
或者,您可以试试这个示例代码:
var reverseStr = function(s, k) {
if (k > s.length)
return s.split('').reverse().join('');
const split = s.split('');
// reverse the seg. then join it back
for (let i = 0; i < s.length; i += 2*k) {
const reverse = split.splice(i, k).reverse();
split.splice(i, 0, ...reverse);
}
return split.join('');
};