알고리즘62 202. Happy Number class Solution { public: int next(int n) { int sum = 0; while (n) { sum += (n%10) * (n%10); n /= 10; } return sum; } bool isHappy(int n) { int fast = next(n), slow = n; while (fast != slow) { slow = next(slow); fast = next(next(fast)); } return fast == 1; } }; Space : O(1) 2022. 7. 27. 189. Rotate Array class Solution { public: void rotate(vector& nums, int k) { int n = nums.size(); k %= n; reverse(nums.begin(), nums.end()); reverse(nums.begin(), nums.begin() + k); reverse(nums.begin() + k, nums.end()); } }; 2022. 7. 24. 19. Remove Nth Node From End of List /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { auto cur = head; while (n - 1) { cur = cur->next; --n; } auto prev = &head; whil.. 2022. 7. 24. 190. Reverse Bits class Solution { public: uint32_t reverseBits(uint32_t n) { n = (n >> 16) | (n > 8) | ((n & 0x00ff00ff) > 4) | ((n & 0x0f0f0f0f) > 2) | ((n & 0x33333333) > 1) | ((n & 0x55555555) 2022. 7. 24. 이전 1 ··· 6 7 8 9 10 11 12 ··· 16 다음