본문 바로가기

Problem Solving/리트코드(leetcode)

(29)
[Leetcode/C++] 169. Majority Element class Solution { public: int majorityElement(vector& nums) { sort(nums.begin(), nums.end()); return nums[nums.size()/2]; } }; 이 문제는 현재 배열에서 과반수(50%이상)을 차지하고 있는 숫자를 찾아내는 문제입니다. 저는 간단하게 sorting한 이후 배열의 가운데에서 있는 숫자를 리턴하도록 하였습니다. 왜냐하면 과반수니까 가운데 부분을 무조건 차지하고 있을테니 이렇게 하였습니다. 장점은 따로 숫자를 세지 않기 때문에 솔팅하는 시간 O(nlogn)만큼만 걸립니다! 문제: https://leetcode.com/problems/majority-element/ Majority Element - LeetCode L..
[Leetcode/C++] 509. Fibonacci Number class Solution { public: int fib(int n) { if(n == 0) return 0; return n > 1 ? fib(n-1) + fib(n-2) : 1; } }; recurrsion (재귀함수) 을 처음 이해하기 가장 좋은 문제입니다. 문제: https://leetcode.com/problems/fibonacci-number/ Fibonacci Number - 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
[Leetcode/C++] 125. Valid Palindrome class Solution { public: bool isPalindrome(string s) { string str; for(char it : s) { if(isalpha(it)) str+=tolower(it); if(isdigit(it)) str+=it; } int start = 0, end = str.length()-1; for(; start end) break; if(str[start] != str[end]) return false; } return true; } }; 가운데 문자를 기준으로 앞뒤가 같은 글자인지 확인하는 문제이다. 한국어로 치면(토마토, 기러기 ...) 문자나 숫자면 새로운 string에 넣어두고 아..
[Leetcode/C++] 412. Fizz Buzz class Solution { public: vector fizzBuzz(int n) { vector answer; for(int i = 1; i
[Leetcode/C++] 1154. Day of the Year class Solution { public: int dayOfYear(string date) { int answer = 0; int data = stoi(date.substr(5,2)) -1; int year = stoi(date.substr(0,4)); for(; data > 0; data--) { if(data == 2) { answer += 28; if(year % 400 == 0 && year % 100 ==0 && year % 4 ==0) { answer += 1; } else if(year % 100 != 0 && year % 4 == 0) answer += 1; } else if(data 7 && da..
[Leetcode/C++] 1047. Remove All Adjacent Duplicates In String class Solution { public: string removeDuplicates(string s) { string answer; for(auto &ch : s) { if(answer.back() == ch) answer.pop_back(); else answer.push_back(ch); } return answer; } }; 중복되는 문자열을 제거하라는 것이 이번 문제의 요약이다! 그런데 문자열을 새로운 문자열에 넣는다고 생각하면 stack자료구조를 이용할 수 있다! Stack에 새로 넣는 것과 가장 top에 쌓여있는 것이 같다면 stack에서 꺼내는 것으로 생각하면 문제가 단순해진다. 이 코드로 빠르면 runtime이 13ms까지는 나온다 만약 auto에서 &를 이용해 복사 연산자를 사용하면..
[Leetcode/C++] 1051. Height Checker class Solution { public: int heightChecker(vector& heights) { vector answer; answer.assign(heights.begin(), heights.end()); sort(answer.begin(), answer.end()); int count= 0; for(int i = 0; i < answer.size(); i++) { if(answer[i] != heights[i]) count++; } return count; } }; 왜 문제가 height checker인지 모르겠다. 뒤에 숫자가 작으면 안보이는 그런 문제를 생각했는데 그냥 sorting 되지 않은 것이 몇개인가 출력하는 문제였다. 같은 배열을 assign하여 받아오고 sort한 뒤 몇개..
[Leetcode/C++] 67. Add Binary class Solution { public: string addBinary(string a, string b) { reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int carry = 0; string answer = ""; for(int i = 0; i < min(a.length(),b.length()); i++) { if(a[i] == b[i]) { carry == 1 ? answer += "1" : answer += "0"; a[i] == '1' ? carry = 1 : carry = 0; } else if(a[i] != b[i] && carry == 1) { answer += "0"; carry = 1; } else { answer += "1..