전체 글
-
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
-
119. Pascal's Triangle II릿코드(LEETCODE) 2020. 2. 14. 15:36
https://leetcode.com/problems/pascals-triangle-ii D 풀이 -0- 다구하고 그냥 반환함 2차원 벡터 만들 필요없이 vector 2 개 선언해서 풀어도 가능해 보임 time complexity O(n)^2 class Solution { public: vector getRow(int rowIndex) { vector arr; int cand = rowIndex + 1; arr.reserve(cand); for (int i = 1; i < cand + 1; i++) { vector vi; for (int j = 0; j < i; j++) { vi.push_back(1); } arr.push_back(vi); vi.clear(); } for (int i = 2; i < c..
-
118. Pascal's Triangle릿코드(LEETCODE) 2020. 2. 14. 15:21
https://leetcode.com/problems/pascals-triangle D 풀이 1 로 모두 다 채워 넣고 상위 요소에서 left, rigt 에 합으로 채우주는 방식으로 풀이 class Solution { public: vector generate(int numRows) { vector arr; // 모든 원소를 1로 다 채움 for (int i = 1; i < numRows + 1; i++) { vector vi; for (int j = 0; j < i; j++) { vi.push_back(1); } arr.push_back(vi); vi.clear(); } // arr[0] = 1 // arr[1] = 1 1 // arr[2] = 1 arr[1-1][1-1]+arr[1-1][1] 1 fo..
-
1347. Minimum Number of Steps to Make Two Strings Anagram릿코드(LEETCODE) 2020. 2. 14. 14:03
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/ D 풀이 anagram hello = olleh와 같이 같은 요소로 구성되어있는 문자열을 anagram이라고 함 t에서 몇 개의 alphabet 변경해야 s와 anagram 이 되는지 변경할 문자의 개수를 출력하는 문제 class Solution { public: int minSteps(string s, string t) { int alphabet[27] = { 0, }, alphabet2[27] = { 0, }; for (int i = 0; i < s.size(); i++) { alphabet[s[i] - 'a']++; alphabet2[t[i] - 'a'..
-
1346. Check If N and Its Double Exist릿코드(LEETCODE) 2020. 2. 14. 14:00
https://leetcode.com/problems/check-if-n-and-its-double-exist/ Source class Solution { public: bool checkIfExist(vector& arr) { unordered_set s; for (int n : arr) { if (s.count(2 * n) || (n % 2 == 0 && s.count(n / 2))) return true; s.insert(n); } return false; } };