leetcode题解


[leetcode] Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Show Tags //preorder储存数字信息 //inorder存储结构信息 //每次inorder都会把数平分成两半 //注意不要内存超界。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode […]


[leetcode] Interleaving String

Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = “aabcc”, s2 = “dbbca”, When s3 = “aadbbcbcac”, return true. When s3 = “aadbbbaccc”, return false. //方案一,递归,超时。复杂度o(n+m) class Solution { public: bool isInterleave(string s1, string s2, string […]


[leetcode] Decode Ways

Decode Ways A message containing letters from A-Z is being encoded to numbers using the following mapping: ‘A’ -> 1 ‘B’ -> 2 … ‘Z’ -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded message “12”, it could […]


[leetcode] Subsets II

Subsets II Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], [] […]


[leetcode] Gray Code

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 […]