전체 글
-
1342. Number of Steps to Reduce a Number to Zero릿코드(LEETCODE) 2020. 2. 13. 11:29
https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero Source class Solution { public: int numberOfSteps (int num) { int sol = 1; while(num !=1){ int cand = num & 1; if(cand) { cand -= 1; sol++; } num /= 2; sol++; } return sol; } };
-
70. Climbing Stairs릿코드(LEETCODE) 2020. 2. 12. 18:23
https://leetcode.com/problems/climbing-stairs/ Climbing Stairs - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 이해 계단을 오르는데 한 번에 1칸, 2칸 오를 수 있다. N 층을 올라갈 때 구할 수 있는 모든 경우의 수를 출력하는 문제. 접근 1 층 [1] 2 층 [1,1] [2] 3 층 [1,1,1] [1,2] [2,1] 4 층 [1,1,1,1] [1,1,2] [1,2,1] [2,1,1] [2,1,2] ..
-
746. Min Cost Climbing Stairs릿코드(LEETCODE) 2020. 2. 12. 18:14
https://leetcode.com/problems/min-cost-climbing-stairs/ Min Cost Climbing Stairs - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1,2 스탭을 움직일 수 있는데 가장 최소의 비용으로 계단을 올라가는 방법을 찾는 문제 DP 응용 문제이고 현재 위치에서 작은 값을 적산하여 출력한다. D 풀이 현재 위치 = 최소값(현재 위치 - 2, 현재 위치 - 1) 시작점은 0, 1 둘 중에 하나를 선택 class..
-
242. Valid Anagram릿코드(LEETCODE) 2020. 2. 12. 16:15
https://leetcode.com/problems/valid-anagram/ Source 28 ms bool isAnagram(string s, string t) { sort(s.begin(), s.end()); sort(t.begin(), t.end()); return s == t; } 8 ms class Solution { public: vector getTable(string s) { vector alphabet(27); for (auto a : s) { alphabet[a - 'a']++; } return alphabet; } bool isAnagram(string s, string t) { vector s_table = getTable(s); vector t_table = getTable(t..
-
56. Merge Intervals릿코드(LEETCODE) 2020. 2. 12. 13:34
https://leetcode.com/problems/merge-intervals/ 2차원 벡터의 각각의 요소의 값이 겹치는 부분이 없도록 병합을 하는 문제. input 이 몇 개가 들어오는지 명시되어 있지 않다. null 도 입력될 수 있으니 주의한다. Source 입력으로 들어오는 vector의 요소는 순서가 보장되어 있지 않다. 오름 차순 정렬을 하고 overlap 되는 구간을 찾아 병합한다. class Solution { public: vector merge(vector& intervals) { vector vvi; const int size = intervals.size(); if (size == 0) { return vvi; } sort(intervals.begin(), intervals.en..
-
520. Detect Capital릿코드(LEETCODE) 2020. 2. 12. 11:07
https://leetcode.com/problems/detect-capital 문자열이 모두 대문자이거나 소문자 이거나 첫 문자가 대문자고 나머지가 소문자인 경우 true class Solution { public: bool detectCapitalUse(string word) { string str = word; std::transform(str.begin(), str.end(),str.begin(), ::toupper); bool isUpper = str == word; std::transform(str.begin(), str.end(),str.begin(), ::tolower); bool isLower = str == word; bool onlyOne = false; if(word[0] >= 'A..
-
146. LRU Cache릿코드(LEETCODE) 2020. 2. 12. 09:59
https://leetcode.com/problems/lru-cache LRU Cache Discussion 에 있는 풀이 class LRUCache { using MyList = list; using Cache = unordered_map; int maxSize; Cache cache; MyList myList; int promote(int key, int value) { // assert key exists // move to front, O(1) myList.erase(cache[key]); myList.push_front({key, value}); // replace iterator to new begin cache[key] = myList.begin(); return value; } void e..
-
703. Kth Largest Element in a Stream릿코드(LEETCODE) 2020. 2. 11. 18:28
https://leetcode.com/problems/kth-largest-element-in-a-stream/ Kth Largest Element in a Stream - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com k 번째 엘리먼트를 출력하는 add 함수를 만드는 문제입니다. 모든 원소를 삽입할 필요 없이 k 번째까지만 삽입하고 k개 이상일 때 가장 작은 값을 삭제하는 식으로 풀이가 나은 듯 Dicussion에 있는 풀이 class KthLargest { ..