Exercise1.1.29 等值键
来源:互联网 发布:金蝶k3无法数据引出 编辑:程序博客网 时间:2024/06/06 01:33
import java.util.Arrays;public class BinarySearch { public static int rank(int key, int[] tar) { Arrays.sort(tar); int low = 0; int high = tar.length - 1; while (low <= high) { int mid = (low + high) / 2; if (key == tar[mid]) { return mid; } else if (key < tar[mid]) { high = mid - 1; } else { low = mid + 1; } } return -1; } public static int find(int key, int[] array) {// 找出array中所有小于key的个数 int judge = rank(key, array); if (judge != -1) { int index = judge; int i=0; while ( (i=index-1) >= 0 && array[--index] == key) { } return index + 1; } return -1; } public static int count(int key, int[] array) {// 找出array中和key相等的元素 int judge = rank(key, array); if (judge != -1) { int index = judge; int index1 = index; int index2 = index; int sum = 1; int i=0; while ((i=index1-1) >= 0 && array[--index1] == key) { sum++; } while ((i=index2+1) < array.length && array[++index2] == key) { sum++; } return sum; } return -1; } public static void main(String[] args) { int[] array = { 0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6, 7, 8, 9 }; System.out.println("find " + find(9, array)); System.out.println("count " + count(9, array)); }}
0 0
- Exercise1.1.29 等值键
- exercise1
- SSD3-exercise1
- SSD5 exercise1
- exercise1-17
- exercise1-19
- exercise1-16
- exercise1-18
- Exercise1.1.22
- python exercise1
- 卡耐基 ssd6 exercise1
- Icarnegie SSD4 Exercise1 Answer
- 分支选择结构Exercise1
- SSD6 exercise1 解题思路
- SSD6 Exercise1 深入分析
- 【UFLDL-exercise1-Sparse Autoencoder】
- 等值数目
- 等值数目
- 巧用XRename批量删除所有文件的文件名中的数字
- Leetcode——Contains Duplicate
- 欢迎使用CSDN-markdown编辑器
- Linux下Makefile文件详解
- Java中的异常体系
- Exercise1.1.29 等值键
- 总结:Ubuntu上安装JDK和Eclipse
- 虚拟机VMware3种网络模式(桥接、nat、Host-only)的工作原理
- Shortcut about SecureCRT as Linux remote terminal
- JS自动识别浏览器
- 重拾编程之路--数据结构--中缀表示法转换成后缀表示法
- MySQL高级十一——慢查询
- 面试题6:重建二叉树
- cocos2d-x骨骼动画使用