日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 知識百科

調整數組元素順序,你了解幾分?

來源: 責編: 時間:2023-08-07 16:30:11 245觀看
導讀 前言有一個整數數組,我們想按照特定規則對數組中的元素進行排序,比如:數組中的所有奇數位于數組的前半部分。實現思路我們通過一個實例來分析下:假設有這樣一個數組:[2, 4, 5, 6,

前言I8l28資訊網——每日最新資訊28at.com

有一個整數數組,我們想按照特定規則對數組中的元素進行排序,比如:數組中的所有奇數位于數組的前半部分。I8l28資訊網——每日最新資訊28at.com

實現思路I8l28資訊網——每日最新資訊28at.com

我們通過一個實例來分析下:假設有這樣一個數組:[2, 4, 5, 6, 7, 8, 9, 11],將奇數移動到最前面后,就是:[11, 9, 5, 7, 6, 8, 4, 2]。I8l28資訊網——每日最新資訊28at.com

通過觀察后,我們發現在掃描這個數組的時候,如果發現有偶數出現在奇數的前面, 就交換他們的順序,交換之后就符合要求了。I8l28資訊網——每日最新資訊28at.com

因此,我們可以維護兩個指針:I8l28資訊網——每日最新資訊28at.com

第一個指針初始化時指向數組的第一個數字,它只向后移動;第二個指針初始化時指向數組的最后一個數字,它只向前移動;I8l28資訊網——每日最新資訊28at.com

在兩個指針相遇之前,第一個指針總是位于第二個指針的前面。如果第一個指針指向的數字是偶數,并且第二個指針指向的數字是奇數,則交換這兩個數字。I8l28資訊網——每日最新資訊28at.com

接下來,我們來通過圖來描述下上述例子交換指針的過程,如下所示:I8l28資訊網——每日最新資訊28at.com

第一個指針永遠指向偶數,如果不為偶數就向后移動;第二個指針永遠指向奇數,如果不為奇數就向前移動;當兩個指針各自指向的數都符合條件時,就交換兩個元素的位置;交換完成后,重復上述步驟,直至兩個指針相遇或者第一個指針位于第二個指針之后則代表問題已得到解決。I8l28資訊網——每日最新資訊28at.com

I8l28資訊網——每日最新資訊28at.com

實現代碼I8l28資訊網——每日最新資訊28at.com

有了思路之后,我們來看下實現代碼,如下所示:I8l28資訊網——每日最新資訊28at.com

export class AdjustArrayOrder {I8l28資訊網——每日最新資訊28at.com
// 指向數組元素的兩個指針:一個指向數組頭部、一個指向數組尾部I8l28資訊網——每日最新資訊28at.com
private begin = 0;I8l28資訊網——每日最新資訊28at.com
private end = 0;I8l28資訊網——每日最新資訊28at.com
I8l28資訊網——每日最新資訊28at.com
// 調整數組中奇數與偶數元素的位置:奇數位于偶數前面I8l28資訊網——每日最新資訊28at.com
reorderOddEven(arr: Array): void {I8l28資訊網——每日最新資訊28at.com
this.end = arr.length - 1;I8l28資訊網——每日最新資訊28at.com
while (this.begin < this.end) {I8l28資訊網——每日最新資訊28at.com
// 向后移動begin(轉成二進制跟1做與運算,運算結果為0就表示為偶數),直至其指向偶數I8l28資訊網——每日最新資訊28at.com
while (this.begin < this.end && (arr[this.begin] & 0x1) !== 0) {I8l28資訊網——每日最新資訊28at.com
this.begin++;I8l28資訊網——每日最新資訊28at.com
}I8l28資訊網——每日最新資訊28at.com
I8l28資訊網——每日最新資訊28at.com
// 向前移動end(轉成二進制跟1做與運算,運算結果為1就表示為奇數),直至其指向奇數I8l28資訊網——每日最新資訊28at.com
while (this.begin < this.end && (arr[this.end] & 0x1) === 0) {I8l28資訊網——每日最新資訊28at.com
this.end--;I8l28資訊網——每日最新資訊28at.com
}I8l28資訊網——每日最新資訊28at.com
I8l28資訊網——每日最新資訊28at.com
// begin指向了偶數,end指向了奇數I8l28資訊網——每日最新資訊28at.com
if (this.begin < this.end) {I8l28資訊網——每日最新資訊28at.com
// 交換兩個元素的順序I8l28資訊網——每日最新資訊28at.com
[arr[this.begin], arr[this.end]] = [arr[this.end], arr[this.begin]];I8l28資訊網——每日最新資訊28at.com
}I8l28資訊網——每日最新資訊28at.com
}I8l28資訊網——每日最新資訊28at.com
// 重置指針位置I8l28資訊網——每日最新資訊28at.com
this.begin = 0;I8l28資訊網——每日最新資訊28at.com
this.end = 0;I8l28資訊網——每日最新資訊28at.com
}I8l28資訊網——每日最新資訊28at.com
}代碼的可擴展I8l28資訊網——每日最新資訊28at.com

性如果數組中的元素不按照奇前偶后排列,我們需要將其按照大小進行劃分,所有負數都排在非負數的前面,應該怎么做?I8l28資訊網——每日最新資訊28at.com

聰明的開發者可能已經想到了方案:雙指針的思路還是不變,我們只需修改內層while循環的的判斷條件即可。I8l28資訊網——每日最新資訊28at.com

這樣回答沒有問題,確實解決了這個問題,那么如果再改改題目,我們需要把數組中的元素分為兩部分,能被3整除的數都在不能被3整除的數前面,應該怎么做?I8l28資訊網——每日最新資訊28at.com

經過思考后,我們發現這個問題無論再怎么改變都有一個共同的部分:雙指針的邏輯永遠不會變。變化的只是判斷條件,那么我們就可以把變化的部分提取成函數,當作參數讓調用者傳進來,這樣就完美的解決了這個問題,也正是我們所提及的代碼的可擴展性。I8l28資訊網——每日最新資訊28at.com

最后,我們來看下實現代碼,如下所示:I8l28資訊網——每日最新資訊28at.com

// 元素排序I8l28資訊網——每日最新資訊28at.com
reorder(arr: Array, checkFun: (checkVal: number) => boolean): void {I8l28資訊網——每日最新資訊28at.com
this.end = arr.length - 1;I8l28資訊網——每日最新資訊28at.com
while (this.begin < this.end) {I8l28資訊網——每日最新資訊28at.com
// 向后移動beginI8l28資訊網——每日最新資訊28at.com
while (this.begin < this.end && !checkFun(arr[this.begin])) {I8l28資訊網——每日最新資訊28at.com
this.begin++;I8l28資訊網——每日最新資訊28at.com
}I8l28資訊網——每日最新資訊28at.com
I8l28資訊網——每日最新資訊28at.com
// 向前移動endI8l28資訊網——每日最新資訊28at.com
while (this.begin < this.end && checkFun(arr[this.end])) {I8l28資訊網——每日最新資訊28at.com
this.end--;I8l28資訊網——每日最新資訊28at.com
}I8l28資訊網——每日最新資訊28at.com
I8l28資訊網——每日最新資訊28at.com
// begin與end都指向了正確的位置I8l28資訊網——每日最新資訊28at.com
if (this.begin < this.end) {I8l28資訊網——每日最新資訊28at.com
// 交換兩個元素的順序I8l28資訊網——每日最新資訊28at.com
[arr[this.begin], arr[this.end]] = [arr[this.end], arr[this.begin]];I8l28資訊網——每日最新資訊28at.com
}I8l28資訊網——每日最新資訊28at.com
}測試用例I8l28資訊網——每日最新資訊28at.com

我們先來測試下奇數在偶數之前的函數處理代碼能否正常執行,如下所示:I8l28資訊網——每日最新資訊28at.com

const adjustArrayOrder = new AdjustArrayOrder();I8l28資訊網——每日最新資訊28at.com
// 奇數在前I8l28資訊網——每日最新資訊28at.com
const arr = [2, 4, 5, 6, 7, 8, 9, 11];I8l28資訊網——每日最新資訊28at.com
adjustArrayOrder.reorderOddEven(arr);I8l28資訊網——每日最新資訊28at.com
console.log(arr);I8l28資訊網——每日最新資訊28at.com

執行結果如下所示:I8l28資訊網——每日最新資訊28at.com

I8l28資訊網——每日最新資訊28at.com

最后,我們來測試下reorder函數能否正常執行:I8l28資訊網——每日最新資訊28at.com

負數在數組的最前面// 負數在前I8l28資訊網——每日最新資訊28at.com
const checkMinusNumber = function (val: number) {I8l28資訊網——每日最新資訊28at.com
return val > 0;I8l28資訊網——每日最新資訊28at.com
};I8l28資訊網——每日最新資訊28at.com
const arr = [2, 4, 5, 6, 7, -8, -10 - 12, -2];I8l28資訊網——每日最新資訊28at.com
adjustArrayOrder.reorder(arr, checkMinusNumber);I8l28資訊網——每日最新資訊28at.com
console.log(arr);I8l28資訊網——每日最新資訊28at.com

I8l28資訊網——每日最新資訊28at.com

能被3整除的數在數組的最前面const checkDivisible = function (val: number) {I8l28資訊網——每日最新資訊28at.com
return val % 3 !== 0;I8l28資訊網——每日最新資訊28at.com
};I8l28資訊網——每日最新資訊28at.com
const arr = [2, 4, 5, 6, 3, 6, 9, 12];I8l28資訊網——每日最新資訊28at.com
adjustArrayOrder.reorder(arr, checkDivisible);I8l28資訊網——每日最新資訊28at.com
console.log(arr);I8l28資訊網——每日最新資訊28at.com

I8l28資訊網——每日最新資訊28at.com

示例代碼I8l28資訊網——每日最新資訊28at.com

文中所舉代碼的完整版請移步:I8l28資訊網——每日最新資訊28at.com

AdjustArrayOrder.ts[1]adjustArrayOrder-test.ts[2]參考資料I8l28資訊網——每日最新資訊28at.com

[1]AdjustArrayOrder.ts: https://github.com/likaia/algorithm-practice/blob/e7f6a38021426397af60a73d4c6b8bf88548ba91/src/AdjustArrayOrder.ts#L2I8l28資訊網——每日最新資訊28at.com

[2]adjustArrayOrder-test.ts: https://github.com/likaia/algorithm-practice/blob/e7f6a38021426397af60a73d4c6b8bf88548ba91/src/test-case/adjustArrayOrder-test.ts#L3I8l28資訊網——每日最新資訊28at.com

[3]個人網站: https://www.kaisir.cn/I8l28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-119-2283-0.html調整數組元素順序,你了解幾分?

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 數據分析和數據科學的五大不同之處 譯文

下一篇: 亞馬遜云計算主管:AWS將保持云服務領先地位 不打算將其分拆

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 金堂县| 沾益县| 四子王旗| 文登市| 宁蒗| 桃江县| 余干县| 仁寿县| 青川县| 精河县| 阳东县| 湘潭县| 闻喜县| 聂荣县| 巴林左旗| 安西县| 奇台县| 宝山区| 济阳县| 大悟县| 琼海市| 鄂尔多斯市| 丹寨县| 富川| 石景山区| 眉山市| 年辖:市辖区| 通许县| 洪湖市| 长葛市| 泰来县| 东丰县| 红桥区| 新龙县| 浮梁县| 安泽县| 新晃| 乐业县| 石楼县| 醴陵市| 邮箱|