Algorithm
-
[TIL] Algorithm 3Develope/Algorithm 2020. 5. 19. 11:32
1. 문제 2. 결과 const getLengthOfStr = str => { if(!str){return 0} let arr = []; let sliced = ""; for (let i in str) { if (sliced.includes(str[i])) { //for문을 돌면서 만약 같은 알파벳이 있다면 sliced = sliced.slice(sliced.indexOf(str[i]) + 1); }//빈문자열인 sliced for문을 돌면서 하나씩 잘라준다. sliced += str[i]; arr.push(sliced.length); } return Math.max(...arr); // 14 } getLengthOfStr("abcdefghcijklmnop");
-
[TIL] Algorithm 2Develope/Algorithm 2020. 5. 13. 21:29
1. 문제 2. 해결방안 const reverse = x => { let array = []; let strings = x.toString().split("").reverse().join(''); console.log(strings); for(let i =0; i < strings.length; i++) { if(strings[i] === '-') { array.unshift(strings[i]); }else { array.push(strings[i]); } } return Number(array.join('')); } reverse(-1234);
-
[TIL] Algorithm 1Develope/Algorithm 2020. 5. 13. 21:25
1. 문제 2. 해결방안 const twoSum = (nums, target) => { let array = []; for(let i = 0; i < nums.length; i++) { for(let y = 0; y < nums.length; y++){ if(nums[i] + nums[y] === target) { array.push(i , y); return array; } } } } twoSum([4, 9, 11, 14], 13)