Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions javascript/LeetCode/Array/1464.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ var maxProduct = function (nums) {

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

// solution 2.
// 取得第一、第二大value,並將該陣列做切割…只取前兩個
// let sortNums = nums.sort((a,b) => b - a).slice(0,2);
// // 第一大元素
// let i = sortNums[0];
// // 第二大元素
// let j = sortNums[1];
// return (i - 1) * (j - 1);

};
const nums = [1, 5, 4, 5];
// return 16
Expand Down
61 changes: 17 additions & 44 deletions javascript/LeetCode/Array/1572.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,25 @@
// 1572. Matrix Diagonal Sum
// return the sum of the matrix diagonals. 返回正方形中對角線的總和
// sum X =>
// primary: 1 + 5 + 9
// secondary: 3 + 5 + 7

// 1. 是二維陣列
// 2. 判斷陣列length是奇數或偶數,共有幾個二維陣列
// 3.
// a.第一個二維陣列:找該array中位於第0個位置和最後一個位置的值,之後其他二維陣列則取往後移一個位置的值
// b.最後一個二維陣列:找該array中位於第0個位置和最後一個位置的值

// 二維陣列
// 判斷陣列length是奇數或偶數,共有幾個二維陣列
// ==> 找第一個和最後一個二維陣列中,第0個位置和最後一個位置的值
// 其他二維陣列則取往後移一個位置的值

/*
Input: mat = [[1,2,3],
[4,5,6],
[7,8,9]]
Output: 25
Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
Notice that element mat[1][1] = 5 is counted only once.
*/

/**
* 1572. Matrix Diagonal Sum
*
* @param {number[][]} mat
* @return {number}
*/
var diagonalSum = function (mat) {

var diagonalSum = function(mat) {
// 二維陣列,裡面的陣列是奇數的,取該陣列偶數index值;裡面陣列是偶數的,取該陣列奇數index值
let result = 0;
let j = mat[0].length - 1;
for (let i = 0; i < mat.length; i++, j--) {
if (i !== j) {
result += mat[i][j];
}
result += mat[i][i];

// $result += (i != j) ? mat[i][j] : mat[i][i];
// let end = endTime(start);
// console.log(end);
let len = mat.length;
let mid = Math.floor(len / 2 );
for (let i = 0; i < len; i++) {
result += mat[i][i];
result += mat[len - 1 - i][i];
}
if (len % 2 != 0) {
result -= mat[mid][mid];
}
return result;
};
const arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log(diagonalSum(arr));
let mat = [[1,2,3],[4,5,6],[7,8,9]];
// Output: 25
// Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
// Notice that element mat[1][1] = 5 is counted only once.
console.log(diagonalSum(mat));
11 changes: 11 additions & 0 deletions javascript/LeetCode/Array/1588.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ var sumOddLengthSubarrays = function (arr) {
}
}
return count;

// solution 2.
// let ans = 0;
// for(let i = 0;i < arr.length;++i) {
// let currentSum = 0;
// for(let j = i;j < arr.length;++j) {
// currentSum += arr[j];
// ans += (j - i + 1) % 2 === 1 ? currentSum : 0;
// }
// }
// return ans;
};
const arr = [1, 4, 2, 5, 3];
console.log(sumOddLengthSubarrays(arr));
16 changes: 16 additions & 0 deletions javascript/LeetCode/Array/1980.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 1980. Find Unique Binary String
*
* @param {string[]} nums
* @return {string}
*/
var findDifferentBinaryString = function(nums) {
let res = "";
for(let i = 0;i < nums.length;i++) {
res += (nums[i][i] === '0' ? '1' : '0');
}
return res;
};
let nums = ["01","10"];
// "11"
// console.log(findDifferentBinaryString(nums));
34 changes: 34 additions & 0 deletions javascript/LeetCode/math/165.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 165. Compare Version Numbers
*
* 參數為兩個字串,其內含有".",將參數依據"."拆成左右兩部份,從左到右比較每個部份大小。
*
* If version1 < version2, return -1.
* If version1 > version2, return 1.
* Otherwise, return 0.
*
* 若部份的前面有0,則忽略0,取整數
*
* @param {string} version1
* @param {string} version2
* @return {number}
*/
var compareVersion = function(version1, version2) {
let splitV1 = version1.split("."),splitV2 = version2.split(".");
let length = Math.max(splitV1.length, splitV2.length);
for(let i = 0;i < length;i++) {
let num1 = parseInt(splitV1[i]) || 0;
let num2 = parseInt(splitV2[i]) || 0;

if(num1 === num2){
continue;
}
return num1 > num2 ? 1 : -1;
}
return 0;
};
// let version1 = "1.2", version2 = "1.10"
// -1
let version1 = "1.01", version2 = "1.001";
// 0
console.log(compareVersion(version1,version2))
32 changes: 32 additions & 0 deletions javascript/LeetCode/math/3099.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 3099. Harshad Number
*
* Harshad:能被其各位數字之和整除
* 如果 x 是Harshad,則傳回 x 各位數字總和;否則,回傳 -1。
* @param {number} x
* @return {number}
*/
var sumOfTheDigitsOfHarshadNumber = function(x) {
// let split = x.toString().split("");
// let sum = 0;
// for(let i = 0;i < split.length;i++) {
// sum+=parseInt(split[i]);
// }
// if(x % sum === 0){
// return sum;
// }
// return -1;

// solution 2.
let ans = 0;
let temp = x;
while(temp > 0){
// 取尾數
ans += temp % 10;
temp = Math.floor(temp / 10);
}
return x % ans === 0 ? ans : -1;
};
let x = 18;
// 9
console.log(sumOfTheDigitsOfHarshadNumber(x));
21 changes: 21 additions & 0 deletions javascript/LeetCode/math/3602.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 3602. Hexadecimal and Hexatrigesimal Conversion
*
* hexadecimal = base 16,使用數字0 - 9和大寫A - F代表0 - 15
* hexatrigesimal = base 16,使用數字0 - 9和大寫A - Z代表0 - 35
* 取得hexadecimal的n的二次方(n * 2)和hexatrigesimal的n的三次方(n * 3)串連
* @param {number} n
* @return {string}
*/
var concatHex36 = function(n) {
return ((n ** 2).toString(16) + (n ** 3).toString(36)).toUpperCase();
};
let n = 36
/**
* Output: "5101000"
n2 = 36 * 36 = 1296. In hexadecimal, it converts to (5 * 162) + (1 * 16) + 0 = 1296, which corresponds to "510".
n3 = 36 * 36 * 36 = 46656. In hexatrigesimal, it converts to (1 * 363) + (0 * 362) + (0 * 36) + 0 = 46656, which corresponds to "1000".
Concatenating both results gives "510" + "1000" = "5101000".
*
*/
console.log(concatHex36(n));
18 changes: 18 additions & 0 deletions javascript/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// debugger
import { format } from 'node:path';
import {ExecutionTimer} from './time.js';
import assert from 'node:assert/strict';

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

/**
* 166. Fraction to Recurring Decimal
*
* 參數為分子、分母,以字串資料型態回傳分數
* 如果小數部分重複,則將重複部分放在括號中。
* 若有很多個答案,任一回傳
*
* @param {number} numerator
* @param {number} denominator
* @return {string}
*/
var fractionToDecimal = function(numerator, denominator) {

};
let numerator = 1, denominator = 2;
// "0.5"
// console.log(fractionToDecimal(numerator,denominator));
5 changes: 5 additions & 0 deletions javascript/oop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 具名匯出 (Named Export),使用{},匯出多個獨立的功能
import { Vehicle } from "./oop/Vehicle.js ";

car = new Vehicle("Ford","Kuga");
console.log(car.startEngine())
32 changes: 0 additions & 32 deletions javascript/oop/1_basic_literals.js

This file was deleted.

14 changes: 0 additions & 14 deletions javascript/oop/2_constructor.js

This file was deleted.

16 changes: 0 additions & 16 deletions javascript/oop/3_prototypes.js

This file was deleted.

24 changes: 0 additions & 24 deletions javascript/oop/4_inheritence.js

This file was deleted.

24 changes: 0 additions & 24 deletions javascript/oop/5_object_create.js

This file was deleted.

25 changes: 0 additions & 25 deletions javascript/oop/6_classes-ES6.js

This file was deleted.

Loading