题解 | #调整数组顺序使奇数位于偶数前面(二)#
调整数组顺序使奇数位于偶数前面(二)
https://www.nowcoder.com/practice/0c1b486d987b4269b398fee374584fc8
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
export function reOrderArrayTwo(array: number[]): number[] {
// write code here
if (array.length === 0) return array
let evenIndex = 0
while (array[evenIndex] % 2 !== 0) {
evenIndex++
if (evenIndex >= array.length) return array
}
let oddIndex = evenIndex + 1
while (oddIndex < array.length) {
while (array[oddIndex] % 2 === 0) {
oddIndex++
if (oddIndex >= array.length) return array
}
const currentOddTemp = array[oddIndex]
for (let i = oddIndex; i > evenIndex; i--) {
array[i] = array[i - 1]
}
array[evenIndex] = currentOddTemp
evenIndex++
oddIndex++
}
return array
}
海康威视公司福利 1160人发布