摘要: 96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, th 阅读全文
posted @ 2017-04-09 22:04 蓦然闻声 阅读(160) 评论(0) 推荐(0)
摘要: 530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find the minimum absolute difference between values of an 阅读全文
posted @ 2017-04-09 22:03 蓦然闻声 阅读(237) 评论(0) 推荐(0)
摘要: 520. Detect Capital Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to 阅读全文
posted @ 2017-04-09 22:02 蓦然闻声 阅读(93) 评论(0) 推荐(0)
摘要: leetcode 105题,由树的前序序列和中序序列构建树结构。详细解答参考《剑指offer》page56. 先序遍历结果的第一个节点为根节点,在中序遍历结果中找到根节点的位置。然后就可以将问题拆分,递归求解。 阅读全文
posted @ 2017-04-09 22:01 蓦然闻声 阅读(202) 评论(0) 推荐(0)
摘要: 递归方法: 非递归:要借助栈,可以利用C++ STL中的stack。首先将根节点压栈,然后压入栈顶元素的左节点直到叶子节点,然后访问叶子节点后压入该节点的右子节点。 阅读全文
posted @ 2017-04-09 22:01 蓦然闻声 阅读(143) 评论(0) 推荐(0)
摘要: 437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The pa 阅读全文
posted @ 2017-04-09 22:00 蓦然闻声 阅读(146) 评论(0) 推荐(0)
摘要: 500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the 阅读全文
posted @ 2017-04-09 21:59 蓦然闻声 阅读(365) 评论(0) 推荐(0)
摘要: 504. Base 7 Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202" Example 2: Input: -7 Output: "-10" Note: Th 阅读全文
posted @ 2017-04-09 21:58 蓦然闻声 阅读(152) 评论(0) 推荐(0)
摘要: 28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思 阅读全文
posted @ 2017-04-09 21:56 蓦然闻声 阅读(127) 评论(0) 推荐(0)
摘要: 70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many dist 阅读全文
posted @ 2017-04-09 21:55 蓦然闻声 阅读(145) 评论(0) 推荐(0)
摘要: 459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of t 阅读全文
posted @ 2017-04-09 21:55 蓦然闻声 阅读(99) 评论(0) 推荐(0)
摘要: 53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the a 阅读全文
posted @ 2017-04-09 21:54 蓦然闻声 阅读(141) 评论(0) 推荐(0)
摘要: 326. Power of Three Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / rec 阅读全文
posted @ 2017-04-09 21:53 蓦然闻声 阅读(147) 评论(0) 推荐(0)
摘要: Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive intege 阅读全文
posted @ 2017-04-09 21:53 蓦然闻声 阅读(100) 评论(0) 推荐(0)
摘要: 405. Convert a Number to Hexadecimal Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method 阅读全文
posted @ 2017-04-09 21:52 蓦然闻声 阅读(194) 评论(0) 推荐(0)
摘要: 415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: 思路:控制num1的长度不小于num2的长度,在n 阅读全文
posted @ 2017-04-09 21:51 蓦然闻声 阅读(116) 评论(0) 推荐(0)
摘要: 108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树。直接递归构建,取中间的元素为根节点,然后分别构建左子树和右子树。 阅读全文
posted @ 2017-04-09 21:51 蓦然闻声 阅读(99) 评论(0) 推荐(0)
摘要: 501. Find Mode in Binary Search Tree Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in 阅读全文
posted @ 2017-04-09 21:50 蓦然闻声 阅读(238) 评论(0) 推荐(0)
摘要: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, Given nums = [0, 1, 阅读全文
posted @ 2017-04-09 21:49 蓦然闻声 阅读(120) 评论(0) 推荐(0)
摘要: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between iand j equals 阅读全文
posted @ 2017-04-09 21:49 蓦然闻声 阅读(102) 评论(0) 推荐(0)
摘要: 思路一:递归 思路二:迭代 从头至尾,依次反转相邻节点指针 阅读全文
posted @ 2017-04-09 21:48 蓦然闻声 阅读(154) 评论(0) 推荐(0)
摘要: 121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitte 阅读全文
posted @ 2017-04-09 21:47 蓦然闻声 阅读(82) 评论(0) 推荐(0)
摘要: 122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to 阅读全文
posted @ 2017-04-09 21:47 蓦然闻声 阅读(83) 评论(0) 推荐(0)
摘要: 167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascending order, find two numbers such that they add up t 阅读全文
posted @ 2017-04-09 21:46 蓦然闻声 阅读(136) 评论(0) 推荐(0)
摘要: 思路:罗马计数转阿拉伯数字。罗马数字的构造:XCVIII表示98,DCXXI表示621。从右至左,用max记录当前最大数的符号,若当前索引处的数字比max大,则将总数加上当前索引字符所代表的的数,如果小,则将总数减去当前索引所代表的数字。 阿拉伯数字转换为罗马计数 思路:罗马数字的基本型为:I = 阅读全文
posted @ 2017-04-09 21:45 蓦然闻声 阅读(332) 评论(0) 推荐(0)
摘要: A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). Each LED represents 阅读全文
posted @ 2017-04-09 21:44 蓦然闻声 阅读(120) 评论(0) 推荐(0)
摘要: 217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears a 阅读全文
posted @ 2017-04-09 21:42 蓦然闻声 阅读(153) 评论(0) 推荐(0)
摘要: 350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 阅读全文
posted @ 2017-04-09 21:42 蓦然闻声 阅读(116) 评论(0) 推荐(0)
摘要: 409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built 阅读全文
posted @ 2017-04-09 21:41 蓦然闻声 阅读(127) 评论(0) 推荐(0)
摘要: 169. Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. Y 阅读全文
posted @ 2017-04-09 21:40 蓦然闻声 阅读(102) 评论(0) 推荐(0)
摘要: 492: 给定一个面积值,求它的长l和宽w。长和宽需满足:长大于等于宽,长和宽的差值尽可能小,长乘宽等于面积。 思路:先将l和w初始化为sqrt(area),然后看l*w是否等于面积,如果等于则返回l和w,若小则将l加一,若大则将w减一。 阅读全文
posted @ 2017-04-09 21:39 蓦然闻声 阅读(130) 评论(0) 推荐(0)
摘要: 22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = 阅读全文
posted @ 2017-04-09 21:39 蓦然闻声 阅读(203) 评论(0) 推荐(0)
摘要: Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 阅读全文
posted @ 2017-04-09 21:38 蓦然闻声 阅读(125) 评论(0) 推荐(0)
摘要: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical a 阅读全文
posted @ 2017-04-09 21:36 蓦然闻声 阅读(168) 评论(0) 推荐(0)
摘要: Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. 阅读全文
posted @ 2017-04-09 21:35 蓦然闻声 阅读(114) 评论(0) 推荐(0)
摘要: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 - 阅读全文
posted @ 2017-04-09 21:34 蓦然闻声 阅读(122) 评论(0) 推荐(0)
摘要: Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element 阅读全文
posted @ 2017-04-09 21:33 蓦然闻声 阅读(123) 评论(0) 推荐(0)
摘要: Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two left leaves in the binary tree, with values 9 and 1 阅读全文
posted @ 2017-04-09 21:33 蓦然闻声 阅读(62) 评论(0) 推荐(0)
摘要: Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the rans 阅读全文
posted @ 2017-04-09 21:32 蓦然闻声 阅读(116) 评论(0) 推荐(0)
摘要: Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n  阅读全文
posted @ 2017-04-09 21:31 蓦然闻声 阅读(105) 评论(0) 推荐(0)
摘要: Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a g 阅读全文
posted @ 2017-04-09 21:30 蓦然闻声 阅读(130) 评论(0) 推荐(0)
摘要: 思路:递归。先将左子树反转,再将右子树反转,然后让root->left指向反转后的右子树,root->right指向反转后的左子树。 阅读全文
posted @ 2017-04-09 21:29 蓦然闻声 阅读(85) 评论(0) 推荐(0)
摘要: Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, giv 阅读全文
posted @ 2017-04-09 21:29 蓦然闻声 阅读(78) 评论(0) 推荐(0)
摘要: Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at 阅读全文
posted @ 2017-04-09 21:26 蓦然闻声 阅读(104) 评论(0) 推荐(0)
摘要: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two di 阅读全文
posted @ 2017-04-09 21:25 蓦然闻声 阅读(142) 评论(0) 推荐(0)
摘要: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime comple 阅读全文
posted @ 2017-04-09 21:24 蓦然闻声 阅读(95) 评论(0) 推荐(0)
摘要: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest l 阅读全文
posted @ 2017-04-09 21:24 蓦然闻声 阅读(168) 评论(0) 推荐(0)
摘要: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] 阅读全文
posted @ 2017-04-09 21:20 蓦然闻声 阅读(147) 评论(0) 推荐(0)
摘要: The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Input: x = 1, y = 4 Output: 2 Expl 阅读全文
posted @ 2017-04-09 21:19 蓦然闻声 阅读(90) 评论(0) 推荐(0)
摘要: 思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2。 阅读全文
posted @ 2017-04-09 21:18 蓦然闻声 阅读(112) 评论(0) 推荐(0)
摘要: 1. 利用位运算求两数之和 2. 不使用新的变量,交换两个变量的值 阅读全文
posted @ 2017-04-09 21:16 蓦然闻声 阅读(174) 评论(0) 推荐(0)
摘要: 数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止。而这个一位数便是原来数字的数字根。例如: 198的数字根为9(1+9+8=18,1+8=9)。 性质说明 小学学加法的时候我们都明白,一个数字加9,就是把十位加1,个位减1。因此十位 阅读全文
posted @ 2017-04-09 21:15 蓦然闻声 阅读(2382) 评论(2) 推荐(1)