본문 바로가기

HOME

(207)
[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에서 &를 이용해 복사 연산자를 사용하면..
[백준/C++] 1427번 소트인사이드 #include #include #include using namespace std; int main() { string answer; cin >> answer; sort(answer.begin(), answer.end(), greater()); cout
[백준/C++] 11650번 좌표 정렬하기 #include using namespace std; bool compare(pair &a, pair &b) { if(a.first == b.first) { return a.second > N; vector arr(N); for(int i = 0; i > arr[i].first >> arr[i].second; } sort(arr.begin(), arr.end(), compare); for(int i = 0; i < N; i++) { cout
[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..
[Leetcode/C++] 168. Excel Sheet Column Title class Solution { public: string convertToTitle(int n) { string answer = ""; while(n != 0) { answer = (char)((n - 1) % 26 + 'A') + answer; n = (n-1) / 26; } return answer; } }; 그냥 단순한 사칙연산과 구현문제였는데 26으로 나누고 마드하는 전형적인 문제이다. 문제: https://leetcode.com/problems/excel-sheet-column-title/ Excel Sheet Column Title - LeetCode Level up your coding skills and quickly land a job. This is the best place to ex..
[Leetcode/C++] 682. Baseball Game class Solution { public: int calPoints(vector& ops) { vector answer; int temp1 = 0, temp2 = 0; for(auto& it : ops) { if(it == "+") { temp1 = answer.back(); answer.pop_back(); temp2 = answer.back(); answer.emplace_back(temp1); answer.emplace_back(temp2+temp1); } else if(it == "C") { answer.pop_back(); } else if(it == "D") { temp1 = answer.back(); answer.emplace_back(temp1*2); } else { answer.empl..
[Leetcode/C++] 206. Reverse Linked List /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { ListNode *prev = nullptr; ListNode *curr = head; while(head != nullptr) { curr = curr->next;..