- 博客(34)
- 收藏
- 关注
原创 两数组的交 II
计算两个数组的交 解题代码:class Solution { public: /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ vector intersection(vector&
2017-06-12 22:47:53
226
原创 两数组的交
问题描述:返回两个数组的交 解题代码:class Solution { public: /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ vector intersection(ve
2017-06-12 22:31:57
206
原创 整数排序 II
给一组整数,按照升序排序。使用归并排序,快速排序,堆排序或者任何其他 O(n log n) 的排序算法。 解题代码: void sortIntegers2(vector& A) { // Write your code here QuickSort(A,0,A.size()-1); } void QuickSort(vector&
2017-06-11 23:20:24
240
原创 整数排序
给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。 解题代码:class Solution { public: /** * @param A an integer array * @return void */ void sortIntegers(vector& A) { //
2017-06-11 22:37:18
189
原创 中位数
题目:给定一个未排序的整数数组,找到其中位数。 中位数是排序后数组的中间值,如果数组的个数是偶数个,则返回排序后数组的第N/2个数。 解题代码:class Solution { public: /** * @param nums: A list of integers. * @return: An integer denotes the middle num
2017-06-11 22:18:39
325
原创 两数之和
要求: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是 1 到 n,不是以 0 开头。 实现代码:class Solution { public: /* * @param numbers : An array of Intege
2017-06-11 21:30:29
235
原创 在二叉查找树中插入节点
样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 2 2 / \ / \ 1 4 --> 1 4 / / \ 3 3 6 解题思路:首先理解二叉查找树的定义,插入的过程中只需三点:1,是否二叉树为空,如果为空插入节点为根节
2017-05-21 23:39:05
257
原创 二叉树的层次遍历 - C++
样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3], [9,20], [15,7] ] class Solution { /** * @param root : The root of binary tree.
2017-04-23 22:34:44
610
原创 等价二叉树
样例 1 1 / \ / \ 2 2 and 2 2 / / 4 4 就是两棵等价的二叉树。 1 1 / \ / \ 2 3 and 2 3 /
2017-04-23 22:33:28
147
原创 翻转二叉树
样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 class Solution { public: /** * @param root: a TreeNode, the root of the binary tree * @re
2017-04-23 22:28:07
200
原创 子树
样例 下面的例子中 T2 是 T1 的子树: 1 3 / \ / T1 = 2 3 T2 = 4 / 4 下面的例子中 T2 不是 T1 的子树: 1 3 / \
2017-04-23 22:26:51
182
原创 把排序数组转换为高度最小的二叉搜索树
给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树 样例 给出数组 [1,2,3,4,5,6,7], 返回 4 / \ 2 6 / \ / \ 1 3 5 7 class Solution { public: /** * @param A: A sorted (increa
2017-04-23 22:06:35
190
原创 克隆二叉树
深度复制一个二叉树。 给定一个二叉树,返回一个他的 克隆品 。 class Solution { public: /** * @param root: The root of binary tree * @return root of new tree */ void look (TreeN
2017-04-23 21:59:44
226
原创 二叉树的最大深度
给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的距离。 样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的最大深度为3. class Solution { public: /** * @param root: The root of binary
2017-04-23 21:41:29
197
原创 二叉树的最大节点
在二叉树中寻找值最大的节点并返回。 样例 给出如下一棵二叉树: 1 / \ -5 2 / \ / \ 0 3 -4 -5 返回值为 3 的节点。 class Solution { public: /** * @param root the root of binary tree
2017-04-23 21:39:09
361
原创 二叉树的后序遍历
给出一棵二叉树,返回其节点值的后序遍历。 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] class Solution { /** * @param root: The root of binary tree. * @return: Postord
2017-04-23 21:28:56
202
原创 二叉树的前序遍历
给出一棵二叉树,返回其节点值的前序遍历。 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. class Solution { public: /** * @param root: The root of binary tree. * @return: Pr
2017-04-23 21:26:44
170
原创 二叉树的中序遍历
给出一棵二叉树,返回其中序遍历 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]. public: vector a; void look(TreeNode *root) { if(root==NULL) return;
2017-04-23 21:22:22
141
转载 链表划分
给定链表 1->4->3->2->5->2->null,并且 x=3 返回 1->2->2->4->3->5->null 借鉴 class Solution { public: /** * @param head: The first node of linked list. * @param x: an integer
2017-03-31 22:20:29
139
转载 合并两个排序链表
给出 1->3->8->11->15->null,2->null, 返回 1->2->3->8->11->15->null。 借鉴别人的 /** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val
2017-03-31 22:19:31
146
转载 链表插入排序
Given 1->3->2->0->null, return 0->1->2->3->null 借鉴别人的, /** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { *
2017-03-31 22:17:21
164
转载 链表求和
/** 给出两个链表 3->1->5->null 和 5->9->2->null,返回 8->0->8->null 最后这四个题实在不会做了,抄的别人的 * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *
2017-03-31 22:14:56
286
原创 删除排序链表中的重复元素
给出 1->1->2->null,返回 1->2->null 给出 1->1->2->3->3->null,返回 1->2->3->null class Solution { public: /** * @param head: The first node of linked list. * @return: head no
2017-03-31 20:42:03
209
原创 删除链表中倒数第n个节点
给出链表1->2->3->4->5->null和 n = 2. 删除倒数第二个节点之后,这个链表将变成1->2->3->5->null. istNode *removeNthFromEnd(ListNode *head, int n) { // write your code here ListNode *a, *b, *pre;
2017-03-31 20:35:30
296
原创 两两交换链表中的节点
给出 1->2->3->4, 你应该返回的链表是 2->1->4->3 ListNode* swapPairs(ListNode* head) { // Write your code here ListNode *x; x=head; if ( head == NULL) { r
2017-03-31 20:07:23
207
原创 在O(1)时间复杂度删除链表节点
给定 1->2->3->4,和节点 3,删除 3 之后,链表应该变为 1->2->4 class Solution { public: /** * @param node: a node in the list should be deleted * @return: nothing */ void deleteNod
2017-03-31 19:58:50
228
原创 翻转一个链表
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null public: /** * @param head: The first node of linked list. * @return: The new head of reversed linked list. */ ListNode *reverse(
2017-03-31 19:56:38
224
原创 链表倒数第n个节点
给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1 class Solution { public: /** * @param head: The first node of linked list. * @param n: An integer. * @return: Nth to last node of a si
2017-03-31 13:38:37
190
原创 删除链表中的元素
ListNode *removeElements(ListNode *head, int val) { while ( head != NULL && head->val == val){ head = head->next; } if ( head == NULL) { re
2017-03-27 21:27:06
235
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅