Skip to content

Commit ebe7faa

Browse files
authored
Merge pull request #157 from clingoram/mavis
新增解題
2 parents 8304b81 + f44964b commit ebe7faa

4 files changed

Lines changed: 136 additions & 2 deletions

File tree

javascript/LeetCode/Array/1967.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 1967. Number of Strings That Appear as Substrings in Word
3+
*
4+
* @param {string[]} patterns
5+
* @param {string} word
6+
* @return {number}
7+
*/
8+
var numOfStrings = function(patterns, word) {
9+
/**
10+
* 計算patterns中有幾個元素出現在word中
11+
*/
12+
let count = 0;
13+
for(let i = 0;i < patterns.length;++i) {
14+
if(word.includes(patterns[i])){
15+
count++;
16+
}
17+
}
18+
return count;
19+
};
20+
let patterns = ["a","abc","bc","d"], word = "abc";
21+
/*
22+
Output: 3
23+
Explanation:
24+
- "a" appears as a substring in "abc".
25+
- "abc" appears as a substring in "abc".
26+
- "bc" appears as a substring in "abc".
27+
- "d" does not appear as a substring in "abc".
28+
3 of the strings in patterns appear as a substring in word.
29+
*/
30+
console.log(numOfStrings(patterns,word));
31+

javascript/LeetCode/Array/804.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 804. Unique Morse Code Words
3+
*
4+
* 給一個字串陣列作為參數,把該陣列內的元素一一拆開成字元,去比對每個字元轉換成摩斯密碼後共有幾個不同的
5+
*
6+
* 英文字母是小寫
7+
* @param {string[]} words
8+
* @return {number}
9+
*/
10+
var uniqueMorseRepresentations = function(words) {
11+
// 26英文字母分別代表的morse code
12+
let morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."];
13+
14+
let set = new Set();
15+
for(const letter of words){
16+
let combine = "";
17+
for(const a of letter) {
18+
combine += morse[a.charCodeAt(0) - 'a'.charCodeAt(0)];
19+
}
20+
set.add(combine);
21+
}
22+
return set.size;
23+
};
24+
let words = ["gin","zen","gig","msg"]
25+
/*
26+
Output: 2
27+
Explanation: The transformation of each word is:
28+
"gin" -> "--...-."
29+
"zen" -> "--...-."
30+
"gig" -> "--...--."
31+
"msg" -> "--...--."
32+
There are 2 different transformations: "--...-." and "--...--.".
33+
*/
34+
console.log(uniqueMorseRepresentations(words));

javascript/LeetCode/String/3340.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 3340. Check Balanced String
3+
*
4+
* 參數為字串數字,檢查index 偶數和基數各自相加後是否相等。
5+
*
6+
* @param {string} num
7+
* @return {boolean}
8+
*/
9+
var isBalanced = function(num) {
10+
// solution
11+
// TC:O(N)
12+
let oddSum = 0, evenSum = 0;
13+
for(let i = 0;i < num.length;++i) {
14+
if(i % 2 === 0){
15+
evenSum += parseInt(num[i]);
16+
}else{
17+
oddSum += parseInt(num[i]);
18+
}
19+
}
20+
return evenSum === oddSum;
21+
};
22+
let num = "1234";
23+
console.log(isBalanced(num));

javascript/index.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ var longestPalindrome = function(words) {
836836

837837

838838
};
839-
let words = ["ab","ty","yt","lc","cl","ab"];
839+
// let words = ["ab","ty","yt","lc","cl","ab"];
840840
// 8
841841
// "ty" + "lc" + "cl" + "yt" = "tylc clyt"
842842
// abtyytba
@@ -1383,4 +1383,50 @@ Query 0: The element at queries[0] = 0 is nums[0] = 1. The nearest index with th
13831383
Query 1: The element at queries[1] = 3 is nums[3] = 4. No other index contains 4, so the result is -1.
13841384
Query 2: The element at queries[2] = 5 is nums[5] = 3. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: 5 -> 6 -> 0 -> 1).
13851385
*/
1386-
// console.log(solveQueries(nums,queries));
1386+
// console.log(solveQueries(nums,queries));
1387+
1388+
/**
1389+
* 645. Set Mismatch
1390+
*
1391+
* 參數為數字陣列,從1至n,但內有重複的元素,找出它們並調整成對的元素
1392+
*
1393+
* @param {number[]} nums
1394+
* @return {number[]}
1395+
*/
1396+
var findErrorNums = function(nums) {
1397+
let res = [];
1398+
let map = new Map();
1399+
for(const ele of nums){
1400+
map.has(ele) ? map.set(ele,map.get(ele) + 1 ) : map.set(ele,1);
1401+
}
1402+
// console.log(map)
1403+
for(const [key,value] of map) {
1404+
if(value >= 2){
1405+
console.log(key);
1406+
1407+
}
1408+
}
1409+
};
1410+
let nums = [1,2,2,4];
1411+
// [2,3]
1412+
// console.log(findErrorNums(nums));
1413+
1414+
1415+
/**
1416+
* 2864. Maximum Odd Binary Number
1417+
*
1418+
* @param {string} s
1419+
* @return {string}
1420+
*/
1421+
var maximumOddBinaryNumber = function(s) {
1422+
/**
1423+
* binary string s包含至少一個'1'
1424+
* rearrange 參數s成最大的奇二進制數
1425+
* 回傳的結果值開頭可以是0
1426+
*/
1427+
1428+
};
1429+
let s = "010";
1430+
// Output: "001"
1431+
// Explanation: Because there is just one '1', it must be in the last position. So the answer is "001".
1432+
console.log(maximumOddBinaryNumber(s));

0 commit comments

Comments
 (0)