二分搜索,和LeetCode 153. Find Minimum in Rotated Sorted Array相似。
只是在num[begin] == num[mid]时,需要binary_search(++ begin, end, num); 这时仅将begin移进一位,并没有进行二分查找。
所以如测试用例为 num = {1, 1, 1, 1, 1, ..., 1}等特殊情况时,最坏情况...
分类:
其他好文 时间:
2015-02-24 00:50:43
阅读次数:
206
二分查找。
因为在旋转前的数组是排好序了的,
所以当num[begin] > num[mid]时,表示我们要搜寻的最小数字在num[begin, ..., mid]之间;
反之,num[begin]
例:考虑num = {5, 6, 7, 1, 2, 3, 4},
begin = 0, end = 6, mid = 3
num[begin] = 5 > num[m...
分类:
其他好文 时间:
2015-02-23 23:43:09
阅读次数:
350
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is ...
分类:
其他好文 时间:
2015-02-23 15:26:34
阅读次数:
168
odt文件其实由若干文本文件组成,git 经过一些配置是可以支持diff的。
step 1:
在git项目中i添加.gitattributes文件, 内容如下:
*.odt diff=odt
step 2:
在.git/config文件中添加下面一段:
[diff "odt"]
binary = true
textconv = /usr/local/bin/odt-to-txt...
分类:
其他好文 时间:
2015-02-23 14:18:21
阅读次数:
144
题意:一个长度为N的01序列,会有N个不同的轮换(当然,字符相同,其中也可能会有相同的),将这N个不同轮换按字典序排
序,取排序后的每个轮换的最后一排,组成一个序列。题中给出压缩后的序列,求原始序列,输出的是字典序最小的那个序列。
思路:这题基于一个性质:在已经排序好的矩阵中,对于首位相同的两行,经过左移一位的操作后,形成的新的两行的先后次序不发
生改变。即:设i行在j行前面,i行左移一位变...
分类:
编程语言 时间:
2015-02-22 11:08:30
阅读次数:
193
Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note...
分类:
其他好文 时间:
2015-02-22 06:46:02
阅读次数:
162
Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and al...
分类:
其他好文 时间:
2015-02-21 15:26:40
阅读次数:
185
写这道题目的时候遇到了一个令人诧异的问题,就是平台上跑来的结果和我本机跑起来的结果不一样。后来Debug了之后才发现是我数组开小了,只开到100 的数组竟然都去访问他170位的地址肯定要跪成翔啊...好吧,解释一下题意。有N盏台灯,C次操作每次操作可以按一次按钮,一共一个四个按钮可以得出的规律是每6...
分类:
其他好文 时间:
2015-02-20 23:06:37
阅读次数:
189
很简单。没什么好说的。主要学习了STL的find_if、binary_search、和sort函数。find_if函数原型为:template InputIterator find_if (InputIterator first, InputIterator last, UnaryPredic...
分类:
其他好文 时间:
2015-02-20 16:20:31
阅读次数:
199
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.这道题要求用先序和中序遍历来建...
分类:
其他好文 时间:
2015-02-20 14:08:33
阅读次数:
169