Skip to content

Commit f88f490

Browse files
authored
Merge pull request #154 from clingoram/mavis
解題
2 parents 6d26457 + bd9a1ba commit f88f490

6 files changed

Lines changed: 198 additions & 34 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# practice
1+
# leetcode_javascript_and_python
2+
23
<h1>目的</h1>
3-
1. 主要用來練習JS,和Python
4+
1. 主要用來練習JavaScript和Python
45
2. 題目來源:
56
- LeetCode
67
- CodeWars
78
- HackerRank
89

9-
雖是JavaScript,但實際上使用Node.js,因此不需要打開瀏覽器便可執行JS的環境
10-
CMD打上node {檔案名稱.js},EG.node index.js <br>
11-
而Python,則是打上 python3 {檔案名稱.py} EG.python3 index.py
10+
使用Docker對應Image和腳本來執行
11+
JavaScript 使用node

javascript/LeetCode/Array/2442.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 2442. Count Number of Distinct Integers After Reverse Operations
3+
*
4+
* 計算陣列元素digits反轉後,加上原有陣列會有幾個數字是唯一值
5+
*
6+
* @param {number[]} nums
7+
* @return {number}
8+
*/
9+
var countDistinctIntegers = function(nums) {
10+
// O(N)
11+
nums.push(...nums.map(num =>
12+
parseInt(num.toString().split('').reverse().join(''))
13+
));
14+
return new Set(nums).size;
15+
};
16+
let nums = [1,13,10,12,31];
17+
/*
18+
Output: 6
19+
Explanation: After including the reverse of each number, the resulting array is [1,13,10,12,31,1,31,1,21,13].
20+
The reversed integers that were added to the end of the array are underlined. Note that for the integer 10, after reversing it, it becomes 01 which is just 1.
21+
The number of distinct integers in this array is 6 (The numbers 1, 10, 12, 13, 21, and 31).
22+
*/
23+
console.log(countDistinctIntegers(nums));

javascript/LeetCode/String/1653.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* 1653. Minimum Deletions to Make String Balanced
3+
*
4+
* 參數s中只有'a' & 'b'這兩個字母。
5+
* 刪除任一字母使s balanced,若不存在一對index (i,j) 使得 i < j 且 s[i] = 'b' 且 s[j] = 'a',則s 是balanced。
6+
* 回傳至少須刪除幾次(操作幾次)才能使s balanced
7+
*
8+
*
9+
* @param {string} s
10+
* @return {number}
11+
*/
12+
var minimumDeletions = function(s) {
13+
// balanced string中,b不能出現在a之後
14+
// no such 'b' at s[i] where s[j] is 'a' and i < j
15+
16+
// TC:O(N)
17+
// 計算a,b各自出現幾次
18+
let countA = 0,countB = 0;
19+
let minDel = s.length;
20+
// 先計算a出現幾次
21+
for(let i = 0;i < s.length;++i) {
22+
if(s[i] === "a"){
23+
countA++;
24+
}
25+
}
26+
// 之後再次迴圈,若遇到a則--
27+
for(let i = 0; i < s.length;++i) {
28+
if(s[i] === "a"){
29+
countA--;
30+
}
31+
// 不斷更新比較雙方次數
32+
minDel = Math.min(minDel,countA + countB);
33+
34+
// 遇到b,++
35+
if(s[i] === "b"){
36+
countB++;
37+
}
38+
}
39+
return minDel;
40+
};
41+
let s = "aababbab";
42+
/*Output: 2
43+
Explanation: You can either:
44+
Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or
45+
Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
46+
*/
47+
console.log(minimumDeletions(s));

javascript/LeetCode/String/3760.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 3760. Maximum Substrings With Distinct Start
3+
* Difficulty:Medium
4+
*
5+
* @param {string} s
6+
* @return {number}
7+
*/
8+
var maxDistinct = function(s) {
9+
// 計算字串中,若每個開頭是跟另一substring開頭不同的字母,可以有幾種組合
10+
// 計算每個字母出現次數
11+
12+
// let map = new Map();
13+
// for(let i = 0; i < s.length;++i) {
14+
// map = map.has(s[i]) ? map.set(s[i], map.get(s[i]) + 1) : map.set(s[i], 1);
15+
// }
16+
// return map.size;
17+
18+
// solution 2.
19+
/**
20+
* TC: O(N) =>
21+
* 將s弄成陣列,須loop所有元素,因此O(N)
22+
* new Set(...) 將每個元素插入set,add是O(1)但要做n次,因此O(N)
23+
* size 讀取長度,因此O(1)
24+
*
25+
* new Set([...s]) 需要loop並插入所有元素,所以整體是O(n)
26+
* */
27+
return new Set([...s]).size;
28+
};
29+
let s = "abab";
30+
// 2
31+
console.log(maxDistinct(s))

javascript/LeetCode/String/3884.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 3884. First Matching Character From Both Ends
3+
*
4+
* Return the smallest index i such that s[i] == s[s.length - i - 1].
5+
* 找出最小index,須符合s[i] === s[s.length - i - 1]這條件,若沒有則-1
6+
*
7+
* @param {string} s
8+
* @return {number}
9+
*/
10+
var firstMatchingIndex = function(s) {
11+
// TC: O(N)
12+
// SC: O(1)
13+
let i = 0,j = s.length - 1;
14+
while(i <= j){
15+
if(s[i] === s[j]){
16+
// 左邊index一定是最小的
17+
return i;
18+
}
19+
i++; // 左邊index ++
20+
j--; // 右邊index --
21+
}
22+
return -1;
23+
};
24+
let s = "abcacbd";
25+
// 1
26+
console.log(firstMatchingIndex(s));

javascript/index.js

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// debugger
2-
import { format } from 'node:path';
32
import {ExecutionTimer} from './time.js';
43
import assert from 'node:assert/strict';
54
import { count } from 'node:console';
6-
import { lchown } from 'node:fs';
75

86
/*
97
22. Generate Parentheses
@@ -108,7 +106,7 @@ var findLongestWord = function (s, dictionary) {
108106
* Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
109107
* Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
110108
*
111-
* Input num1 and num2 are 非負數以字串方式呈現
109+
* Input num1 and num2 非負數以字串方式呈現
112110
* Output num1 * num2(以字串方式呈現)
113111
* 不能使用內建含式或直接把Input轉成數字
114112
* -------------------------------------------
@@ -131,21 +129,23 @@ var findLongestWord = function (s, dictionary) {
131129
* @return {string}
132130
*/
133131
var multiply = function (num1, num2) {
132+
/**
133+
* 不能使用內建涵式或轉換型態
134+
*/
134135

135-
let pattern = /^[0-9]+$/;
136-
137-
if (!num1.match(pattern) || !num2.match(pattern) || Number(num1) === 0 || Number(num2) === 0) {
138-
return;
136+
let answer = Array(num1.length + num2.length).fill(0);
137+
console.log(answer)
138+
for(let i = num1.length - 1;i >= 0;i--){
139+
139140
}
140-
141-
142-
143141
};
144142
// const num1 = "2", num2 = "3";
145143
// "6"
146144
// const num1 = "123", num2 = "456";
147145
// "56088"
148-
// console.log(multiply(num1, num2));
146+
const num1 = "123456789",num2 = "987654321";
147+
// "121932631112635269"
148+
// console.log(multiply(num1, num2));ㄋㄋ
149149

150150

151151

@@ -1258,24 +1258,61 @@ var minRemoval = function(nums, k) {
12581258
// console.log(minRemoval(nums,k));
12591259

12601260

1261-
/**
1262-
* 1653. Minimum Deletions to Make String Balanced
1261+
/***
1262+
* 890. Find and Replace Pattern
12631263
*
1264-
* 參數s中只有'a' & 'b'這兩個字母。
1265-
* 刪除任一字母使s balanced,若不存在一對index (i,j) 使得 i < j 且 s[i] = 'b' 且 s[j] = 'a',則s 是balanced。
1266-
* 回傳最小須刪除幾次才能使s balanced
1267-
*
1268-
* @param {string} s
1269-
* @return {number}
1264+
* @param {string[]} words
1265+
* @param {string} pattern
1266+
* @return {string[]}
12701267
*/
1271-
var minimumDeletions = function(s) {
1272-
1273-
};
1274-
// let s = "aababbab";
1275-
/*Output: 2
1276-
Explanation: You can either:
1277-
Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or
1278-
Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
1279-
*/
1280-
// console.log(minimumDeletions(s));
1268+
var findAndReplacePattern = function(words, pattern) {
1269+
// solution 1.
1270+
// TC: O(n * m)
1271+
// let result = [];
1272+
// for(let i = 0;i < words.length;i++) {
1273+
// if(checkEqual(words[i],pattern)){
1274+
// result.push(words[i]);
1275+
// }
1276+
// }
1277+
// return result;
1278+
1279+
// /**
1280+
// * @param {string} a
1281+
// * @param {string} b
1282+
// * @return {boolean}
1283+
// */
1284+
// function checkEqual(a,b) {
1285+
// for(let i = 0;i < a.length;i++) {
1286+
// if(a.indexOf(a[i]) !== b.indexOf(b[i])){
1287+
// return false;
1288+
// }
1289+
// }
1290+
// return true;
1291+
// }
1292+
1293+
// solution 2.
1294+
// hash map
1295+
let result = [];
1296+
for(const a of words) {
1297+
if(checkEqual(a,pattern)){
1298+
result.push(a);
1299+
}
1300+
// console.log(a)
1301+
}
1302+
1303+
function checkEqual(a,b){
1304+
let map = new Map();
1305+
for(let i = 0;i < a.length;++i) {
1306+
if(!map.has(a[i])){
1307+
map.set(i,a[i]);
1308+
}
1309+
if(map.get(a[i]) ){
1310+
1311+
}
1312+
}
12811313

1314+
}
1315+
};
1316+
let word = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb";
1317+
// ["mee","aqq"]
1318+
// console.log(findAndReplacePattern(word,pattern));

0 commit comments

Comments
 (0)