题目意思
1、给定有序数组A和关键字key,判断A中是否存在key,如果存在则返回下标值,不存在则返回-1。
2、给定无序数组A和关键字key,判断A中是否存在key,如果存在则返回1,不存在则返回0。
对于1、2问题,我们都可以简单的写出O(n)的从头到尾为的扫描算法,这里就不在累赘,这里我们讨论的是基于二分查找的算法,使其时间在渐进意义上达到O(logn)。
对于有序的数组,很“容...
分类:
编程语言 时间:
2015-03-15 23:46:44
阅读次数:
446
1.两个有序数组组合成一个新的有序数组<?php$arr1=array(2,5,7,9,12);$arr2=array(3,4,6,8,10,11);function merge_sort($arr1,$arr2){ $len1=count($arr1); $len2=count($ar...
分类:
编程语言 时间:
2015-03-15 10:46:14
阅读次数:
217
??
问题描述:在一个数组(无序)中快速找出两个数字,使得两个数字之和等于一个给定的值。假设数组中肯定存在至少一组满足要求。
《剑指Offer》P214(有序数组) 《编程之美》P176
Que:Given an array of integers, find twonumbers such that they add up to a specific target number...
分类:
编程语言 时间:
2015-03-12 17:13:37
阅读次数:
8101
1 // 合并两个有序数组a和b到c 2 void Merge_Array(int a[], int n, int b[], int m, int c[]) 3 { 4 int i, j, k; 5 i = j = k = 0; 6 while(i < n && j < m...
分类:
编程语言 时间:
2015-03-12 16:52:33
阅读次数:
191
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉。后来搜了一下leetcode的难度分布表(leetcode难度及面试频率)才发现,该问题是难度为5的问题,真是小看了它!网上搜了很多答案,但是鲜见简明正...
分类:
编程语言 时间:
2015-03-11 23:17:02
阅读次数:
322
/* * 二分查找算法也称为折半搜索、二分搜索,是一种在有序数组中查找某一特定元素的搜索算法。 * 请注意这种算法是建立在有序数组基础上的。 * */public class BinarySearch { public static void main(String[] args) { ...
分类:
编程语言 时间:
2015-03-11 17:06:42
阅读次数:
115
Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should ret...
分类:
编程语言 时间:
2015-03-11 12:21:34
阅读次数:
193
Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for a...
分类:
编程语言 时间:
2015-03-11 10:40:39
阅读次数:
123
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value t...
分类:
编程语言 时间:
2015-03-10 13:40:27
阅读次数:
144
欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢
Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
这道题是二分查找树的题目,要把一个有序数组转换...
分类:
其他好文 时间:
2015-03-07 14:15:44
阅读次数:
130