Skip to content

Commit

Permalink
new problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Copyes committed Jun 22, 2020
1 parent 95fa890 commit 9b85759
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ScriptOJ/add.js
Original file line number Diff line number Diff line change
@@ -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;
};
22 changes: 22 additions & 0 deletions ScriptOJ/isStraight.js
Original file line number Diff line number Diff line change
@@ -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
};
17 changes: 17 additions & 0 deletions ScriptOJ/maxSlidingWindow.js
Original file line number Diff line number Diff line change
@@ -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;
};
7 changes: 7 additions & 0 deletions ScriptOJ/maxSubArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {

};
24 changes: 24 additions & 0 deletions ScriptOJ/minArray.js
Original file line number Diff line number Diff line change
@@ -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];
};
22 changes: 22 additions & 0 deletions ScriptOJ/missingNumber.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 9b85759

Please sign in to comment.