릿코드(LEETCODE)
-
1366. Rank Teams by Votes릿코드(LEETCODE) 2020. 3. 3. 11:33
https://leetcode.com/problems/rank-teams-by-votes/ a~z 까지 알파벳의 가중치의 합과 정렬을 이용 struct Ele { char ch; vector cnt; Ele() { cnt.resize(26, 0); } }; bool operator b.cnt; return a.ch < b.ch; } class Solution { public: string rankTeams(vector& votes) { map my; for (int i = 0; i < votes.size(); ++i) { for (int j = 0; j < votes[i].size()..
-
733. Flood Fill릿코드(LEETCODE) 2020. 3. 3. 11:32
https://leetcode.com/problems/flood-fill/ sr, sc row, col 의 크기가 다를 수 있음을 주의 const int dx[] = { -1, 0, 0, 1 }; const int dy[] = { 0, -1, 1, 0 }; class Solution { public: void dfs(vector& image, int sr, int sc, int newColor, int dstColor) { image[sr][sc] = newColor; for (int i = 0; i = image[0].size()) { continue; } if (cy < 0..
-
463. Island Perimeter릿코드(LEETCODE) 2020. 3. 3. 11:30
https://leetcode.com/problems/island-perimeter/ 현재위치에서 4방향으로 이웃한 점이 있다면 그 개수만큼 - class Solution { public: int islandPerimeter(vector& grid) { int count = 0; int rows = grid.size(); int cols = grid[0].size(); for (int i = 0; i = 0 && grid[i - 1][j]) count--; if (i + 1 < rows && grid[i + 1][j]) count--; i..
-
1207. Unique Number of Occurrences릿코드(LEETCODE) 2020. 2. 28. 17:05
https://leetcode.com/problems/unique-number-of-occurrences 이전값고 다를때 hash 내의 중복수가 있는지 확인하고 없으면 hash 에 삽입 있으면 중복된 카운트가 있음으로 false 를 리턴 한다. class Solution { public: unordered_map m; bool check(int count) { return m.count(count); } bool uniqueOccurrences(vector& arr) { sort(arr.begin(), arr.end()); int prev = arr[0]; int prev_count = 0; for (int i = 0; i < arr.size(); i++) { int current = arr[i]; if..
-
1310. XOR Queries of a Subarray릿코드(LEETCODE) 2020. 2. 16. 16:55
https://leetcode.com/problems/xor-queries-of-a-subarray/ Source class Solution { public: vector xorQueries(vector& arr, vector& queries) { vector result; for (int i = 0; i < queries.size(); i++) { int left = queries[i][0]; int right = queries[i][1]; int cand = arr[left]; for (int j = left; j < right; j++) { cand = cand ^ arr[j+1]; } result.push_back(cand); } return result; } };
-
1309. Decrypt String from Alphabet to Integer Mapping릿코드(LEETCODE) 2020. 2. 16. 16:38
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/ Source class Solution { public: string freqAlphabets(string s) { string ret; int size = s.size(); int pos = 0; for (int i = 0; i < size; ) { unsigned char code[3]; code[0] = s[i]; code[1] = code[2] = -1; if (i + 1 < size) code[1] = s[i + 1];; if (i + 2 < size) code[2] = s[i + 2]; if (code[2] == '#') { string d; d.push..
-
1352. Product of the Last K Numbers릿코드(LEETCODE) 2020. 2. 16. 15:55
https://leetcode.com/problems/product-of-the-last-k-numbers/ 입력으로 들어오는 숫자가 들어온다. getProuct() 는 배열의 뒷 부분부터의 요소의 곱을 출력 한다. cmd 원소 출력 1 add(1) [1] 2 add(2) [1, 2] 3 add(3) [1, 2, 3] 4 add(0) [1, 2, 3, 0] 5 add(8) [1, 2, 3, 0, 8] 6 add(2) [1, 2, 3, 0, 8, 2] 7 getProduct(1) [1, 2, 3, 0, 8, 2] 2 8 getProduct(2) [1, 2, 3, 0, 8, 2] 16 9 add(8) [1, 2, 3, 0, 8, 2, 8] 10 getProduct(1) [1, 2, 3, 0, 8, 2, 8]..
-
1143. Longest Common Subsequence릿코드(LEETCODE) 2020. 2. 15. 17:06
https://leetcode.com/problems/longest-common-subsequence/ 입력이 아래와 같을 때 text1 = "abcde", text2 = "ace" a b c d e a 1 1 1 1 1 a -> ace c 1 1 2 2 2 c -> ace e 2 2 2 2 3 e -> ace Source class Solution { public: int dp[1001][1001]; int longestCommonSubsequence(string text1, string text2) { int sol = 0; for (int i = 1; i