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] Remove Duplicates from Sorted List II

Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3. 困倦时不要解题,这题做了3遍才AC。 一个bonus,删掉多余的node,以免内存溢出。 我用了superHead的技巧,不用再单独判断链表的起始情况。 查看当前节点和下一个节点,如果相同,将isDuplicated置1.当遇到当前节点和下一节点不同时,看isDuplicated的值,如果是1,则忽略掉它,把isDuplicated置0.如果是0,证明当前节点没有重复,连入结果链表中。 /** * Definition for singly-linked list. * struct ListNode { […]