在排序数组中的出现次数 Count the number of occurrences in a sorted array
来源:互联网 发布:汽车网络推广 编辑:程序博客网 时间:2024/06/08 10:15
在排序数组中的出现次数 Count the number of occurrences in a sorted array
http://www.geeksforgeeks.org/count-number-of-occurrences-in-a-sorted-array/
给定一个排序数组arr[]和一个数x,求出x在arr[]中出现的次数。例子:
Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output: 4 // x (or 2) occurs 4 times in arr[] Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 3 Output: 1 Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 1 Output: 2 Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 4 Output: -1 // 4 doesn't occur in arr[]解法:
- 用二分查找法找到x在arr[]里第一次出现的位置i。
- 用二分查找法找到x在arr[]里最后一次出现的位置j。
- 返回 j-i+1
/* if x is present in arr[] then returns the count of occurrences of x, otherwise returns -1. */int count(int arr[], int x, int n){ int i; // index of first occurrence of x in arr[0..n-1] int j; // index of last occurrence of x in arr[0..n-1] /* get the index of first occurrence of x */ i = first(arr, 0, n-1, x, n); /* If x doesn't exist in arr[] then return -1 */ if(i == -1) return i; /* Else get the index of last occurrence of x. Note that we are only looking in the subarray after first occurrence */ j = last(arr, i, n-1, x, n); /* return count */ return j-i+1;} /* if x is present in arr[] then returns the index of FIRST occurrence of x in arr[0..n-1], otherwise returns -1 */int first(int arr[], int low, int high, int x, int n){ if(high >= low) { int mid = (low + high)/2; /*low + (high - low)/2;*/ if( ( mid == 0 || x > arr[mid-1]) && arr[mid] == x) return mid; else if(x > arr[mid]) return first(arr, (mid + 1), high, x, n); else return first(arr, low, (mid -1), x, n); } return -1;} /* if x is present in arr[] then returns the index of LAST occurrence of x in arr[0..n-1], otherwise returns -1 */int last(int arr[], int low, int high, int x, int n){ if(high >= low) { int mid = (low + high)/2; /*low + (high - low)/2;*/ if( ( mid == n-1 || x < arr[mid+1]) && arr[mid] == x ) return mid; else if(x < arr[mid]) return last(arr, low, (mid -1), x, n); else return last(arr, (mid + 1), high, x, n); } return -1;} /* driver program to test above functions */int main(){ int arr[] = {1, 2, 2, 3, 3, 3, 3}; int x = 3; // Element to be counted in arr[] int n = sizeof(arr)/sizeof(arr[0]); int c = count(arr, x, n); printf(" %d occurs %d times ", x, c); getchar(); return 0;}
0 0
- 在排序数组中的出现次数 Count the number of occurrences in a sorted array
- Count occurrences of a number in a sorted array with duplicates using Binary Search
- Write a method that counts the number of occurrences of a given character in a string
- 统计某列出现次数 Counting occurrences in first column of a file
- Given an array of ages (integers) sorted lowest to highest, output the number of occurrences for eac
- count the number of 1 in a decimal integer
- count duplicate in a sorted array
- lettcode:540. Single Element in a Sorted Array(已排序数组中出现一次的数字,其他数字出现两次)
- Find missing number in a sorted array
- quick count the number of the rows in a big table
- Give a very good method to count the number of ones in a "n" (e.g. 32) bit number.
- count the number of "1" in the binary figure
- HDU 3336 Count the string 所有前缀在串中的出现总次数
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)
- Get max consecutive occurrences of value in array
- 循环有序数组中的二分查找 Search in a rotated sorted array
- 循环有序数组中的二分查找 Search in a rotated sorted array
- 查找已排序的旋转后的数组中的数(Search in Rotated Sorted Array)
- HDU's ACM 1040 As Easy As A+B
- Cheap nfl super bowl jerseys wholesale from china
- wholesale stitched jerseys from china
- 十六进制与十进制之间的转换
- 搜索引擎项目文档-----环境搭建
- 在排序数组中的出现次数 Count the number of occurrences in a sorted array
- 杨辉三角
- cocos2dx ver3.3 的Sprite3DTest 例子中可以学到什么
- cheap authentic jerseys from china
- [分享]Ubuntu12.04安装基础教程(图文)
- 图 DFS BFS 回溯
- Ubuntu12.04安装Jdk1.7
- cheap china nfl jerseys
- hats and jerseys for cheap