Skip to content

Commit 944ca4c

Browse files
committed
add no. 2553 and no.2186
1 parent 65f01f4 commit 944ca4c

3 files changed

Lines changed: 124 additions & 153 deletions

File tree

javascript/LeetCode/Array/2553.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* 2553. Separate the Digits in an Array
3+
*
4+
* @param {number[]} nums
5+
* @return {number[]}
6+
*/
7+
var separateDigits = function(nums) {
8+
/*
9+
將參數元素依照位數拆開之後依照原本的順序合併
10+
*/
11+
// solution 1.
12+
// const ans = [];
13+
// for(let i = 0;i < nums.length;++i) {
14+
// const temp = [];
15+
// while(nums[i] > 0){
16+
// temp.push(nums[i] % 10);
17+
// nums[i] = Math.floor(nums[i] / 10);
18+
// }
19+
// for(let j = temp.length - 1;j >= 0;j--){
20+
// ans.push(temp[j])
21+
// }
22+
// }
23+
// return ans;
24+
25+
// solution 2.
26+
// Reverse Traversal
27+
const ans = [];
28+
for(let i = nums.length - 1;i >= 0 ;i--){
29+
while(nums[i] > 0){
30+
ans.push(nums[i] % 10);
31+
nums[i] = Math.floor(nums[i] / 10);
32+
}
33+
}
34+
ans.reverse();
35+
return ans;
36+
};
37+
let nums = [13,25,83,77];
38+
/*
39+
Output: [1,3,2,5,8,3,7,7]
40+
Explanation:
41+
- The separation of 13 is [1,3].
42+
- The separation of 25 is [2,5].
43+
- The separation of 83 is [8,3].
44+
- The separation of 77 is [7,7].
45+
answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.
46+
*/
47+
console.log(separateDigits(nums));

javascript/LeetCode/String/2186.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* 2186. Minimum Number of Steps to Make Two Strings Anagram II
3+
*
4+
* 兩個字串參數s & t,在一次操作中,可以加上任一字母至s或t中。
5+
* 回傳讓s和t變成anagrams的最少步驟數
6+
*
7+
* anagrams:長度一樣、字母一樣但排序可以不一樣
8+
*
9+
* @param {string} s
10+
* @param {string} t
11+
* @return {number}
12+
*/
13+
var minSteps = function(s, t) {
14+
// 檢查是否是anagrams可用:sort、count
15+
let countOP = 0;
16+
let mapS = new Map();
17+
let mapT = new Map();
18+
// 參數s字母出現次數
19+
for (let i = 0; i < s.length; i++) {
20+
const element = s[i];
21+
mapS.has(element) ? mapS.set(element, mapS.get(element) + 1) : mapS.set(element, 1);
22+
}
23+
// 參數t字母出現次數
24+
for (let i = 0; i < t.length; i++) {
25+
const element = t[i];
26+
mapT.has(element) ? mapT.set(element, mapT.get(element) + 1) : mapT.set(element, 1);
27+
}
28+
29+
// 字母出現幾次就得是幾次
30+
// t有但s沒有的字母有幾個 (a,s) 2
31+
// s有但t沒有的字母有幾個 (l,e,e,d,e) 5
32+
// 因此 2 + 5 = 7
33+
for(let [key,value] of mapS){
34+
if(!mapT.has(key)){
35+
countOP+= value;
36+
}
37+
38+
}
39+
for(let [key,value] of mapT){
40+
if(!mapS.has(key)){
41+
countOP+= value;
42+
}
43+
}
44+
return countOP;
45+
46+
// solution 2.
47+
// use obj
48+
// let countOP = 0;
49+
// let freq = {};
50+
// for (const element of s) {
51+
// freq[element] = freq[element] || 0) + 1;
52+
// }
53+
// for(const element of t) {
54+
// if(!freq[element]){
55+
// continue;
56+
// }
57+
// --freq[element];
58+
// ++countOP;
59+
// }
60+
// return s.length + t.length - countOP * 2;
61+
};
62+
// let s = "leetcode", t = "coats";
63+
/**
64+
* 7
65+
* - In 2 steps, we can append the letters in "as" onto s = "leetcode", forming s = "leetcodeas".
66+
* - In 5 steps, we can append the letters in "leede" onto t = "coats", forming t = "coatsleede".
67+
* "leetcodeas" and "coatsleede" are now anagrams of each other.
68+
* We used a total of 2 + 5 = 7 steps.
69+
* It can be shown that there is no way to make them anagrams of each other with less than 7 steps.
70+
*/
71+
// let s = "cotxazilut",t = "nahrrmcchxwrieqqdwdpneitkxgnt";
72+
// 27
73+
// console.log(minSteps(s,t));

javascript/index.js

Lines changed: 4 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -149,52 +149,6 @@ const num1 = "123456789",num2 = "987654321";
149149

150150

151151

152-
/**
153-
* @param {number[]} nums
154-
* @param {number} target
155-
* @return {number[]}
156-
*/
157-
var twoSum = function (nums, target) {
158-
159-
// two pointer solution.Big O(n)
160-
if (nums.length < 1 || !target) {
161-
return;
162-
}
163-
164-
nums.sort((a, b) => a - b);
165-
166-
let left = 0;
167-
let right = nums.length - 1;
168-
let result = [];
169-
170-
while (left < right) {
171-
if (nums[left] + nums[right] === target) {
172-
// return [left + 1, right + 1];
173-
result.push(left, right);
174-
left++;
175-
right--;
176-
} else if (nums[left] + nums[right] < target) {
177-
left++;
178-
} else {
179-
right--;
180-
}
181-
}
182-
return result;
183-
};
184-
// const target = 9;
185-
// const nums = [2, 7, 11, 15];
186-
// ^ ^
187-
// Output: [0,1]
188-
// Output: Because nums[0] + nums[1] == 9, we return [0, 1].
189-
190-
// const nums = [3, 2, 4];
191-
// const target = 6;
192-
// Output: [1,2]
193-
// nums[1] + nums[2] = 6
194-
// console.log(twoSum(nums, target));
195-
196-
197-
198152
/**
199153
* 1415. The k-th Lexicographical String of All Happy Strings of Length n
200154
*
@@ -250,6 +204,7 @@ var getHappyString = function (n, k) {
250204

251205
/**
252206
* 2099. Find Subsequence of Length K With the Largest Sum
207+
* Level:Hard
253208
*
254209
* You are given an integer array nums and an integer k. You want to find a subsequence of nums of length k that has the largest sum.
255210
* Return any such subsequence as an integer array of length k.
@@ -322,80 +277,6 @@ var maxSubsequence = function(nums, k) {
322277
// 「3,3]
323278
// console.log(maxSubsequence(nums,k))
324279

325-
/**
326-
* 2186. Minimum Number of Steps to Make Two Strings Anagram II
327-
*
328-
* 兩個字串參數s & t,在一次操作中,可以加上任一字母至s或t中。
329-
* 回傳讓s和t變成anagrams的最少步驟數
330-
*
331-
* anagrams:長度一樣、字母一樣但排序可以不一樣
332-
*
333-
* @param {string} s
334-
* @param {string} t
335-
* @return {number}
336-
*/
337-
var minSteps = function(s, t) {
338-
// 檢查是否是anagrams可用:sort、count
339-
let countOP = 0;
340-
let mapS = new Map();
341-
let mapT = new Map();
342-
// 參數s 字母出現次數
343-
for (let i = 0; i < s.length; i++) {
344-
const element = s[i];
345-
mapS.has(element) ? mapS.set(element, mapS.get(element) + 1) : mapS.set(element, 1);
346-
}
347-
// 參數t 字母出現次數
348-
for (let i = 0; i < t.length; i++) {
349-
const element = t[i];
350-
mapT.has(element) ? mapT.set(element, mapT.get(element) + 1) : mapT.set(element, 1);
351-
}
352-
353-
// 字母出現幾次就得是幾次
354-
// t有但s沒有的字母有幾個 (a,s) 2
355-
// s有但t沒有的字母有幾個 (l,e,e,d,e) 5
356-
for(let [key,value] of mapS){
357-
if(!mapT.has(key)){
358-
countOP+= value;
359-
}
360-
361-
}
362-
for(let [key,value] of mapT){
363-
if(!mapS.has(key)){
364-
countOP+= value;
365-
}
366-
}
367-
return countOP;
368-
369-
// solution 2.
370-
// use obj
371-
// let countOP = 0;
372-
// let freq = {};
373-
// for (const element of s) {
374-
// freq[element] = freq[element] || 0) + 1;
375-
// }
376-
// for(const element of t) {
377-
// if(!freq[element]){
378-
// continue;
379-
// }
380-
// --freq[element];
381-
// ++countOP;
382-
// }
383-
// return s.length + t.length - countOP * 2;
384-
};
385-
// let s = "leetcode", t = "coats";
386-
/**
387-
* 7
388-
* - In 2 steps, we can append the letters in "as" onto s = "leetcode", forming s = "leetcodeas".
389-
* - In 5 steps, we can append the letters in "leede" onto t = "coats", forming t = "coatsleede".
390-
* "leetcodeas" and "coatsleede" are now anagrams of each other.
391-
* We used a total of 2 + 5 = 7 steps.
392-
* It can be shown that there is no way to make them anagrams of each other with less than 7 steps.
393-
*/
394-
// let s = "cotxazilut",t = "nahrrmcchxwrieqqdwdpneitkxgnt";
395-
// 27
396-
// console.log(minSteps(s,t));
397-
398-
399280

400281
/**
401282
* Largest three in an array
@@ -475,10 +356,11 @@ var kthDigit = function(a,b,k){
475356
* */
476357
var fractionRecurringDecimal = function (a,b) {
477358
// 回傳a,b的分數,若小數點皆是重複數字,則加上括號 => .(XXX)
359+
console.log(Math.floor(b / a))
478360
}
479-
// let a = 1,b = 2;
361+
let a = 1,b = 2;
480362
// "0.5"
481-
// console.log(fractionRecurringDecimal(a,b));
363+
console.log(fractionRecurringDecimal(a,b));
482364

483365
/**
484366
* Recurring Sequence in a Fraction
@@ -1405,34 +1287,3 @@ let s = "010";
14051287
// Explanation: Because there is just one '1', it must be in the last position. So the answer is "001".
14061288
// console.log(maximumOddBinaryNumber(s));
14071289

1408-
/**
1409-
* 2553. Separate the Digits in an Array
1410-
*
1411-
* @param {number[]} nums
1412-
* @return {number[]}
1413-
*/
1414-
var separateDigits = function(nums) {
1415-
/*
1416-
將參數元素依照位數拆開之後依照原本的順序合併
1417-
*/
1418-
let ans = [];
1419-
for(let i = 0;i < nums.length;++i) {
1420-
while(nums[i] > 0){
1421-
ans.push(nums[i] % 10);
1422-
nums[i]/=10;
1423-
}
1424-
}
1425-
console.log(ans);
1426-
1427-
};
1428-
let nums = [13,25,83,77];
1429-
/*
1430-
Output: [1,3,2,5,8,3,7,7]
1431-
Explanation:
1432-
- The separation of 13 is [1,3].
1433-
- The separation of 25 is [2,5].
1434-
- The separation of 83 is [8,3].
1435-
- The separation of 77 is [7,7].
1436-
answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.
1437-
*/
1438-
console.log(separateDigits(nums));

0 commit comments

Comments
 (0)