Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
walkingtospace / gist:f6a69eae012e541249a9
Created July 14, 2014 17:23
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.
Note:
https://oj.leetcode.com/problems/same-tree/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
@walkingtospace
walkingtospace / gist:8ddfba7e991ccfa38b03
Created July 17, 2014 03:19
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.
https://oj.leetcode.com/problems/minimum-path-sum/
/*
초기 접근:
전수조사를 해야하는가->그렇다.
어떻게 가지치기 할 것인가-> dp 적용
base case 설정:
if left == m-1, down == n-1
@walkingtospace
walkingtospace / gist:c4a91e82d77a11b2cf92
Created July 21, 2014 15:06
minimum-depth-of-binary-tree
https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
https://oj.leetcode.com/problems/jump-game-ii/
/*
O(n^2) solution: 각 ele에 도달하는 mininum jump 수를 계산
class Solution {
public:
int jump(int A[], int n) {
int* B = new int[n];
int minJump = INT_MIN;
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.
/*
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:
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
@walkingtospace
walkingtospace / gist:98937fcbb0f4059cdf4a
Created August 1, 2014 06:16
binary-tree-zigzag-level-order-traversal
https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
/*
BFS를 응용해서, 자료구조를 queue 대신에 deque 또는 vector를 이용, 한번은 front에서부터, 한번은 back 에서부터 pop 하면 될 듯
Time complexity : O(n)
순회순서는
root(->)
1 level depth(<-)
2 level depth(->)