Yearly Archives: 2015


[leetcode] Copy List with Random Pointer

Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. tag: hashtable, linked-list 虽然被标记为hard,但代码不长。 问题关键是,在拷贝完next指针后,要根据原来链表中的random的地址,找到新建的链表中对应的node的地址,这里我们用一个hashtable来实现,在拷贝next指针时,将对应关系记录下来。 空间复杂度o(n),时间复杂度o(n)。 /** * Definition for singly-linked list with […]


[leetcode] Divide Two Integers

Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. tags: math, binary search 8/10/2015 update Reference http://yucoding.blogspot.com/2013/01/leetcode-question-28-divide-two-integers.html class Solution { public: int divide(int dividend, int divisor) { int flag = 1; long ldividend = dividend; long ldivisor = divisor; if(ldividend […]


[leetcode] Remove Element

Remove Element Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn’t matter what you leave beyond the new length. 简单题,与上一题Remove Element in Sorted Array类似 class Solution { public: int removeElement(int A[], […]


[leetcode] Generate Parentheses

Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: “((()))”, “(()())”, “(())()”, “()(())”, “()()()” tag: backtracking 7/10/2015 udpate DFS approach, add some restrictions to guarantee the validation of parentheses class Solution { […]


[leetcode] Valid Parentheses

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not. tag: stack, string 5/10/2015 update a neater approach, with […]