리트코드 (2) 썸네일형 리스트형 [Leetcode/C++] 551. Student Attendance Record I class Solution { public: bool checkRecord(string s) { return s.find("LLL") != string::npos || count(s.begin(), s.end(), 'A') > 1 ? false : true; } }; string에 내장되어있는 find 함수를 이용해 LLL을 찾았거나 결석(A) 가 2번이상이면 false를 리턴하도록 구현하였다. stiring의 find 함수는 algorithm의 find 함수와 다르게 find(찾을문자, 시작점)을 주어준다. 문제: https://leetcode.com/problems/student-attendance-record-i/ Student Attendance Record I - LeetCode Level up.. [LeetCode/C++] 14. Longest Common Prefix class Solution { public: string longestCommonPrefix(vector& strs) { if(strs.empty()) return ""; sort(strs.begin(), strs.end()); string smallest = strs[0]; string biggest = strs[strs.size()-1]; string answer = ""; for(int i = 0; i < biggest.size(); i++) { if(smallest[i] == biggest[i]) answer += smallest[i]; else break; } return answer; } }; 맨 처음에는 모든 벡터를 돌아가면서 첫 문자부터 가장 길이가 짧은 문자열까지 반복하며 비교하는 것을 .. 이전 1 다음