29. 数组中出现次数超过一半的数字.

方法a. 排序取中       O(nlogn).

方法b. partition 函数分割找中位数     >=O(n).

方法c. 设计数变量,扫描一遍。     O(n).

#include <stdio.h>
int getNumber(int data[], int length){
/* if(checkInvalidArray(data, length)) return 0; */
int count = 1, value = data[0];
for(int i = 1; i < length; ++i)
{
if(count == 0){
value = data[i];
}else if(data[i] == value){
++count;
}else
--count;
}
return value;
}
int main(){
int numbers[] = {2, 2, 2, 2, 6, 6, 6, 6, 6};
int value = getNumber(numbers, sizeof(numbers) / 4);
/* if(value != 0 || !checkInvalidArray(data, length)) */
printf("%d\n", value);
return 0;
}

30. 最小的 k 个数

a. partition 函数找到第 k 个数.   >=O(n)

#include <stdio.h>
int partition(int data[], int low, int high){
int value = data[low];
while(low < high){
while(low < high && data[high] >= value) --high;
data[low] = data[high];
while(low < high && data[low] <= value) ++low;
data[high] = data[low];
}
data[low] = value;
return low;
}
void getKNumber(int input[], int length, int out[], int k){
if(!input || !out || length < 1 || k > length || k < 1) return;
int low = 0, high = length - 1, index;
do{
index = partition(input, low, high);
if(index < k-1) low = index + 1;
else if(index > k-1) high = index - 1;
}while(index != k-1);
for(int i = 0; i < k; ++i)
out[i] = input[i];
}
int main(){
int numbers[10] = {3, 5, 2, 6, 7, 4, 9, 1, 2, 6};
int k = 5;
getKNumber(numbers, 10, numbers, k);
for(int i = 0; i < k; ++i)
printf("%-3d", numbers[i]);
printf("\n");
return 0;
}

b. 构造k 个元素的大顶堆

#include <stdio.h>
void HeapAdjust(int data[], int endIndex, int father){
if(!data || endIndex < 0 || father > endIndex || father < 0) return;
int value = data[father]; // set data[0] to save the value of original father
for(int child = 2*father+1; child <= endIndex; child = 2*father+1){
if(child < endIndex && data[child] < data[child+1]) ++child;
if(data[child] < value) break;
else data[father] = data[child];
father = child;
}
data[father] = value;
}
void getKNumber(int input[], int length, int out[], int k){
if(!input || !out || length < 1 || k > length || k < 1) return;
for(int i = 0; i < k; ++i)
out[i] = input[i];
for(int i = k/2-1; i >= 0; --i)
HeapAdjust(out, k-1, i);
for(int i = k; i < length; ++i){
if(input[i] < out[0]){
out[0] = input[i];
HeapAdjust(out, k-1, 0);
}
}
}
int main(){
int numbers[10] = {3, 5, 2, 6, 7, 4, 9, 1, 2, 6};
enum{ k = 1};
int out[k+1] = {0};
getKNumber(numbers, 10, out, k);
for(int i = k-1; i >= 0; --i){
int tem = out[i];
out[i] = out[0];
out[0] = tem;
printf("%-3d", out[i]);
HeapAdjust(out, i-1, 0); // DESC
}
printf("\n");
return 0;
}

31. 连续子数组的最大和

#include <stdio.h>
bool Invalid_Input = false;
int getKNumber(int data[], int length){
Invalid_Input = false;
if(data == NULL || length < 1) {
Invalid_Input = true;
return 0;
}
int maxSum = 0x80000000;
int curSum = 0;
for(int i = 0; i < length; ++i){
if(curSum < 0) curSum = data[i];
else curSum += data[i];
if(curSum > maxSum) maxSum = curSum;
}
return maxSum;
}
int main(){
int numbers[] = {1,-2, 3, 10, -4, 7, 2, -5, -2, 4, -5, 4};
int maxSum = getKNumber(numbers, sizeof(numbers)/4);
if(!Invalid_Input)
printf("%d\n", maxSum);
return 0;
}

Chap5: question: 29 - 31的更多相关文章

  1. Chap5: question 35 - 37

    35. 第一个只出现一次的字符 char firtNotRepeat(char *s) { if(s == NULL) return 0; int i = 0; while(s[i] != '\0') ...

  2. [C++]3-1 得分(Score ACM-ICPC Seoul 2005,UVa1585)

    Question 习题3-1 得分(Score ACM-ICPC Seoul 2005,UVa1585) 题目:给出一个由O和X组成的串(长度为1~80),统计得分. 每个O的分数为目前连续出现的O的 ...

  3. 29. Divide Two Integers - LeetCode

    Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...

  4. 2016 Google code jam 答案

    二,RoundC import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundE ...

  5. 1Z0-050

    QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...

  6. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q29-Q31)

    Question 29 You are designing a SharePoint 2010 intranet site at your company. The accounting depart ...

  7. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q28-Q31)

    Question28You have a Microsoft Office SharePoint Server 2007 site.You upgrade the site to SharePoint ...

  8. HDOJ 1164 Eddy's research I(拆分成素数因子)

    Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...

  9. hdu1079 Calendar Game

    Calendar Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

随机推荐

  1. ABAP遇到的问题——1

    在创建ABAP对象的时候抛出“测试对象不能被创建在外来命名空间”的错误 原因:程序的名字不是以Z或者Y开头的.

  2. ExtJS 刷新后,默认选中刷新前最后一次选中的节点

          在对树节点进行操作后往往需要进行reload操作刷新一下树,但是很多业务都需要在树形刷新后默认选中最后一次选中的节点.这样就必须先保存前一次选中节点的信息,在reload之后再次通过节点的 ...

  3. 计算机网络(5)-----ICMP协议和PING程序

    控制报文协议(Internet Control Message Protocol) 定义 它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制消息.控制消息是指网络通不通.主机是否可 ...

  4. 计算机网络(1)-----网络层IP协议概述

    网络层(Network Layer) 概念 网络层是OSI参考模型中的第三层,介于传输层和数据链路层之间,它在数据链路层提供的两个相邻端点之间的数据帧的传送功能上,进一步管理网络中的数据通信,将数据设 ...

  5. 【转】 linux之sed用法

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed ...

  6. git常用命令有用

    http://www.cnblogs.com/cspku/articles/Git_cmds.html

  7. HDU 3622 Bomb Game(二分+2SAT)

    题意:有一个游戏,有n个回合,每回合可以在指定的2个区域之一放炸弹,炸弹范围是一个圈,要求每回合的炸弹范围没有重合.得分是炸弹半径最小的值.求可以得到的最大分数. 思路:二分+2SAT. 二分炸弹范围 ...

  8. ubuntu安装过程中的一些问题

    安装了ubuntu后,用scp命令从另外一台电脑上复制文件过来,没有执行权限,查看执行文件的目录,文件所有者全部是root/root, 原来scp是sudo状态下操作的,所以复制过来的文件都属于roo ...

  9. 计算纯文本情况下RichTextBox实际高度的正确方法(.NET)

    2016-07-17重大更新           其实有更好.更系统的方法,也是最近才发现的,分享给大家!! /// <summary> /// /// </summary> ...

  10. mysql 把文件中的sql语句导入到mysql中

    mysql -uroot -proot -Dcollege</home/wwwroot/default/data/xlxxb_2014-10-16.txt;