Skip to content

Commit 352194d

Browse files
committed
新增題目2540以及3925解法
1 parent 944ca4c commit 352194d

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

javascript/LeetCode/Array/2540.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 2540. Minimum Common Value
3+
*
4+
* @param {number[]} nums1
5+
* @param {number[]} nums2
6+
* @return {number}
7+
*/
8+
var getCommon = function(nums1, nums2) {
9+
/**
10+
* 找出兩個陣列中最小的共通元素,找不到則-1
11+
*
12+
* 2個陣列長度不一定一樣
13+
*/
14+
// Solution 1.
15+
// Set
16+
// let set = new Set();
17+
// for(const a of nums1){
18+
// set.add(a);
19+
// }
20+
// for(const b of nums2){
21+
// if(set.has(b)){
22+
// return b;
23+
// }
24+
// }
25+
// return -1;
26+
27+
28+
// Solution 2.
29+
// 2 pointers
30+
let i = 0, j = 0;
31+
while(i < nums1.length && j < nums2.length){
32+
if(nums1[i] < nums2[j]){
33+
i++;
34+
}else if(nums1[i] > nums2[j]){
35+
j++;
36+
}else{
37+
return nums1[i];
38+
}
39+
}
40+
return -1;
41+
};
42+
let nums1 = [1,2,3], nums2 = [2,4];
43+
// 2
44+
console.log(getCommon(nums1,nums2));

javascript/LeetCode/Array/3925.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 3925. Concatenate Array With Reverse
3+
*
4+
* @param {number[]} nums
5+
* @return {number[]}
6+
*/
7+
var concatWithReverse = function(nums) {
8+
/**
9+
* 新陣列長度 = nums.length * 2
10+
* 前n個為nums原有排序,後n個為nums反轉後並將它合併一起成一個陣列
11+
*/
12+
let ans = [];
13+
for(let i = 0;i < nums.length;++i) {
14+
ans.push(nums[i]);
15+
}
16+
let reverse = nums.reverse();
17+
return ans.concat(reverse);
18+
};
19+
let nums = [1,2,3];
20+
/*
21+
Output: [1,2,3,3,2,1]
22+
Explanation:
23+
The first n elements of ans are the same as nums.
24+
For the next n = 3 elements, each element is taken from nums in reverse order:
25+
ans[3] = nums[2] = 3
26+
ans[4] = nums[1] = 2
27+
ans[5] = nums[0] = 1
28+
Thus, ans = [1, 2, 3, 3, 2, 1].
29+
*/
30+
console.log(concatWithReverse(nums));

javascript/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ var fractionRecurringDecimal = function (a,b) {
360360
}
361361
let a = 1,b = 2;
362362
// "0.5"
363-
console.log(fractionRecurringDecimal(a,b));
363+
// console.log(fractionRecurringDecimal(a,b));
364364

365365
/**
366366
* Recurring Sequence in a Fraction

0 commit comments

Comments
 (0)