본문 바로가기
반응형

알고리즘222

[알고리즘] Goal Parser Interpretation 포인트 1. 그냥 떠올리는 거 편한거를 쓰는 것이 좋을 것 같다. 공부할때는 어렵게 할때는 쉽게포인트 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. c++/cpp class Solution { public: string interpret(string command) { stack st; string result; for(auto ele : command){ if(ele=='G'){ result+=ele; continue; } if(ele=='('){ st.push(ele); continue; } if(st.top()=='(' && ele == ')'){ st.pop(); result+='o'; continue; } if(st.top()=='l'&& ele==')'){ st.pop(); st.p.. 2021. 2. 1.
[알고리즘] Count the Number of Consistent Strings 포인트 1. 연관되어 있는 단어, 포함되어 있는 단어를 찾는 문제, 적절히 set과 map 을 사용하여 해결하자 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. c++/cpp #include using namespace std; class Solution { public: int countConsistentStrings(string allowed, vector &words) { int count = 0; for(auto ele1: words){ unordered_set set(ele1.begin(), ele1.end()); for(auto ele2 : set){ if(allowed.find(ele2)==string::npos){ count+=1; break; } } } return words.s.. 2021. 1. 31.
[알고리즘] Check If Two String Arrays are Equivalent 포인트 1. string 을 합쳐서 비교를 할 수 있는가? 를 물어보는 문제 해결 방법은 간단하다. 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. cpp class Solution { public: bool arrayStringsAreEqual(vector& word1, vector& word2) { string a; for (auto ele1: word1){ a+=ele1; } string b; for(auto ele2: word2){ b+=ele2; } return a==b; } }; python class Solution: def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool: return (''.join(w.. 2021. 1. 31.
[알고리즘] 짝지어 제거하기 포인트 1. 스택을 사용할 수 있는가? 정말 신기하게도 문자열을 다루는 것들에서는 Stack 을 많이 쓴다. c++/cpp #include #include #include using namespace std; int solution(string s) { int answer = 0; stack stack; for (int i = 0; i < s.size(); i++) { // 1. 스택이 비어 있거나 이전에 스택 top 의 값이 현재 s[i] 와 다르다면 push if (stack.empty() || stack.top() != s[i]) { stack.push(s[i]); } else { // 2. 스택 top 의 값이 현재 s[i]와 같다면 top 에 있는 값을 pop stack.pop(); } } //.. 2021. 1. 28.
반응형