본문 바로가기

풀어본 Algorithm 문제 정리

[OJ.leetcode] binary-tree-zigzag-level-order-traversal https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ iven a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its zigzag level order traversal as: [ [3], [20,9], [15,7] ] /* BFS를 응용해서, 자료.. 더보기
[OJ.leetcode] graycode https://oj.leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:00 - 0 01 - 1 11 - 3 10 - 2 /* testc.. 더보기
[OJ.leetcode] maximum-subarray https://oj.leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. class Solution { public: int maxSubArray(int A[], int n) { if(n == 1) return A[0]; int maxSum = A[0]; int currSum = 0; for(int i.. 더보기
[Oj.leetcode] swap-nodes-in-pair https://oj.leetcode.com/problems/swap-nodes-in-pairs/ Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. /* 단순히 포인터 치환문제. 포인터가 가리키는 노드 자체를 치환하기 위해서는 더블 포인터를 사용하던지, 포인터->next를 변경하여 .. 더보기
[OJ.leetcode] jump-game-ii https://oj.leetcode.com/problems/jump-game-ii/ Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. For example: Given array A = [2,3,1,1,4] The minimum number of jumps to reach the last index is 2. .. 더보기
[OJ.leetcode] minimum-depth-of-binary-tree https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solut.. 더보기
[OJ.leetcode] minimum-path-sum https://oj.leetcode.com/problems/minimum-path-sum/ Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any point in time. /* 초기 접근: 전수조사를 해야하는가->그렇다. 어떻게 가지치기 할 것인가-> dp 적용 base case 설정: if left == m-1, down == n-1 dp 적용: dp를 적용하기 위해서는 처음에 짤때부터 dp를 어떻게 .. 더보기
[OJ.leetcode] reverse-nodes-in-k-group https://oj.leetcode.com/problems/reverse-nodes-in-k-group/ Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nodes itself may be changed. Only constant memory is allowed. For example, Given this linked.. 더보기
[OJ.leetcode] same-tree https://oj.leetcode.com/problems/same-tree/ Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ /* Goa.. 더보기
[OJ.leetcode] remove-nth-node-from-end-of-list https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Given n will always be valid. Try to do this in one pass. /** * Definition for singly-linked list. * struct Li.. 더보기