Chap5: question: 29 - 31
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的更多相关文章
- Chap5: question 35 - 37
35. 第一个只出现一次的字符 char firtNotRepeat(char *s) { if(s == NULL) return 0; int i = 0; while(s[i] != '\0') ...
- [C++]3-1 得分(Score ACM-ICPC Seoul 2005,UVa1585)
Question 习题3-1 得分(Score ACM-ICPC Seoul 2005,UVa1585) 题目:给出一个由O和X组成的串(长度为1~80),统计得分. 每个O的分数为目前连续出现的O的 ...
- 29. Divide Two Integers - LeetCode
Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...
- 2016 Google code jam 答案
二,RoundC import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundE ...
- 1Z0-050
QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q29-Q31)
Question 29 You are designing a SharePoint 2010 intranet site at your company. The accounting depart ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q28-Q31)
Question28You have a Microsoft Office SharePoint Server 2007 site.You upgrade the site to SharePoint ...
- HDOJ 1164 Eddy's research I(拆分成素数因子)
Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...
- hdu1079 Calendar Game
Calendar Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
随机推荐
- Android TextView里显示两种颜色
今天介绍一个小技巧,在Android的TextView里设置两种颜色,直接上代码: TextView TV = (TextView)findViewById(R.id.mytextview01); S ...
- Invalid byte 3 of 3-byte UTF-8 sequence
用maven编译,tomcat启动时报错:IOException parsing XML document from class path resource [applicationContext.x ...
- Ubuntu下Speedtest的安装
要安装Speedtest,需要先安装apache,参见<Ubuntu下Apache的安装>一文:*(再安装LAMP server,参见<Ubuntu下快速安装LAMP server& ...
- 在中心交换机前加入多wan口路由,华为中心交换机的学习
1.前期经过学习,好多笔记不记得了.要慢慢实践,实战中学习一遍, 2.真实情况看图 3.我们没有接路由器的时候,连接cosle口入中心交换机.telnet上去. 命令行:sys_view undo i ...
- Python的平凡之路(2)
一.标准库(sys & os): Python 的标准库(standard library) 是随着 Python 一起安装在你的电脑中的,是 Python 的一部分 (当然也有特殊情况. ...
- poj2240 floyd
//Accepted 732 KB 782 ms //floyd应用 #include <cstdio> #include <cstring> #include <ios ...
- Android Framework层Power键关机流程(二,关机流程)
二,关机流程 从前一篇博文我们知道,当用户长按Power键时会弹出(关机.重启,飞行模式等选项)对话框,我们点击关机,则会弹出关机确认对话框.那么从选项对话框到关机确认对话框又是一个什么流程呢.下面我 ...
- PHP操作Excel – PHPExcel 基本用法详解
导出excel属性设置//Include classrequire_once('Classes/PHPExcel.php');require_once('Classes/PHPExcel/Writer ...
- kinnect相关
1. kinnect的现状. http://tech.qq.com/a/20150909/046760.htm 2. kinnect的相关工作 http://baike.baidu.com/link? ...
- windows dir改成ls
习惯了linux下的ls命令,windows的dir用的很不习惯,又不想装cygwin, bash,就想把dir重命名为ls,发现dos下有个命令doskey可以完成该功能.在命令提示符下敲: > ...