原创 233. Number of Digit One

problem: Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or eq

原创 104. Maximum Depth of Binary Tree

problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest pat

原创 219. Contains Duplicate II

Problems: Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the

原创 451. Sort Characters By Frequency

Problem: Given a string, sort it in decreasing order based on the frequency of characters. solution: #include<iostream

原创 50. Pow(x, n)

problem: Implement pow(x, n). solution: double myPow(double x, int n) {if (n == 1) return x;if (n == 0) return 1;if (n

原创 1004. 拓撲序[Special judge]

Problem: 在圖論中,拓撲序(Topological Sorting)是一個有向無環圖(DAG, Directed Acyclic Graph)的所有頂點的線性序列. 且該序列必須滿足下面兩個條件:   1.       每個頂

原创 1002. 合併二叉樹

Problem: 輸入兩個二叉樹T1和T2,要求對T1和T2進行合併. 合併是指將二叉樹同一位置節點上的數求和,作爲合併後二叉樹相應位置節點的數值. 如果某個位置上只有一個二叉樹有節點,則合併後的二叉樹對應位置上的節點上的數值就等於這個

原创 1007. 怪獸訓練

Problem: 貝爺的人生樂趣之一就是約戰馬會長. 他知道馬會長喜歡和怪獸對決,於是他訓練了N只怪獸,並對怪獸用0到N-1的整數進行編號. 貝爺訓練怪獸的方式是讓它們一對一互毆. 兩隻怪獸互毆會發生以下三種可能的結果: 1) 什麼事

原创 1001. 最小差

Problem:  對於一個整數數列A[0], A[1], ..., A[N-1],要求在其中找兩個數,使得它們的差的絕對值最小.   2 <= N <= 100, -1000 <= A[i] <= 1000.   請實現下面Solut

原创 1000. 分組

Problem:  對於一個整數數列A[0], A[1], ..., A[N-1]進行分組,要求每組1到2個數,並且同組之和不能大於w. 求最少可以分成多少組.   1 <= N <= 100000, 1 <= A[i] <= w <=

原创 1005. 最小和

Problem: 從數列A[0], A[1], A[2], ..., A[N-1]中選若干個數,要求對於每個i(0<=i<N-1),A[i]和A[i+1]至少選一個數,求能選出的最小和.   1 <= N <= 100000, 1 <=

原创 1006. 最長公共子串

Problem: 給定兩個字符串x = x­1x­2…x­n和y = y­1y­2…ym, 請找出x和y的最長公共子串的長度,也就是求出一個最大的k,使得存在下標i和j有x­ix­i+1…x­i+k-1 = yjy­j+1…y­j+k

原创 77. Combinations

Problem: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n

原创 687. Longest Univalue Path

Problem: Given a binary tree, find the length of the longest path where each node in the path has the same value. Thi

原创 Prove that STINGY SAT is NP-complete

Problem: STINGY SAT is the following problem:given a set of clauses(each a disjunction of literals) and an interger k,