릿코드(LEETCODE)
-
896. Monotonic Array릿코드(LEETCODE) 2020. 2. 10. 08:05
https://leetcode.com/problems/monotonic-array 불러오는 중입니다... 증가하거나, 감소하거나 인지 확인 하는 문제 키워드 - 단조함수 입력으로 들어오는 벡터의 개별 요소가 증가 하거나, 감소하는지를 판단하는 문제 벡터의 개별 요소 판정 1 2 3 4 5 6 7 8 9 증가함으로 O 9 8 7 6 4 3 2 1 감소함으로 O 4 5 6 4 8 9 2 1 4 5 6 4 증가하다가 감소한다 X Source class Solution { public: bool isMonotonic(vector& A) { bool inc = true; bool dec = true; for (int i = 0; i A[i+1]) inc =..
-
7. Reverse Integer - no solution릿코드(LEETCODE) 2020. 2. 9. 11:43
https://leetcode.com/problems/reverse-integer/ Reverse Integer - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com signed int n 을 리버스 하는 문제다. reverse 했을때 int 유효범위가 넘어갈때 처리하는 방법이 키포인트인데 방법을 아직 못찾았다. 기록용으로 남겨 두자 나중에 풀자. INT 유효범위 체크를 안한 틀린 코드 #include class Solution { public: int revers..
-
190. Reverse Bits릿코드(LEETCODE) 2020. 2. 9. 00:45
https://leetcode.com/problems/reverse-bits/ Reverse Bits - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com bit 를 좌우로 뒤집는 문제 입력 n 은 오른쪽 으로 쉬프트 하면서 마지막 에 비트가 있는지확인 sol 은 왼쪽으로 쉬프트하면서 or 연산 class Solution { public: uint32_t reverseBits(uint32_t n) { uint32_t sol = 0; for(int i=0; i 1; ..
-
191. Number of 1 Bits릿코드(LEETCODE) 2020. 2. 9. 00:34
https://leetcode.com/problems/number-of-1-bits/submissions/ Number of 1 Bits - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 입력으로 들어오는 unsinged int 자료형에서 bit 의 개수를 출력하는 문제 1 비트씩 shift 해서 출력하는 방법이 있고. class Solution { public: int hammingWeight(uint32_t n) { int sol = 0; while(n) {..
-
231. Power of Two릿코드(LEETCODE) 2020. 2. 8. 20:18
https://leetcode.com/problems/power-of-two 불러오는 중입니다... 와 이거 정말 환장하겠네 엄청 쉽다고 생각한 문제였는데 8번 연속 틀려서 막일로 접근해서 풀었음. 일단 2의 제곱인지 확인하는 문제임. 완전 노가다 접근 0 이면 제곱 아니면 1, 2 는 제곱수 그 외에의 2의 제곱인지 판단하기 위해서 테이블을 만듦 INT 자료 범위 맥스까지 2 곱해서 TABLE을 만든다. 모든 2의 제곱을 TABLE에 계산해서 기록한다. 이후 기록한 테이블과 입력으로 들어오는 N의 값이 같으면 TRUE N의 값이 TABLE에 값 보다 크다면 FALSE로 막일로 풀었다. 으아아 아.. class Solution { public: bool isPowerOfTwo(int n) { if(n =..
-
326. Power of Three릿코드(LEETCODE) 2020. 2. 8. 17:35
https://leetcode.com/problems/power-of-three 불러오는 중입니다... 3의 제곱수인지 확인 하는 문제 3으로 계속 나누어 나머지가 있으면 실패 그외의 경우 에러 처리함 class Solution { public: bool isPowerOfThree(int n) { // 이처리가 마음에 안듦 if(n == 1) { return true; } // 이처리가 마음에 안듦 if(n == 0) { return false; } while(true) { int cand = n % 3; // 나머지 있으면 3의 배수가 아니니 ㅂㅂ2 if(cand != 0) return false; n = n / 3; if(n == 1) { break; } } return true; } }; 허어 So..
-
1051. Height Checker릿코드(LEETCODE) 2020. 2. 8. 17:16
https://leetcode.com/problems/height-checker 불러오는 중입니다... 정렬을 활용한 접근 정렬을 하고 정렬된 값과 비교하여 틀린 횟수를 출력 sort는 O(nlogn)의 시간 복잡도, 반복문은 O(n) 의 시간 복잡도? O(nLogn) + O(n)? class Solution { public: int heightChecker(vector& heights) { int sol = 0; vector arr = heights; sort(arr.begin(), arr.end()); for (int i = 0; i < heights.size(); i++) { if (heights[i] != arr[i]) { sol++; } } return sol; } }; 이방법외에는 빠르게 접근..