原创 leetcode 219: Contains Duplicate II

Use the unordered map to save the number and its index. And the rest is easy. class Solution { public: bool contai

原创 leetcode 220: Contains Duplicate III

Use set to find numbers with time complexity of O(n*logk). Learned this from http://www.cnblogs.com/easonliu/p/4544073.

原创 leetcode 228: Summary Ranges

Use a window to keep track of all continuous numbers. class Solution { public: vector<string> summaryRanges(vector<

原创 leetcode 232: Implement Queue using Stacks

Use two stacks. Every time I need to pop, I push all numbers in st1 into st2 except the last one, then I pop that one.

原创 leetcode 223: Rectangle Area

There are just two conditions that two rectangles can intersect or not. Draw out all possibilities and you can find out

原创 Leetcode 222: Count Complete Tree Nodes

Do only the recursion will cause TLE. Thus, at each node, count the heights of its left subtree and right subtree, if l

原创 leetcode 234: Palindrome Linked List

To solve it with only O(1) space, you can only find the mid point of the linked list and do some reverse operation, so

原创 leetcode 227: Basic Calculator II

Use two deques to calculate from both sides. class Solution { public: int calculate(string s) { deque<int>

原创 leetcode 231: Power of Two

Use shift to determine. Be careful when n is 1 or a negative number. class Solution { public: bool isPowerOfTwo(int

原创 leetcode 226: Invert Binary Tree

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *

原创 leetcode 224: Basic Calculator

My method is a little verbose. The hardest part is how to handle if the '-' is minus or a negative sign. class Solution

原创 leetcode 230: Kth Smallest Element in a BST

Use the inorder traversal and get all numbers in the BST in order. Then return the kth number. /** * Definition for a

原创 leetcode 215: Kth Largest Element in an Array

Use quick sort. This is the 8ms version. With the sorting of the whole array, the time is long. class Solution { publi

原创 leetcode 221: Maximal Square

Use DP, and dp[i][j] means at matrix[i][j] the maximal square's size. First, initialize the first row and column which

原创 leetcode 213: House Robber II

Do the DP two times, one time starts from house 0 to house n-2 and one time starts from house 1 to house n-1. Return th