英文:
Can't find a solution for array reverse algorithm problem
问题
I have a requirement where I have to reverse an array without changing the index of '#' presents in an array, like below example:
- Array
[18,-4,'#',0,8,'#',5]
should return[5, 8, "#", 0, -4, "#", 18]
, here numbers should be reversed, excluding '#' while keeping the same index.
I have tried to get the correct output, but it doesn't seem to be correct in all scenarios:
var arr = [18,-4,'#',0,8,'#',5]; // giving result is correct
var arr1 = [18,-4,0,'#',8,'#',5]; // result is not correct
var reverse = function(numbers, start, end){
var temp = numbers[start];
numbers[start] = numbers[end];
numbers[end] = temp;
}
var flip = function(numbers) {
var start = 0;
var end = numbers.length-1;
for(var i=0;i<parseInt(numbers.length/2);i++) {
if(numbers[i] === '#') {
start = i+1;
end = numbers.length - i - i;
reverse(numbers, start, end);
} else if (numbers[numbers.length - i - 1] === '#') {
start = i;
end = numbers.length - i - 2;
reverse(numbers, start, end);
} else {
reverse(numbers, start, end);
}
}
return numbers;
}
var arr = [18,-4,'#',0,8,'#',5];
var arr1 = [18,-4,0,'#',8,'#',5];
console.log(flip(arr));
console.log(flip(arr1));
英文:
I have a requirement where I have to reverse an array without changing the index of '#' presents in an array, like below example:
- Array
[18,-4,'#',0,8,'#',5]
should return[5, 8, "#", 0, -4, "#", 18]
, here numbers should be reversed, excluding '#' while keeping the same index.
I have tried to get the correct output, but it doesn't seem to be correct in all scenarios:
var arr = [18,-4,'#',0,8,'#',5]; // giving result is correct
var arr1 = [18,-4,0,'#',8,'#',5]; // result is not correct
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var reverse = function(numbers, start, end){
var temp = numbers[start];
numbers[start] = numbers[end];
numbers[end] = temp;
}
var flip = function(numbers) {
var start = 0;
var end = numbers.length-1;
for(var i=0;i<parseInt(numbers.length/2);i++) {
if(numbers[i] === '#') {
start = i+1;
end = numbers.length - i - i;
reverse(numbers, start, end);
} else if (numbers[numbers.length - i - 1] === '#') {
start = i;
end = numbers.length - i - 2;
reverse(numbers, start, end);
} else {
reverse(numbers, start, end);
}
}
return numbers;
}
var arr = [18,-4,'#',0,8,'#',5];
var arr1 = [18,-4,0,'#',8,'#',5];
console.log(flip(arr));
console.log(flip(arr1));
<!-- end snippet -->
答案1
得分: 2
你可以简化这个函数,只使用两个索引,起始和结束,检查索引处的值是否应该保持不变,然后选择另一个索引进行交换。
const
swap = (array, a, b) => [array[a], array[b]] = [array[b], array[a]],
flip = numbers => {
var start = 0,
end = numbers.length - 1;
while (start < end) {
if (numbers[start] === '#') {
start++;
continue;
}
if (numbers[end] === '#') {
end--;
continue;
}
swap(numbers, start++, end--);
}
return numbers;
},
array1 = [18, -4, '#', 0, 8, '#', 5],
array2 = [18, -4, 0, '#', 8, '#', 5];
console.log(...flip(array1));
console.log(...flip(array2));
注意:我保留了代码中的特殊字符和标记,如#
,没有进行翻译。
英文:
You could simplify the function and use only two indices, the start and end and check if the value at the indices should stay, then choose another index for swapping.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
swap = (array, a, b) => [array[a], array[b]] = [array[b], array[a]],
flip = numbers => {
var start = 0,
end = numbers.length - 1;
while (start < end) {
if (numbers[start] === '#') {
start++;
continue;
}
if (numbers[end] === '#') {
end--;
continue;
}
swap(numbers, start++, end--);
}
return numbers;
},
array1 = [18, -4, '#', 0, 8, '#', 5],
array2 = [18, -4, 0, '#', 8, '#', 5];
console.log(...flip(array1));
console.log(...flip(array2));
<!-- end snippet -->
答案2
得分: 2
以下是翻译好的部分:
"The trivial approach would be to remove all '#'
's, reverse the array using the built-in [].reverse
method, and then re-insert the '#'
's:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let flip = numbers => {
let removed = numbers.reduce((r, v, i) =>
v === '#' ? r.concat(i) : r
, []);
let reversed = numbers.filter(v => v !== '#').reverse();
removed.forEach(i => reversed.splice(i, 0, '#'));
return reversed;
};
let arr = [18, -4, '#', 0, 8, '#', 5];
let arr1 = [18, -4, 0, '#', 8, '#', 5];
console.log(flip(arr));
console.log(flip(arr1));
<!-- end snippet -->"
请注意,我已经忽略了代码部分,只翻译了您提供的文字。
英文:
The trivial approach would be to remove all '#'
's, reverse the array using the built in [].reverse
method, and then re-insert the '#'
's:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let flip = numbers => {
let removed = numbers.reduce((r, v, i) =>
v === '#' ? r.concat(i) : r
, []);
let reversed = numbers.filter(v => v !== '#').reverse();
removed.forEach(i => reversed.splice(i, 0, '#'));
return reversed;
};
let arr = [18, -4, '#', 0, 8, '#', 5];
let arr1 = [18, -4, 0, '#', 8, '#', 5];
console.log(flip(arr));
console.log(flip(arr1));
<!-- end snippet -->
答案3
得分: 1
你可以尝试这样做:
var numbers = arr.filter(a => a !== '#')
var revArr = [];
arr.forEach((currentValue) => {
if(currentValue !== "#") {
revArr.push(numbers.pop());
} else {
revArr.push("#");
}
});
英文:
You can try this:
var numbers = arr.filter(a => a !== '#')
var revArr = [];
arr.forEach((currentValue) => {
if(currentValue !== "#") {
revArr.push(numbers.pop());
} else {
revArr.push("#");
}
});
答案4
得分: 1
你可以基于两个基本数组来设计你的算法(一个是包含反转数字的数组,另一个是包含保存位置的"#"的数组)。
const array = [5, 8, "#", 0, -4, "#", 18];
function flip(array) {
const arrayNumbers = array.filter((el, index) => el !== "#").reverse();
var counter = 0;
return array.map(el => el === "#").map(el => {
if (!el) {
let num = arrayNumbers[counter];
counter = counter + 1;
return num;
} else {
return "#";
}
})
}
console.log(flip(array));
希望这有帮助。
英文:
You can base your algorithm on two basic array (array of reversed numbers and and an array with "#" saved positions)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const array = [5, 8, "#", 0, -4, "#", 18];
function flip(array) {
const arrayNumbers = array.filter((el, index) => el !== "#").reverse();
var counter = 0;
return array.map(el => el === "#").map(el => {
if (!el) {
let num = arrayNumbers0+网站访问量;
counter = counter + 1;
return num;
} else {
return "#"
}
})
}
console.log(flip(array));
<!-- end snippet -->
答案5
得分: 0
Here's the translated code portion:
var arr = [18, -4, '#', 0, 8, '#', 5];
var stack = [];
for (i = 0; i < arr.length; i++) {
if (arr[i] == '#') continue;
stack.push(arr[i]);
}
for (i = 0; i < arr.length; i++) {
if (arr[i] != '#') {
arr[i] = stack.pop();
}
}
console.log(arr);
Please note that the code has been translated, and I've excluded the additional explanations and comments.
英文:
var arr = [18,-4,'#',0,8,'#',5]
var stack = []
for (i=0 ; i<arr.length; i++) {
if (arr[i] == '#') continue;
stack.push(arr[i]);
}
for (i=0 ; i<arr.length; i++) {
if (arr[i] != '#') {
arr[i] = stack.pop();
}
}
console.log(arr)
The above code should solve your problem.
<br>
The implementation uses stack where we keep inserting elements into stack untill we see a '#' and skip it.
While creating the output array we refer the original array for '#' index and stack for reverse index.
答案6
得分: 0
这部分的代码可以这样做:
int end = v.length - 1;
int start = 0;
for (int i = 0; i < v.length >> 1; i++) {
if (v[start].equals("#")) {
start++;
continue;
}
if (v[end].equals("#")) {
end--;
continue;
}
Object temp = v[end];
v[end] = v[start];
v[start] = temp;
end--;
start++;
}
System.out.println(Arrays.toString(v));
英文:
You can do it like this.
int end = v.length - 1;
int start = 0;
for (int i = 0; i < v.length >> 1; i++) {
if (v[start].equals("#")) {
start++;
continue;
}
if (v[end].equals("#")) {
end--;
continue;
}
Object temp = v[end];
v[end] = v[start];
v[start] = temp;
end--;
start++;
}
System.out.println(Arrays.toString(v));
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论