@@ -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 * */
476357var 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