lintcode笔记
猫子猪
这个作者很懒,什么都没留下…
展开
-
【lintcode笔记】在二叉查找树中插入节点
给定一棵二叉查找树和一个新的树节点,将节点插入到树中。你需要保证该树仍然是一棵二叉查找树。注意事项You can assume there is no duplicate values in this tree + node.#include <stdio.h> using namespace std; /** * Definition of TreeNode: * class TreeNode原创 2017-02-23 20:38:12 · 330 阅读 · 0 评论 -
【lintcode笔记】搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。你可以假设在数组中无重复元素。样例 [1,3,5,6],5 → 2[1,3,5,6],2 → 1[1,3,5,6], 7 → 4[1,3,5,6],0 → 0//复杂度O(n) #include <stdio.h> #include <vector> using namespace std;原创 2017-02-21 15:11:37 · 308 阅读 · 0 评论 -
【lintcode笔记】经典二分查找问题
在一个排序数组中找一个数,返回该数出现的任意位置,如果不存在,返回-1样例 给出数组 [1, 2, 2, 4, 5, 5].对于 target = 2, 返回 1 或者 2. 对于 target = 5, 返回 4 或者 5. 对于 target = 6, 返回 -1.//最基础的二分查找算法 #include <stdio.h> #include <vector> #include原创 2017-02-21 14:31:26 · 715 阅读 · 0 评论 -
【lintcode笔记】两数组的交 II
计算两个数组的交注意事项 每个元素出现次数得和在数组里一样 答案可以以任意顺序给出 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].//不必去重 #include <stdio.h> #include <vector> #include <algorithm> using namespace std; class Solution { p原创 2017-02-20 23:32:55 · 293 阅读 · 0 评论 -
【lintcode笔记】合并排序数组
合并两个排序的整数数组A和B变成一个新的数组。样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6]//简单的合并算法,时间复杂度O(max(m,n)) 空间复杂度O(m+n) #include <stdio.h> #include <vector> using namespace std; class Solution { public: /*原创 2017-02-20 23:22:28 · 271 阅读 · 0 评论 -
【lintcode笔记】两数组的交
返回两个数组的交注意事项 Each element in the result must be unique. The result can be in any order. 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].熟悉hash解法尝试 负值失败 尝试OFFSET?#include <stdio.h> #include <vector原创 2017-02-20 23:15:44 · 267 阅读 · 0 评论