二分查找,数组去重Java
来源:互联网 发布:linux使用教程.pdf 编辑:程序博客网 时间:2024/05/21 17:47
/** * 二分查找 */package algorithms;/** * @author * * @time 2017年5月9日 下午3:48:29 */public class BinarySearch { public static int rank(int key, int[] a){ //数组必须是有序的 int head = 0; int tail = a.length - 1; while(head <= tail){ //被查找的键要么不存在,要么必然存在于a[head..tail]之中 int mid = head + (tail - head)/2; if(key < a[mid]){ tail = mid - 1; }else if(key > a[mid]){ head = mid + 1; }else{ return mid; } } return -1; } //递归法 public static int binarySearch(int[] a,int head,int tail,int key){ //数组必须是有序的 // int head = 0; // int tail = a.length - 1; if(head <= tail){ //被查找的键要么不存在,要么必然存在于a[head..tail]之中 int mid = head + (tail - head)/2; if(key < a[mid]){ tail = mid - 1; }else if(key > a[mid]){ head = mid + 1; }else{//key == a[mid] return mid; } return binarySearch(a,head,tail,key); } return -1; } public static void main(String[] args) {// int[] a = {10,20,30,40,50,60};// int i = rank(40,a);// System.out.println(i);// if(i>-1){// System.out.println(a[i]);// }// System.out.println(binarySearch(a,0,a.length-1,40)); int[] a = {10,20,30,30,30,40,40,40,40,50,60}; int[] b = duplicateRemoval(a);//去重后的数组 } /** * 数组去重 */ private static int[] duplicateRemoval(int[] a) { // int num = count(a); int[] b = new int[a.length-num]; int count = 0; b[0] = a[0]; for(int i = 0;i < a.length-1;i++){ if(a[i]==a[i+1]){ count ++; }else{ b[i+1-count] = a[i+1]; } } return b; } /** * 计算重复数量 */ private static int count(int[] a) { int count = 0; for(int i = 0;i < a.length-1;i++){ if(a[i]==a[i+1]){ count ++; } } return count; }}
0 0
- 二分查找,数组去重Java
- java数组去重
- Java数组去重
- java数组去重
- Java 数组去重
- java数组去重
- Java 数组去重
- 重学java之随机数组二分查找
- 重学java之随即数组二分查找
- 去哪儿-二分查找-Java
- java 字符串数组去重
- java数组去重总结
- java数组去重总结
- java关于数组去重
- java数组去重总结
- JAVA数组去重和JavaScript数组去重比较
- java数组二分查找法
- Java数组与二分查找
- Ubuntu14.04+CUDA8.0+TitanX+caffe配置教程
- linux端口映射
- 数据结构之栈复杂数据应用实现(2)
- oracel存储过程 创建错误:ORA-06550: line 2, column 2:PLS-00905
- 截取打印机的数据
- 二分查找,数组去重Java
- 在CentOS上搭建PHP服务器环境
- CountDownLatch使用
- Hadoop初探(一)
- SpringBoot项目在eclipse启动成功,在idea中失败
- ZCMU 1143: 又是比智力
- 无题
- spring boot 支持jar包运行jsp
- 开发人员的必备技能