Skip to content

Commit 30882b1

Browse files
committed
30 oct
1 parent 3e47567 commit 30882b1

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

JavaScript/array.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,49 @@
33
* @return {number}
44
*/
55

6-
var twoSum = function(nums, target) { // Find two numbers which are two-sum of target in nums
7-
const map = new Map(); // create a new map
8-
for (let i in nums){ // now iterate over nums
9-
const complement = target - nums[i] // find complement of target to current
10-
if (map.has(complement)){ // if there's complement in map
11-
return [map.get(complement), i] // return complement and current index
6+
var twoSum = function (nums, target) { // Find two numbers which are two-sum of target in nums
7+
const map = new Map(); // create a new map
8+
for (let i = 0; i < nums.length; i++) { // now iterate over nums
9+
const complement = target - nums[i] // find complement of target to current
10+
if (map.has(complement)) { // if there's complement in map
11+
return [map.get(complement), i] // return complement and current index
1212
}
13-
map.set(nums[i], i) // map current number to current index otherwise
13+
map.set(nums[i], i) // map current number to current index otherwise
1414
}
15-
};
15+
};
1616

17-
var missingNumber = function(nums) {
17+
var searchMatrix = function (matrix, target) {
18+
if (!matrix.length || !matrix[0].length) return false;
19+
let row = 0;
20+
let col = matrix[0].length - 1;
21+
while (row < matrix.length && col >= 0) {
22+
if (matrix[row][col] === target) {
23+
return true;
24+
} else if (matrix[row][col] > target) {
25+
col--
26+
} else row++
27+
}
28+
return false
29+
};
30+
31+
var missingNumber = function (nums) {
1832
const n = nums.length;
19-
const expextedSum = (n*(n+1))/2;
20-
const actualSum = nums.reduce((sum, num)=>num+sum, 0)
21-
return expextedSum-actualSum;
33+
const expextedSum = (n * (n + 1)) / 2;
34+
const actualSum = nums.reduce((sum, num) => num + sum, 0)
35+
return expextedSum - actualSum;
2236
};
2337

2438
function singleNumber(nums) {
2539
let unique = 0;
2640
for (let num of nums) {
27-
unique ^= num; // XOR each number
41+
unique ^= num; // XOR each number
2842
}
2943
return unique;
3044
}
3145

3246
function moveZeroes(nums) {
3347
let lastNonZeroIndex = 0;
34-
48+
3549
// Move all non-zero elements to the front
3650
for (let i = 0; i < nums.length; i++) {
3751
if (nums[i] !== 0) {

0 commit comments

Comments
 (0)