본문 바로가기

코테

(32)
LeetCode Top Interview 150 - 3. Longest Substring Without Repeating Characters 🤔 문제 이해 입력된 문자열에서 반복되는 문자 제외 제일 긴 문자열의 길이를 구하라 🤔 EX Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a ..
LeetCode Top Interview 150 - 167. Two Sum II - Input Array Is Sorted 🤔 문제 이해 비정렬된 배열에서 두개의 value을 더해 target값이 맞는 index값 두개 나오기 🤔 EX Example 1: Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2]. Example 2: Input: numbers = [2,3,4], target = 6 Output: [1,3] Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3]. Example 3: Input: numbers ..
LeetCode Top Interview 150 - 125. Valid Palindrome 🤔 문제 이해 대문자인 문자를 소문자로 변경 후 숫자와 알파벳이 아닌 글자는 삭제 + 띄어 쓰기 포함 앞에서 읽는것과 뒤에서부터 읽는게 회문으로 같아야 한다. 🤔 EX Example 1: Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome. Example 2: Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome. Example 3: Input: s = " " Output: true Explanation: s is an empty string "" after removi..
LeetCode Top Interview 150 - 189. Rotate Array var rotate = function(nums, k) { k = k % nums.length for(let i=0;i
LeetCode Top Interview 150 - 80. Remove Duplicates from Sorted Array II 같은수는 2개까지는 배열에 저장하지만 3번째부터는 저장안한다. 가장 간단한 방법을 생각하고 싶었고 해당 수가 몇개있는지를 확인하기 위해서 굳이 map을 안써도 괜찮지만 제일 편리하게 작성할수 있을 것이라고 생각해서 Map()으로 작성하였습니다. var removeDuplicates = function(nums) { //의사코드 //Map()을 통해 배열에 겹치는 수가 1개 이상인지 확인할것 const map = new Map() let index = 0 for(let i =0;i
LeetCode Top Interview 150 - 121. Best Time to Buy and Sell Stock 처음 for문을 중첩으로 작성해주면 시간제한에 걸려서 통과를 못해줬다(시간복잡도를 생각 하지 않았다는...). 그래서 반복문 한번만 작성해주면서 처음 작성한 코드를 조금 바꿔 주면서 최대 이익을 찾는 로직을 찾아 주었다. 최대 이익을 찾으려면 제일 작은 수일때 구매해야하고 제일 큰수일 때 판매해야한다. //for문안에서 돌면서 max값찾기 ->O(N^2) (test만 되고 submit하면 시간제한에 걸린다) // var maxProfit = function(prices) { // let maxProfit = 0 // for(let i=0;i
LeetCode Top Interview 150 - 26. Remove Duplicates from Sorted Array 중복된 값을 제거하기에는 Set()이 제일 효율적이라고 생각했다. Set()을 사용한 김에 무엇이고 어떻게 사용하는지 알아보겠습니다. /** * @param {number[]} nums * @return {number} */ var removeDuplicates = function(nums) { let unique = new Set() let index = 0 for(let i=0;i < nums.length;i++){ if(!unique.has(nums[i])){ unique.add(nums[i]) nums[index]=nums[i] index++ } } return index }; Set 이란? - value값 만을 저장하며 중복을 허용하지 않는 Collection 입니다. - 대소문자를 구분합니다...
LeetCode Top Interview 150 - 80. Remove Duplicates from Sorted Array II 같은수는 2개까지만 배열에 저장할수 있습니다 //의사코드 map()을 이용 map()에 숫자가 없으면 숫자를 넣고 있으면 1일때만 또 추가하는걸 if문을 통해 해줌 /** * @param {number[]} nums * @return {number} */ var removeDuplicates = function(nums) { const map = new Map() let index = 0 for(let i =0;i