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 ...
随机推荐
- Java中的数组
1,可以创建自己的类的数组,但是自己的类必须实现get()和put函数 2,声明数组:int[] arrayOfInt.注意,定义数组时不可以指定大小 3,创建与初始化:通过new.arrayOfIn ...
- c# windows编程控件学习-2
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- AngularJs的UI组件ui-Bootstrap分享(五)——Pager和Pagination
ui-bootstrap中有两个分页控件,一个是轻量级的Pager,只有上一页和下一页的功能,另一个是功能完整的Pagination,除了上一页和下一页,还可以选择首页和最后页,并且支持多种页数的显示 ...
- 用php完成数据库的增删改查
<?php//一.连接数据库$con = mysql_connect("localhost","root","");//二.验证是否连 ...
- Android FM模块学习之三 FM手动调频
前一章主要是FM的自动调频, 接下来我们就看看FM手动调频是如何进行的.如果不清楚FM自动调频的过程,请打开超链接查看FM搜索频率流程. 首先来看一下流程图: 2.滑动刻度盘HorizontalNum ...
- ThreadPoolExecutor机制探索-我们到底能走多远系列(41)
我们到底能走多远系列(41) 扯淡: 这一年过的不匆忙,也颇多感受,成长的路上难免弯路,这个世界上没人关心你有没有变强,只有自己时刻提醒自己,不要忘记最初出发的原因. 其实这个世界上比我们聪明的人无数 ...
- vim备忘
复制指定行 5,20co$(5到20行复制到最后一行之后) 指令模式下,c的使用方式与d相同,但删除后会进入INSERT模式 删除以某一符号开头或结尾的行 :%g/^\s/d(删除以空格开头的行) : ...
- python是一个解释器
python是一个解释器 利用pip安装python插件的时候,观察到python的运作方式是逐步解释执行的 适合作为高级调度语言: 异常的处理以及效率应该是主要的问题
- [USACO08DEC] Trick or Treat on the Farm
题目描述 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N个牛棚里转 悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大,所以约翰要想尽法子让奶牛们得到快乐. ...
- oracle审计详解-转
http://blog.chinaunix.net/u2/66903/showart_2082884.htmlOracle使用大量不同的审计方法来监控使用何种权限,以及访问哪些对象.审计不会防止使用这 ...