var countPointsWithOneCircle = function(points, circle){ var count = 0 for(var i = 0; i < points.length; i++){ if(isIn(points[i], circle))count++ } return count }
var countPoints = function(points, queries) { var arr = newArray() for(var i in queries){ arr.push(countPointsWithOneCircle(points, queries[i])) } return arr };
1480. 一维数组的动态和
考点:动态规划
1 2 3 4 5 6
var runningSum = function(nums) { for(var i = 1; i < nums.length; i++){ nums[i] += nums[i - 1] } return nums };
1720. 解码异或后的数组
考点:动态规划、异或问题
1 2 3 4 5
// 回旋 a ^ b ^ b = a 若:a ^ b = c,则: a = c ^ b b = c ^ a
1 2 3 4 5 6 7
var decode = function(encoded, first) { encoded.unshift(first) for(var i = 1; i < encoded.length; i++){ encoded[i] ^= encoded[i - 1] } return encoded };
1689.十-二进制数的最少数目
考点:…符号的使用,可以让字符串解构
1 2 3 4 5 6 7 8 9
var minPartitions = function(n) { var max = 0 var num = parseInt(n) while(num !== 0){ max = max > num % 10 ? max : (num % 10) num = parseInt(num / 10) } return max };
27.移除元素
考点:双指针法:当指到需要移除的元素时,慢指针停一格,然后用快指针的数据赋值给慢指针的数据
1 2 3 4 5 6 7 8 9 10 11
var removeElement = function(nums, val) { // 慢指针 var k = 0 for(var i = 0; i < nums.length; i++){ if(nums[i] !== val){ nums[k++] = nums[i] } } return k };
977. 有序数组的平方
考点:双指针法,当指到的元素函数值较小时,就将函数值拿出来推进新数组,然后指针移动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var sortedSquares = function(nums) { var arr = newArray() var i = 0 var j = nums.length - 1 while(i <= j){ if(nums[i] ** 2 <= nums[j] ** 2){ arr.unshift(nums[j] ** 2) j-- }else{ arr.unshift(nums[i] ** 2) i++ } } return arr };
209. 长度最小的子数组
考点:双指针法,制造一个滑动窗口
有点类似于栈和队列
当滑动窗口满条件时,就往外出队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var minSubArrayLen = function(target, nums) { var fast = 0 var low = 0 var length = nums.length var sum = 0 var res = length + 1 while(fast < length + 1){ sum += nums[fast] fast++ while(sum >= target){ res = res < fast - low ? res : fast - low sum -= nums[low] low++ } }
/** * Initialize your data structure here. */ var MyLinkedList = function() { this.head = null this.length = 0 };
classnode{ constructor(val, next){ this.val = val this.next = next } }
/** * Get the value of the index-th node in the linked list. If the index is invalid, return -1. * @param {number}index * @return {number} */ MyLinkedList.prototype.get = function(index) { var temp = new node(0, this.head) if(index < 0 || index >= this.length){ return -1 } for(var i = 0; i < index; i++){ temp = temp.next } return temp.next.val };
/** * Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. * @param {number}val * @return {void} */ MyLinkedList.prototype.addAtHead = function(val) { this.addAtIndex(0, val) };
/** * Append a node of value val to the last element of the linked list. * @param {number}val * @return {void} */ MyLinkedList.prototype.addAtTail = function(val) { this.addAtIndex(this.length, val) };
/** * Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. * @param {number}index * @param {number}val * @return {void} */ MyLinkedList.prototype.addAtIndex = function(index, val) { if(index < 0){ return } var virtual = new node(0, this.head) var temp = virtual for(var i = 0; i < index && i < this.length; i++){ temp = temp.next } var insert = new node(val) insert.next = temp.next temp.next = insert this.head = virtual.next this.length++ };
/** * Delete the index-th node in the linked list, if the index is valid. * @param {number}index * @return {void} */ MyLinkedList.prototype.deleteAtIndex = function(index) { var virtualH = new node(0, this.head) var temp = virtualH