摘要:
#include <cstdlib>#include <iostream>using namespace std;//全局变量 int arr[] = {0,1,2,4}; //测试数组 int size = sizeof(arr) / sizeof(*arr); //数组元素个数 bool exist(int arr[],int k){ int low = 0, high = size - 1; //设置一个标志,代表是否存在这个值 bool flag=false; //当找到最后或者第一个元素时,是等... 阅读全文
posted @ 2013-05-29 17:52
夜雨阑珊
阅读(648)
评论(0)
推荐(0)
摘要:
/*** 题目:在排序数组中,找出给定数字的出现次数,比如 [1, 2, 2, 2, 3] 中2的出现次数是3次。* 解法:使用二分查找的方法分别找出给定数字的开始和结束位置,最坏情况下时间复杂度为O(logn)*/#include<stdio.h>#include<stdlib.h> //全局变量 int arr[] = {0,1,1,2,2,2,2,4,4,4}; //测试数组 int size = sizeof(arr) / sizeof(*arr); //数组元素个数 int getUpper(int arr[], int key){//获取某个元素最后出现位置 阅读全文
posted @ 2013-05-29 15:50
夜雨阑珊
阅读(2517)
评论(0)
推荐(0)
摘要:
1 #include <cstdlib> 2 #include <iostream> 3 4 using namespace std; 5 6 /* 7 Name: 8 Copyright: 9 Author: 10 Date: 29/05/13 14:3111 Description: 已排序的数组,求出每个数组的元素出现的次数。 12 */13 14 15 16 int main(int argc, char *argv[])17 {18 //int a[]={1,2,2,2,3,3,4,4,4};19 int a[]={1,2,2,2,3,3... 阅读全文
posted @ 2013-05-29 14:38
夜雨阑珊
阅读(445)
评论(0)
推荐(0)