본문 바로가기

릿코드

206. Reverse Linked List 주어진 LinkedList 를 Reverse 하는 문제. 복잡한 문제 같지만 천천히 생각해보면 그렇지 않다. 1) 첫번째 직관적인 솔루션 Prev라는 이전 노드를 저장하는 노드를 정의. 1 -> 2 -> 3 이라는 리스트가 있다면, Prev = nullptr 일때, head = 1 Prev = 1 일때, head = 2 Prev = 2 일때, head = 3 Prev = 3 일때, head = nullptr이 된다. 즉 새로운 Prev = head가 되고, 새로운 Prev의 next 는 Prev가 된다. 여기서 나는 tp라는 새로운 노드를 만들었고, tp 는 새로운 Prev 노드인데 이는, head->val의 값을 갖고 현재 prev를 next에 저장한다. 그리고 head 를 이동시켜준다. 이는 새로운 .. 더보기
392. Is Subsequence 이건 정말 설명할게 없어서, t 문자열을 스캔할 때, s 문자열에 대한 포인터를 유지하면서, 일치하는게 있으면 p ++ 아니면 t 문자열 인덱스로 넘어간다. 총 p의 갯수가 s 문자열의 길이와 같으면 subsequence 가 true 아니면 false 더보기
1480. Running Sum of 1d Array 사실 이 문제는 너무 쉬워서 설명할 것도 없지만, 최적화된 알고리즘을 위해, 생각해보아야 할 점을 몇 가지 짚어보겠다. [ Stage 1] 기본이다. Sum list라는 Vector에 숫자를 더해서 Sum_list[i] = Sum_list[i-1] + num[i] 라는 기본 점화식을 바탕으로 풀었다. Runtime: 14 ms [Stage 2] Vector 라는 container는 기본적으로 Push_back 이라는 함수를 제공해주고, Memory에 allocate되는 사이즈가 불명확하다. 그래서 필요 이상의 메모리를 잡고 있다. 따라서 array를 사용할 수 있다면 이 문제에서는 array를 사용하는 것이 맞다. 하지만 return type이 vector이기에 억지로 vector를 써줬다. 하지만 우리.. 더보기
724. Find Pivot Index Input: nums = [1,7,3,6,5,6] Output: 3 Explanation: The pivot index is 3. Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 Right sum = nums[4] + nums[5] = 5 + 6 = 11 Input: nums = [1,2,3] Output: -1 Explanation: There is no index that satisfies the conditions in the problem statement. Input: nums = [2,1,-1] Output: 0 Explanation: The pivot index is 0. Left sum = 0 (no elements to the left .. 더보기
LeetCode 75 Challenge https://leetcode.com/study-plan/leetcode-75/?progress=xo5m1ux2 LeetCode 75 - Study Plan - LeetCode This study plan is for those who want to prepare for technical interviews but are uncertain which problems they should focus on. The problems have been carefully curated so that levels 1 and 2 will guide beginner and intermediate users through problems tha leetcode.com Leet code 75 challenge를 시작한다 .. 더보기