今日题目(对应书上第39~42题):

  1. 数组中出现次数超过一半的数字
  2. 最小的k个数(top k,重点!)
  3. 数据流中的中位数
  4. 连续子数组的最大和

今天的题目都比较经典,特别是第2题。

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

题目描述:
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。 思路:
有两种方法,
一,利用类似于快排的思想,寻找数组中的中位数,然后再检查是否满足出现次数。
二,根据数组的特点来做。

代码如下:

 //方法一,快排
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
if(array.length == 0) return 0;
int start = 0, end = array.length-1;
int mid = array.length>>1;
while(start < end){
int ind = partition(array,start,end);
if(ind == mid)
break;
if(ind > mid)
end = ind-1;
if(ind < mid)
start = ind+1;
}
if(check(array,array[mid]))
return array[mid];
else return 0;
} public boolean check(int[] nums,int result){
int times = 0;
for(int n:nums){
if(n == result)
times++;
}
return times*2 > nums.length;
} public int partition(int[] nums,int start,int end){
int target = nums[end];
int res = start;
for(int i = start; i < end; i++){
if(nums[i] < target){
int swap = nums[i];
nums[i] = nums[res];
nums[res] = swap;
res++;
}
}
nums[end] = nums[res];
nums[res] = target;
return res;
}
} //方法二
public int MoreThanHalfNum_Solution(int [] array) {
if(array.length == 0) return 0;
int result = array[0];
int times = 1;
for(int n:array){
if(times == 0){
result = n;
times = 1;
}else if(result == n)
times++;
else
times--;
}
if(check(array,result))
return result;
else return 0;
}

2. 最小的k个数

题目描述:
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。 思路:
这道题是经典的top K问题,有两种解法:
1,运用快排,找出第K个数的位置,将前面的数输出
2,利用容量为K的最大堆,循环数组,每次替换掉堆中最大的数

代码如下:

 //快排
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
ArrayList<Integer> res = new ArrayList();
if(k > input.length || k == 0) return res;
int start = 0, end = input.length-1;
int ind = partition(input,start,end);
while(ind != k-1){
if(ind > k-1){
end = ind-1;
}else{
start = ind+1;
}
ind = partition(input,start,end);
}
for(int i = 0;i < k; i++)
res.add(input[i]);
return res;
} public int partition(int[] nums,int start,int end){
int target = nums[end];
int ind = start;
for(int i = start; i < end;i++){
if(nums[i] < target){
int swap = nums[i];
nums[i] = nums[ind];
nums[ind] = swap;
ind++;
}
}
nums[end] = nums[ind];
nums[ind] = target;
return ind;
}
} //利用最大堆
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
if(k > input.length || k < 1) return new ArrayList();
PriorityQueue<Integer> maxHeap = new PriorityQueue(k,new Comparator<Integer>(){
public int compare(Integer o1,Integer o2){
return o2.compareTo(o1);
}
});
for(int i = 0; i < input.length; i++){
if(maxHeap.size() < k)
maxHeap.add(input[i]);
else{
if(maxHeap.peek() > input[i]){
maxHeap.poll();
maxHeap.add(input[i]);
}
}
}
return new ArrayList(maxHeap);
}
}

3.数据流中的中位数

题目描述:
如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。 思路:
这道题的关键是选择一个怎样的数据结构来维护数据流,可以使插入和查询的速度都比较理想。在这边维护了一个最大堆和一个最小堆,最大堆存储中位数左边的数字,最小堆存储中位数右边的数字。

代码如下:

 public class Solution {
int count = 0;
PriorityQueue<Integer> min = new PriorityQueue();
PriorityQueue<Integer> max = new PriorityQueue(new Comparator<Integer>(){
public int compare(Integer o1,Integer o2){
return o2-o1;
}
});
public void Insert(Integer num) {
if((count&1) == 0){
min.offer(num);
max.offer(min.poll());
}else{
max.offer(num);
min.offer(max.poll());
}
count++;
} public Double GetMedian() {
if(((min.size()+max.size())&1) == 0)
return (min.peek()+max.peek())/2.0;
else
return max.peek()*1.0;
}
}

4.连续子数组的最大和

题目描述:
输入一个整数数组,数组里有正数也有负数。数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为O(n)。 思路:
这道题也算是比较经典的一道题了,面经里面经常看到,下面给出两种解法。

代码如下:

 //解法一
public class Solution {
public int FindGreatestSumOfSubArray(int[] array) {
int max = Integer.MIN_VALUE;
int sum = 0;
for(int n:array){
if(sum <= 0)
sum = n;
else
sum += n;
max = max>sum?max:sum; }
return max;
}
} //解法二,动态规划
public class Solution {
public int FindGreatestSumOfSubArray(int[] array) {
int[] dp = new int[array.length];
dp[0] = array[0];
for(int i = 1; i < array.length; i++){
if(dp[i-1] < 0)
dp[i] = array[i];
else
dp[i] = array[i] + dp[i-1];
}
int res = dp[0];
for(int n:dp)
res = n>res?n:res;
return res;
}
}

《剑指offer》算法题第八天的更多相关文章

  1. 剑指offer算法题

    数组中只出现一次的数字(一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字): 解法在于位运算中的异或,直接异或可以得到这两个数的异或,按照最后的有效数字位可以 ...

  2. 剑指offer算法总结

    剑指offer算法学习总结 节选剑指offer比较经典和巧妙的一些题目,以便复习使用.一部分题目给出了完整代码,一部分题目比较简单直接给出思路.但是不保证我说的思路都是正确的,个人对算法也不是特别在行 ...

  3. 剑指Offer——算法复杂度中的O(logN)底数是多少

    剑指Offer--算法复杂度中的O(logN)底数是多少 前言 无论是计算机算法概论.还是数据结构书中,关于算法的时间复杂度很多都用包含O(logN)这样的描述,但是却没有明确说logN的底数究竟是多 ...

  4. 剑指 offer 第一题: 二维数组中的查找

    打算写 图解剑指 offer 66 题 的系列文章,不知道大家有没有兴趣

  5. 剑指Offer编程题2——替换空格

    剑指Offer编程题2——替换空格 题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happ ...

  6. 剑指Offer编程题1——二维数组中的查找

    剑指Offer编程题1---------------二维数组中的查找 题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完 ...

  7. 剑指offer编程题66道题 36-66

    36.两个链表的第一个公共节点 题目描述 输入两个链表,找出它们的第一个公共结点. 1.具有重合节点的两个链表是一个Y字性,用两个堆栈放这两个链表,从尾部开始遍历,直到遍历到最后一个重合节点. 这种算 ...

  8. 牛客网剑指offer刷题总结

    二维数组中的查找: 题目描述:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 两 ...

  9. LeetCode剑指Offer刷题总结(一)

    LeetCode过程中值得反思的细节 以下题号均指LeetCode剑指offer题库中的题号 本文章将每周定期更新,当内容达到10题左右时将会开下一节. 二维数组越界问题04 public stati ...

  10. 剑指offer编程题Java实现——面试题11数值的整数次方

    题目: 实现函数double power(double base,int exponent),求base的exponent次方.不得使用库函数,同时不需要考虑大数问题. 解题思路:最一般的方法实现数值 ...

随机推荐

  1. POJ - 3687 Labeling Balls (拓扑)

    (点击此处查看原题) 题意 此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的 ...

  2. 图论+思维(2019牛客国庆集训派对day2)

    题意:https://ac.nowcoder.com/acm/contest/1107/J n个点的完全图编号0-n-1,第i个点的权值为2^i,原先是先手选取一些边,然后后手选取一些点,满足先手选取 ...

  3. Snoopy.class.php使用手册

    Snoopy - the PHP net client v1.2.4 Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单.Snoopy的特点:1.抓取网页的内容 fetch2 ...

  4. h5中的分组元素figure、figcaption、hgroup元素介绍

    分组元素用于对页面中的内容进行分组. figure元素和figcaption元素 figure元素用于定义独立的流内容(图像.图表.照片.代码等),一般指一个独立的单元.figure元素的内容应该与主 ...

  5. Elastic Search中mapping的问题

    Mapping在ES中是非常重要的一个概念.决定了一个index中的field使用什么数据格式存储,使用什么分词器解析,是否有子字段,是否需要copy to其他字段等.Mapping决定了index中 ...

  6. redis 主从、哨兵、集群

    出处: redis主从复制和哨兵 Redis集群方式共有三种:主从模式,哨兵模式,cluster(集群)模式 一.Redis主从复制 主从复制:主节点负责写数据,从节点负责读数据,主节点定期把数据同步 ...

  7. Left4Dead2 LAN Online

    Left4Dead2 LAN Online Franklin vs Wolverine 求生之路 局域网联机说明 ============================ 局域网联机方法: 1.先找到 ...

  8. 你不知道的css各类布局(四)之响应式布局

    响应式布局 概念 响应式布局指的是同一页面在不同屏幕尺寸下有不同的布局 布局特点 响应式设计的目标是确保一个页面在所有终端上(各种尺寸的PC.手机.手表.冰箱的Web浏览器等等)都能显示出令人满意的效 ...

  9. luogu题解P2502[HAOI2006]旅行--最小生成树变式

    题目链接 https://www.luogu.org/problemnew/show/P2502 分析 一个很\(naive\)的做法是从\(s\)到\(t\)双向BFS这当然会TLE 这时我就有个想 ...

  10. Laravel 查询或写入Enum字段出错的bug解决办法

    查询: if($request->filled('type')){ $where[] = ['type', strval(intval($request->input('type')))] ...