From 9b85759db1f756ac9fa191cfcc085b197f690295 Mon Sep 17 00:00:00 2001 From: Copyes Date: Mon, 22 Jun 2020 19:08:36 +0800 Subject: [PATCH] new problem --- ScriptOJ/add.js | 14 ++++++++++++++ ScriptOJ/isStraight.js | 22 ++++++++++++++++++++++ ScriptOJ/maxSlidingWindow.js | 17 +++++++++++++++++ ScriptOJ/maxSubArray.js | 7 +++++++ ScriptOJ/minArray.js | 24 ++++++++++++++++++++++++ ScriptOJ/missingNumber.js | 22 ++++++++++++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 ScriptOJ/add.js create mode 100644 ScriptOJ/isStraight.js create mode 100644 ScriptOJ/maxSlidingWindow.js create mode 100644 ScriptOJ/maxSubArray.js create mode 100644 ScriptOJ/minArray.js create mode 100644 ScriptOJ/missingNumber.js diff --git a/ScriptOJ/add.js b/ScriptOJ/add.js new file mode 100644 index 0000000..89102d0 --- /dev/null +++ b/ScriptOJ/add.js @@ -0,0 +1,14 @@ +/** + * 写一个函数,求两个整数之和,要求在函数体内不得使用 “+”、“-”、“*”、“/” 四则运算符号。 + * @param {number} a + * @param {number} b + * @return {number} + */ +var add = function(a, b) { + while(b !== 0) { + let c = a ^ b; + b = (a & b) << 1; + a = c; + } + return a; +}; \ No newline at end of file diff --git a/ScriptOJ/isStraight.js b/ScriptOJ/isStraight.js new file mode 100644 index 0000000..2537fc4 --- /dev/null +++ b/ScriptOJ/isStraight.js @@ -0,0 +1,22 @@ +/** + * 剑指 Offer 61. 扑克牌中的顺子 + * 从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王为 0 ,可以看成任意数字。A 不能视为 14。 + +来源:力扣(LeetCode) +链接:https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof +著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + * @param {number[]} nums + * @return {boolean} + */ +var isStraight = function(nums) { + nums = nums.sort((a, b)=> a-b); + let count = 0; + for(let i = 0; i < nums.length; i++) { + if(nums[i] === 0) { + count++; + } else if(nums[i] === nums[i-1]) { + return false; + } + } + return nums[4] - nums[count] < 5 +}; \ No newline at end of file diff --git a/ScriptOJ/maxSlidingWindow.js b/ScriptOJ/maxSlidingWindow.js new file mode 100644 index 0000000..6437e00 --- /dev/null +++ b/ScriptOJ/maxSlidingWindow.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var maxSlidingWindow = function(nums, k) { + if(k === 1) return nums; + if(k === nums.length) return nums; + let length = nums.length - k; + let result = []; + for (let i = 0; i <= length; i++) { + let arrSlice = nums.slice(i, i+k); + arrSlice.sort((a,b)=> a-b); + result.push(arrSlice[k-1]); + } + return result; +}; \ No newline at end of file diff --git a/ScriptOJ/maxSubArray.js b/ScriptOJ/maxSubArray.js new file mode 100644 index 0000000..cf3f56b --- /dev/null +++ b/ScriptOJ/maxSubArray.js @@ -0,0 +1,7 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var maxSubArray = function(nums) { + +}; \ No newline at end of file diff --git a/ScriptOJ/minArray.js b/ScriptOJ/minArray.js new file mode 100644 index 0000000..cc93cd6 --- /dev/null +++ b/ScriptOJ/minArray.js @@ -0,0 +1,24 @@ +/** + * 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一个旋转,该数组的最小值为1。 + +来源:力扣(LeetCode) +链接:https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof +著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + * @param {number[]} numbers + * @return {number} + */ +var minArray = function(numbers) { + let left = 0; + let right = numbers.length - 1; + while (left <= right) { + let mid = left + Math.floor((right - left) / 2); + if(numbers[mid] < numbers[right]) { + right = mid; + } else if(numbers[mid] > numbers[right]) { + left = mid + 1; + } else { + right--; + } + } + return numbers[left]; +}; \ No newline at end of file diff --git a/ScriptOJ/missingNumber.js b/ScriptOJ/missingNumber.js new file mode 100644 index 0000000..55745ba --- /dev/null +++ b/ScriptOJ/missingNumber.js @@ -0,0 +1,22 @@ +/** + * 一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0~n-1之内。在范围0~n-1内的n个数字中有且只有一个数字不在该数组中,请找出这个数字。 + +来源:力扣(LeetCode) +链接:https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof +著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + let left = 0; + let right = nums.length - 1; + while(left <= right) { + let mid = left + Math.floor((right - left) / 2); + if(nums[mid] === mid) { + left = mid + 1; + } else if(mid < nums[mid]){ + right = mid -1; + } + } + return left; +}; \ No newline at end of file