- 博客(24)
- 收藏
- 关注
原创 把排序数组转换为高度最小的二叉搜索树
class Solution {public: TreeNode *sortedArrayToBST(vector &A) { TreeNode *result = buildBST(A,0,A.size()-1); return result; } TreeNode *buildBST(vector &A, int start, int
2016-03-20 16:48:33
286
原创 二叉树的前后中序遍历
前序遍历class Solution {public: vector result; void getNode(TreeNode *root){ result.push_back(root->val); if(root->left != NULL){ getNode(root->left); }
2016-03-20 16:26:27
313
原创 二叉树的最大深度 & 二叉树的最小深度
分治法 二叉树的最大深度class Solution {public: /** * @param root: The root of binary tree. * @return: An integer */ int maxDepth(TreeNode *root) { // write your code here
2016-03-20 15:53:50
500
原创 Paint Fence 栅栏涂色
不能有超过连续两根柱子是一个颜色,也就意味着第三根柱子要么根第一个柱子不是一个颜色,要么跟第二根柱子不是一个颜色。如果不是同一个颜色,计算可能性的时候就要去掉之前的颜色,也就是k-1种可能性。假设dp[1]是第一根柱子及之前涂色的可能性数量,dp[2]是第二根柱子及之前涂色的可能性数量,则dp[3]=(k-1)*dp[1] + (k-1)*dp[2]。class Solution {
2016-03-19 21:44:51
699
原创 A+B
class Solution {public: /* * @param a: The first integer * @param b: The second integer * @return: The sum of a and b */ int aplusb(int a, int b) { // write your c
2016-03-10 17:44:26
266
原创 不同的路径
class Solution {public: /** * @param n, m: positive integer (1 <= n ,m <= 100) * @return an integer */ int uniquePaths(int m, int n) { // wirte your code here v
2016-03-09 12:36:11
283
原创 判断字符串是否没有重复字符
class Solution {public: /** * @param str: a string * @return: a boolean */ bool isUnique(string &str) { // write your code here int ch[128] = {0}; for (
2016-03-09 12:23:33
363
原创 恢复旋转排序数组
难!class Solution {public: int getGCD(int a, int b) { if (a % b == 0) { return b; } return getGCD(b, a % b); } void recoverRotatedSortedArray(
2016-03-08 17:22:59
282
原创 删除排序数组中的重复数字
class Solution {public: /** * @param A: a list of integers * @return : return an integer */ int removeDuplicates(vector &nums) { // write your code here if(nums
2016-03-08 10:09:46
360
原创 合并区间
/** * Definition of Interval: * classs Interval { * int start, end; * Interval(int start, int end) { * this->start = start; * this->end = end; * } */class Solution
2016-03-06 15:35:40
280
原创 最大子数组
class Solution {public: /** * @param nums: A list of integers * @return: A integer indicate the sum of max subarray */ int maxSubArray(vector nums) { // write your
2016-01-24 01:16:16
325
原创 x的平方根——(不知为何这道有点难)
class Solution {public: /** * @param x: An integer * @return: The sqrt of x */ int sqrt(int x) { // write your code here if(x ==1 && x == 0){return x;}
2016-01-23 23:54:23
567
原创 两个链表的交叉——值得注意的错误
class Solution {public: /** * @param headA: the first list * @param headB: the second list * @return: a ListNode */ ListNode *getIntersectionNode(ListNode *headA, ListNode
2016-01-21 14:43:06
231
原创 带环链表
class Solution {public: /** * @param head: The first node of linked list. * @return: True if it has a cycle, or false */ bool hasCycle(ListNode *head) { // write your c
2016-01-21 01:43:42
303
原创 重排链表
给定一个单链表L: L0→L1→…→Ln-1→Ln,重新排列后为:L0→Ln→L1→Ln-1→L2→Ln-2→…必须在不改变节点值的情况下进行原地操作。class Solution {public: /** * @param head: The first node of linked list. * @return: void */
2016-01-20 23:21:27
429
原创 合并k个排序链表
class Solution {public: /** * @param lists: a list of ListNode * @return: The head of one sorted list. */ ListNode *mergeKLists(vector &lists) { // write your code here
2016-01-20 17:47:46
262
原创 翻转链表1和2
class Solution {public: /** * @param head: The first node of linked list. * @return: The new head of reversed linked list. */ ListNode *reverse(ListNode *head) { // wri
2016-01-19 23:45:53
212
原创 链表排序
class Solution {public: ListNode *sortList(ListNode *head) { if(head == NULL || head->next ==NULL){return head;} ListNode *mid = getMid(head); ListNode *right = mid->nex
2016-01-19 21:18:12
237
原创 链表插入排序
class Solution {public: /** ---->[1]----> [3]----> [2]----->[0]---->[NULL](原链表) head 1->next 3->next 2->next 0->next ----->[1]---->[NULL](从原链表中取第1个节点作为只有一个节点的有序链表) dum
2016-01-17 14:13:44
258
原创 删除链表中倒数第n个节点
/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * }
2016-01-16 18:24:27
244
原创 在O(1)时间复杂度删除链表节点
/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * }
2016-01-16 17:21:06
300
原创 链表求和
给出两个链表 3->1->5->null 和 5->9->2->null,返回8->0->8->null/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL)
2016-01-14 02:12:18
244
原创 删除排序链表中的重复数字 II
/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * }
2016-01-14 00:11:13
451
原创 删除链表中的元素
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: /** * @
2016-01-13 19:16:54
234
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人