본문 바로가기

Algorithm/자료구조

(110)
(C++) - LeetCode (easy) 206. Reverse Linked List https://leetcode.com/problems/reverse-linked-list/description/ Reverse Linked List - LeetCode Reverse Linked List - Given the head of a singly linked list, reverse the list, and return the reversed list. Example 1: [https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg] Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] Example 2: [https://asset leetcode.com 자료구조를 이용한 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 h..
(C++) - LeetCode (easy) 203. Remove Linked List Elements https://leetcode.com/problems/remove-linked-list-elements/description/ Remove Linked List Elements - 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 linked list 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 인자 head를 받아 deletedNode를 선언 후 저장해줍니다. 📔 풀이과정 1. 지울 node가 처음에 있는 경우를 대비해 시작지점을 옮겨줘야합니다. 값이 val이 ..
(C++) - LeetCode (easy) 202. Happy Number https://leetcode.com/problems/happy-number/description/ 간단 구현 문제였습니다. 📕 풀이방법 📔 풀이과정 순환됨을 알 수 있습니다. n = 4인 경우를 예시로 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4가 되므로 1을 만들 수 없습니다. 매 단계 별로 이미 있는지 여부를 알기 위해 map에 저장해줍니다. 이런 순환구조를 가진 경우 false를 반환해줍니다. 📕 Code 📔 C++ class Solution { public: bool isHappy(int n) { string num = to_string(n); map dict; while(1) { int sum = 0; for(auto n : num) { sum +=..
(C++) - LeetCode (easy) 191. Number of 1 Bits https://leetcode.com/problems/number-of-1-bits/description/ 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 bitmasking을 사용해보는 문제였습니다. 📕 풀이방법 📔 풀이과정 n의 0~31번째 bit를 검사해서 해당 bit가 1이라면 세줍니다. n & (1
(C++) - LeetCode (easy) 190. Reverse Bits https://leetcode.com/problems/reverse-bits/description/ 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 bitmasking을 이용한 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 uint32_t ans, n의 32자리 bit를 확인할 piv를 선언 후 각각 0, 1로 초기화해줍니다. 📔 풀이과정 가장 오른쪽 bit부터검사하면서 &연산시 bit가 1이라면 ans의 가장 왼쪽부터 해당 값을 채워줍..
(C++) - LeetCode (easy) 169. Majority Element https://leetcode.com/problems/majority-element/description/ Majority Element - 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 자료구조를 이용한 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 1. 과반수의 기준 majorLimit을 선언해 저장해줍니다. 짝수라면 2로 나눈 값을, 홀수라면 올림한 정수를 저장해줘야 합니다. 이는 ceil함수로 해결할 수 있습니다. casting에 주의합니다. 2. map..
(C++) - LeetCode (easy) 160. Intersection of Two Linked Lists https://leetcode.com/problems/intersection-of-two-linked-lists/description/ Intersection of Two Linked Lists - 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 linked list를 이용한 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 1. headA, headB의 길이를 aLength, bLength선언 후 각각 저장해줍니다. 2. 두 list의 최소길이를 minLength에..
(C++) - LeetCode (easy) 145. Binary Tree Postorder Traversal https://leetcode.com/problems/binary-tree-postorder-traversal/description/ Binary Tree Postorder Traversal - 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 재귀를 활용한 node 순회 문제였습니다. 📕 풀이방법 📔 풀이과정 postorder는 왼쪽 자식 -> 오른쪽 자식 -> 자기 자신 순으로 이진 tree를 순회하는 방식입니다. *재귀이므로 vector의 원소 삽입 순서가 반..