找到数组中出现次数超过一半的数字
2016-07-22 17:15
375 查看
package test1; /** * 找到数组中出现次数超过一半的数字 * @author yanjie * */ public class Test { static int[] data = {1,2,3,2,2,2,5,4,2}; public static int partion(int[] data, int low, int high){ int temp = data[low]; while(low<high){ while(low<high && data[high]>=temp) high--; data[low] = data[high]; while(low<high && data[low]<=temp) low++; data[high] = data[low]; System.out.println(low+" "+high); } data[low] = temp; return low; } public static int bisearch(int[] data, int low, int high, int mid){ int par = 0; while(par!=mid){ par = partion( data, low, high); if(par==mid){ return data[par]; }else if(par<mid){ low = par+1; }else if(par>mid){ high = par-1; } } return 0; } public static void print(int[] data){ for(int i: data){ System.out.print(i+" "); } } public static void main(String[] args) { // TODO Auto-generated method stub print(data); int mid = (data.length)/2; int mid_data = bisearch(data, 0, data.length-1, mid); System.out.println(data[mid_data]); } }
相关文章推荐
- 抗几何攻击数字水印分类总结
- hdu_5742_It's All In The Mind
- Android进阶UI之BadgeView(数字提醒)
- BOOTStrap
- android项目报错 org.JSONException:Value<html><head><title>Apache of type java.lang.String.
- example OpenCV Hello World
- 全局变量的产生
- 【Android进阶】ListView使用“内存双缓存+硬盘缓存”加载网络图片
- HDU(1789)Doing Homework again(贪心,找出损失最少分)
- 韩信点兵
- hdu 1258 Sum it up dfs+剪枝
- iOS原生二维码扫描&libqrencode生成二维码
- 为excel自动增加目录
- ActionBar 带返回箭头
- 设计模式之建造者模式
- CentOS根分区扩容方法
- LCDC的作用
- Java中的几个基本设计模式
- UVA 673-Parentheses Balance
- Android动画开发