Skip to content

Commit 5f8df17

Browse files
authored
Merge pull request #149 from clingoram/mavis
解題、oop
2 parents eba70c4 + 626b1fd commit 5f8df17

36 files changed

Lines changed: 466 additions & 427 deletions

javascript/LeetCode/Array/1464.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ var maxProduct = function (nums) {
5959

6060
return (lastNumber - 1) * (secondLastNumber - 1);
6161

62+
// solution 2.
63+
// 取得第一、第二大value,並將該陣列做切割…只取前兩個
64+
// let sortNums = nums.sort((a,b) => b - a).slice(0,2);
65+
// // 第一大元素
66+
// let i = sortNums[0];
67+
// // 第二大元素
68+
// let j = sortNums[1];
69+
// return (i - 1) * (j - 1);
70+
6271
};
6372
const nums = [1, 5, 4, 5];
6473
// return 16

javascript/LeetCode/Array/1572.js

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,25 @@
1-
// 1572. Matrix Diagonal Sum
2-
// return the sum of the matrix diagonals. 返回正方形中對角線的總和
3-
// sum X =>
4-
// primary: 1 + 5 + 9
5-
// secondary: 3 + 5 + 7
6-
7-
// 1. 是二維陣列
8-
// 2. 判斷陣列length是奇數或偶數,共有幾個二維陣列
9-
// 3.
10-
// a.第一個二維陣列:找該array中位於第0個位置和最後一個位置的值,之後其他二維陣列則取往後移一個位置的值
11-
// b.最後一個二維陣列:找該array中位於第0個位置和最後一個位置的值
12-
13-
// 二維陣列
14-
// 判斷陣列length是奇數或偶數,共有幾個二維陣列
15-
// ==> 找第一個和最後一個二維陣列中,第0個位置和最後一個位置的值
16-
// 其他二維陣列則取往後移一個位置的值
17-
18-
/*
19-
Input: mat = [[1,2,3],
20-
[4,5,6],
21-
[7,8,9]]
22-
Output: 25
23-
Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
24-
Notice that element mat[1][1] = 5 is counted only once.
25-
*/
26-
271
/**
2+
* 1572. Matrix Diagonal Sum
3+
*
284
* @param {number[][]} mat
295
* @return {number}
306
*/
31-
var diagonalSum = function (mat) {
32-
7+
var diagonalSum = function(mat) {
8+
// 二維陣列,裡面的陣列是奇數的,取該陣列偶數index值;裡面陣列是偶數的,取該陣列奇數index值
339
let result = 0;
34-
let j = mat[0].length - 1;
35-
for (let i = 0; i < mat.length; i++, j--) {
36-
if (i !== j) {
37-
result += mat[i][j];
38-
}
39-
result += mat[i][i];
40-
41-
// $result += (i != j) ? mat[i][j] : mat[i][i];
42-
// let end = endTime(start);
43-
// console.log(end);
10+
let len = mat.length;
11+
let mid = Math.floor(len / 2 );
12+
for (let i = 0; i < len; i++) {
13+
result += mat[i][i];
14+
result += mat[len - 1 - i][i];
15+
}
16+
if (len % 2 != 0) {
17+
result -= mat[mid][mid];
4418
}
4519
return result;
4620
};
47-
const arr = [
48-
[1, 2, 3],
49-
[4, 5, 6],
50-
[7, 8, 9]
51-
];
52-
console.log(diagonalSum(arr));
21+
let mat = [[1,2,3],[4,5,6],[7,8,9]];
22+
// Output: 25
23+
// Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
24+
// Notice that element mat[1][1] = 5 is counted only once.
25+
console.log(diagonalSum(mat));

javascript/LeetCode/Array/1588.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ var sumOddLengthSubarrays = function (arr) {
6969
}
7070
}
7171
return count;
72+
73+
// solution 2.
74+
// let ans = 0;
75+
// for(let i = 0;i < arr.length;++i) {
76+
// let currentSum = 0;
77+
// for(let j = i;j < arr.length;++j) {
78+
// currentSum += arr[j];
79+
// ans += (j - i + 1) % 2 === 1 ? currentSum : 0;
80+
// }
81+
// }
82+
// return ans;
7283
};
7384
const arr = [1, 4, 2, 5, 3];
7485
console.log(sumOddLengthSubarrays(arr));

javascript/LeetCode/Array/1980.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* 1980. Find Unique Binary String
3+
*
4+
* @param {string[]} nums
5+
* @return {string}
6+
*/
7+
var findDifferentBinaryString = function(nums) {
8+
let res = "";
9+
for(let i = 0;i < nums.length;i++) {
10+
res += (nums[i][i] === '0' ? '1' : '0');
11+
}
12+
return res;
13+
};
14+
let nums = ["01","10"];
15+
// "11"
16+
// console.log(findDifferentBinaryString(nums));

javascript/LeetCode/math/165.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 165. Compare Version Numbers
3+
*
4+
* 參數為兩個字串,其內含有".",將參數依據"."拆成左右兩部份,從左到右比較每個部份大小。
5+
*
6+
* If version1 < version2, return -1.
7+
* If version1 > version2, return 1.
8+
* Otherwise, return 0.
9+
*
10+
* 若部份的前面有0,則忽略0,取整數
11+
*
12+
* @param {string} version1
13+
* @param {string} version2
14+
* @return {number}
15+
*/
16+
var compareVersion = function(version1, version2) {
17+
let splitV1 = version1.split("."),splitV2 = version2.split(".");
18+
let length = Math.max(splitV1.length, splitV2.length);
19+
for(let i = 0;i < length;i++) {
20+
let num1 = parseInt(splitV1[i]) || 0;
21+
let num2 = parseInt(splitV2[i]) || 0;
22+
23+
if(num1 === num2){
24+
continue;
25+
}
26+
return num1 > num2 ? 1 : -1;
27+
}
28+
return 0;
29+
};
30+
// let version1 = "1.2", version2 = "1.10"
31+
// -1
32+
let version1 = "1.01", version2 = "1.001";
33+
// 0
34+
console.log(compareVersion(version1,version2))

javascript/LeetCode/math/3099.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 3099. Harshad Number
3+
*
4+
* Harshad:能被其各位數字之和整除
5+
* 如果 x 是Harshad,則傳回 x 各位數字總和;否則,回傳 -1。
6+
* @param {number} x
7+
* @return {number}
8+
*/
9+
var sumOfTheDigitsOfHarshadNumber = function(x) {
10+
// let split = x.toString().split("");
11+
// let sum = 0;
12+
// for(let i = 0;i < split.length;i++) {
13+
// sum+=parseInt(split[i]);
14+
// }
15+
// if(x % sum === 0){
16+
// return sum;
17+
// }
18+
// return -1;
19+
20+
// solution 2.
21+
let ans = 0;
22+
let temp = x;
23+
while(temp > 0){
24+
// 取尾數
25+
ans += temp % 10;
26+
temp = Math.floor(temp / 10);
27+
}
28+
return x % ans === 0 ? ans : -1;
29+
};
30+
let x = 18;
31+
// 9
32+
console.log(sumOfTheDigitsOfHarshadNumber(x));

javascript/LeetCode/math/3602.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 3602. Hexadecimal and Hexatrigesimal Conversion
3+
*
4+
* hexadecimal = base 16,使用數字0 - 9和大寫A - F代表0 - 15
5+
* hexatrigesimal = base 16,使用數字0 - 9和大寫A - Z代表0 - 35
6+
* 取得hexadecimal的n的二次方(n * 2)和hexatrigesimal的n的三次方(n * 3)串連
7+
* @param {number} n
8+
* @return {string}
9+
*/
10+
var concatHex36 = function(n) {
11+
return ((n ** 2).toString(16) + (n ** 3).toString(36)).toUpperCase();
12+
};
13+
let n = 36
14+
/**
15+
* Output: "5101000"
16+
n2 = 36 * 36 = 1296. In hexadecimal, it converts to (5 * 162) + (1 * 16) + 0 = 1296, which corresponds to "510".
17+
n3 = 36 * 36 * 36 = 46656. In hexatrigesimal, it converts to (1 * 363) + (0 * 362) + (0 * 36) + 0 = 46656, which corresponds to "1000".
18+
Concatenating both results gives "510" + "1000" = "5101000".
19+
*
20+
*/
21+
console.log(concatHex36(n));

javascript/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// debugger
2+
import { format } from 'node:path';
23
import {ExecutionTimer} from './time.js';
34
import assert from 'node:assert/strict';
45

@@ -1028,3 +1029,20 @@ var closetPair = function(arr1,arr2,x) {
10281029
// [1,30];
10291030
// console.log(closetPair(arr1,arr2,x));
10301031

1032+
/**
1033+
* 166. Fraction to Recurring Decimal
1034+
*
1035+
* 參數為分子、分母,以字串資料型態回傳分數
1036+
* 如果小數部分重複,則將重複部分放在括號中。
1037+
* 若有很多個答案,任一回傳
1038+
*
1039+
* @param {number} numerator
1040+
* @param {number} denominator
1041+
* @return {string}
1042+
*/
1043+
var fractionToDecimal = function(numerator, denominator) {
1044+
1045+
};
1046+
let numerator = 1, denominator = 2;
1047+
// "0.5"
1048+
// console.log(fractionToDecimal(numerator,denominator));

javascript/oop.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// 具名匯出 (Named Export),使用{},匯出多個獨立的功能
2+
import { Vehicle } from "./oop/Vehicle.js ";
3+
4+
car = new Vehicle("Ford","Kuga");
5+
console.log(car.startEngine())

javascript/oop/1_basic_literals.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)