Skip to content

Commit 4cd06a5

Browse files
committed
add 804
1 parent 27f430e commit 4cd06a5

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

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/index.js

Lines changed: 3 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,5 @@ 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+

0 commit comments

Comments
 (0)