什麼是對撞指針?前端
JavaScript中的Array與對撞指針git
leetcode 對撞指針 解法題目github
167.兩數之II-輸入有序數組(easy)的對撞過程圖。
藍色指針:頭指針 紅色指針:尾指針 終止條件:頭尾指針對撞
算法
頭尾指針的命名能夠爲:segmentfault
i, j
head, tail
start, end
let arr = [1, 7, 5, 2]; let head = 0; let tail = arr.length -1;
var arr = new Array(10).fill(0).map((num,i)=>num+i); var i =0; var j = arr.length - 1; while(i<j){ i++; j--; } var collision = [i - 1, j + 1] var tip = `Array's head and tail had a collision between ${collision[0]} and ${collision[1]}`; console.log(tip); // Array's head and tail had a collision between 4 and 5
題目:https://leetcode-cn.com/probl...
題解:https://github.com/FrankKai/l...數組
var reverse = function (x) { /** * 解法2.指針對撞法 * 性能:96 ms 35.38% 35.9 MB 77.35% */ var sign = Math.sign(x); var arr = x.toString().split(""); // if (sign === -1) { arr.shift(); } // 指針對撞開始 var i = 0; var j = arr.length - 1; while (i < j) { swap(arr, i, j); i++; j--; } function swap(arr, i, j) { var temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // 指針對撞結束 var rX = parseInt(arr.join("")); var result = sign * rX; var min = -Math.pow(2, 31); var max = Math.pow(2, 31) - 1; if (rX < min || rX > max) return 0; return result; };
題目:https://leetcode-cn.com/probl...
題解:https://github.com/FrankKai/l...瀏覽器
var isPalindrome = function (x) { /** * 解法3:對撞指針法 * 性能:244ms 45.5MB */ var strArr = `${x}`.split(""); var i = 0; var j = strArr.length - 1; while (i < j) { if (strArr[i] !== strArr[j]) { return false; } i++; j--; } return true; };
題目:https://leetcode-cn.com/probl...
題解:https://github.com/FrankKai/l...微信
var removeElement = function (nums, val) { /** * 解法2:對撞指針 * 性能:64ms 33.9MB */ var i = 0; var j = nums.length - 1; while (i <= j) { if (nums[i] === val) { nums.splice(i, 1); j--; } else if (nums[j] === val) { nums.splice(j, 1); j--; } else { i++; } } return nums.length; };
題目:https://leetcode-cn.com/probl...
題解:https://github.com/FrankKai/l...前端工程師
var isPalindrome = function (s) { /** * 解法1:正則 + 對撞指針 * 性能:76ms 89.76% 37.5MB 70.83% */ var parlinDrome = s.replace(/[^\w]/g, "").replace(/_/g, "").toLowerCase(); var strArr = parlinDrome.split(""); var i = 0; var j = strArr.length - 1; while (i < j) { if (strArr[i] !== strArr[j]) { return false; } i++; j--; } return true; };
題目:https://leetcode-cn.com/probl...
題解:https://github.com/FrankKai/l...性能
var twoSum = function (numbers, target) { /** * 解法2:對撞指針 * 性能:68ms 71.18% 35.2MB 76.60% * 時間複雜度:O(n^2) */ var left = 0; var right = numbers.length - 1; while (left < right) { if (numbers[left] + numbers[right] === target) { return [left + 1, right + 1]; } else if (numbers[left] + numbers[right] > target) { right--; } else { left++; } } };
題目:https://leetcode-cn.com/probl...
題解:https://github.com/FrankKai/l...
/** * @param {number} n - a positive integer * @return {number} - a positive integer */ var reverseBits = function (n) { /** * 解法1:轉數組後對撞交換位置 * 性能:76ms 35.8MB * 思路: * 十進制轉二進制:toString(2) * 32位空位補0:padStart(32,0) * 反轉算法:對撞指針法 * 二進制轉十進制:parseInt(,2) */ let arr = n.toString(2).padStart(32, 0).split(""); let head = 0; let tail = arr.length - 1; while (head < tail) { swap(arr, head, tail); head++; tail--; } function swap(arr, i, j) { let temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } let result = parseInt(arr.join(""), 2); return result; };
題目:https://leetcode-cn.com/probl...
題解:https://github.com/FrankKai/l...
var reverseString = function (s) { /** * 解法2:對撞指針 */ var headIdx = 0; var tailIdx = s.length - 1; while (headIdx < tailIdx) { swap(s, headIdx, tailIdx); headIdx++; tailIdx--; } function swap(arr, i, j) { var temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } return s; };
題目:https://leetcode-cn.com/probl...
題解:https://github.com/FrankKai/l...
/** * @param {string} s * @return {string} */ var reverseVowels = function (s) { /** * 解法1:對撞指針 * 性能:108 ms 31.59% 38.4 MB 66.67% */ var univocalic = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]; var sArr = s.split(""); var head = 0; var tail = sArr.length - 1; while (head < tail) { if (univocalic.includes(sArr[head]) && univocalic.includes(sArr[tail])) { swap(sArr, head, tail); head++; tail--; } else if (!univocalic.includes(sArr[head])) { head++; } else if (!univocalic.includes(sArr[tail])) { tail--; } } function swap(arr, i, j) { var temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } return sArr.join(""); };
題目:https://leetcode-cn.com/probl...
題解:https://github.com/FrankKai/l...
var maxArea = function (height) { /** * 解法1:對撞指針法 * 性能:64ms 35.6MB * */ var head = 0; var tail = height.length - 1; var maxCapacity = 0; while (head < tail) { maxCapacity = Math.max(Math.min(height[head], height[tail]) * (tail - head), maxCapacity) if (height[head] < height[tail]) { head++ } else { tail--; } } return maxCapacity; };
期待和你們交流,共同進步,歡迎你們加入我建立的與前端開發密切相關的技術討論小組:
- 微信公衆號: 生活在瀏覽器裏的咱們 / excellent_developers
- Github博客: 趁你還年輕233的我的博客
- SegmentFault專欄:趁你還年輕,作個優秀的前端工程師
- Leetcode討論微信羣:Z2Fva2FpMjAxMDA4MDE=(加我微信拉你進羣)
努力成爲優秀前端工程師!