릿코드(LEETCODE)
-
1290. Convert Binary Number in a Linked List to Integer릿코드(LEETCODE) 2020. 2. 13. 12:43
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/ Source shift operation /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: int getDecimalValue(ListNode* head) { int num = head->val; while(head = head->next) num = (num val; return num; } };
-
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..