摘要: 题目描述 实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1。 给定指向树根结点的指针TreeNode* root,请返回一个bool,代表这棵树是否平衡。 解法:1.递归求取节点的高度,注意递归终止条件,其余就是取左右子树最大的高度。 2.递归求取 阅读全文
posted @ 2016-05-24 09:43 梦幻之海 阅读(215) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your a 阅读全文
posted @ 2016-05-11 15:31 梦幻之海 阅读(195) 评论(0) 推荐(0) 编辑
摘要: Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s 阅读全文
posted @ 2016-05-05 21:28 梦幻之海 阅读(192) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space fo 阅读全文
posted @ 2016-05-04 19:12 梦幻之海 阅读(428) 评论(0) 推荐(0) 编辑
摘要: 题目描述 请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中。 给定一个int[] numbers(C++中为vector&ltint>),其中第一个元素为栈顶,请返回排序后的栈。请注意这是一个栈,意味着排序过程中你只 阅读全文
posted @ 2016-04-12 10:09 梦幻之海 阅读(329) 评论(0) 推荐(0) 编辑
摘要: 题目描述 请实现一种数据结构SetOfStacks,由多个栈组成,其中每个栈的大小为size,当前一个栈填满时,新建一个栈。该数据结构应支持与普通栈相同的push和pop操作。 给定一个操作序列int[][2] ope(C++为vector<vector<int>>),每个操作的第一个数代表操作类型 阅读全文
posted @ 2016-04-11 16:55 梦幻之海 阅读(194) 评论(0) 推荐(0) 编辑
摘要: 题目描述 请编写一个函数,检查链表是否为回文。 给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文。 测试样例: {1,2,3,2,1} 返回:true {1,2,3,2,3} 返回:false /* struct ListNode { int val; struct 阅读全文
posted @ 2016-04-11 15:08 梦幻之海 阅读(125) 评论(0) 推荐(0) 编辑
摘要: 有两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结果。 给定两个链表ListNode* A,ListNode* B,请返回A+B的结果(ListNode*)。 测试样例: 阅读全文
posted @ 2016-04-11 14:40 梦幻之海 阅读(251) 评论(0) 推荐(0) 编辑
摘要: 这道题被折磨死了,整了好久一直编译通不过出问题,原因出在最后返回值那里。 1.最后一个链表末尾指针需要置NULL,不然总是出现程序打印过多数据。 2.后面的几种情况需要进行判断,应该可以优化写法。 阅读全文
posted @ 2016-04-08 09:21 梦幻之海 阅读(158) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the or 阅读全文
posted @ 2016-03-25 14:19 梦幻之海 阅读(143) 评论(0) 推荐(0) 编辑