We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
922. 按奇偶排序数组II
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
你可以返回任何满足上述条件的数组作为答案。
示例:
输入:[4,2,5,7] 输出:[4,5,2,7] 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。
提示:
The text was updated successfully, but these errors were encountered:
/** * @param {number[]} A * @return {number[]} */ var sortArrayByParityII = function (A) { let j = 1; for (let i = 0; i < A.length; i += 2) { if (A[i] % 2 === 1) { while (A[j] % 2 === 1) { j += 2; } [A[i], A[j]] = [A[j], A[i]]; } } return A; };
Sorry, something went wrong.
/** * @param {number[]} A * @return {number[]} */ var sortArrayByParityII = function(A) { let j = 1; // j 为奇数下标保证值也是奇数 for (let i = 0; i < A.length; i += 2) { // i 为偶数下标保证值也是偶数 if (A[i] % 2 === 1) { while (A[j] % 2 === 1) { // 不用处理越界,取不到为 undefined,再取模为 NaN,不会等于 1 j += 2; } const temp = A[i]; A[i] = A[j]; A[j] = temp; } } return A; };
No branches or pull requests
922. 按奇偶排序数组II
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
你可以返回任何满足上述条件的数组作为答案。
示例:
提示:
The text was updated successfully, but these errors were encountered: