一些关于数组查找的题目

//ACM 1 数组
#include <stdio.h>
#include "sort.h"

int arrDump(int * a, int n)
{
	int i = 0;
	printf("Array:[");
	for (i=0; i<n; i++)
	{
		printf(" %d ", a[i]);
	}
	printf("]\n");
	return 0;
}

/*
删除有序数组中重复元素
时间 O(n)
空间 O(1)
*/
int removeSortedDup(int * a, int n, int * len)
{
	int i = 0, index = 0;
	for (i=1; i<n; i++)
	{
		if (a[i] != a[index])//no duplicate
		{
			a[++index] = a[i];//resort
		}
	}
	*len = index+1;
	return 0;
}

/*
允许最多两次重复
时间 O(n)
空间 O(1)
*/
int removeSortedDupTwice(int * a, int n, int * len)
{
	int i = 0, index = 0, sum = 1;
	for (i=1; i<n; i++)
	{
		if (a[i] != a[index])//no duplicate
		{
			a[++index] = a[i];//resort
			sum = 1;
		}
		else if (sum <2)
		{
			a[++index] = a[i];
			sum ++;
		}
	}
	*len = index+1;
	return 0;
}
/*网上的代码*/
int removeDuplicates(int A[], int n) {
        int index = 2, i=2;
        if (n <= 2) return n;

        for (i = 2; i < n; i++){
            if (A[i] != A[index - 2])//窗口 index-2, index-1
                A[index++] = A[i]; //操作后A[index]始终与之前元素不同
        }

        return index;
    }

/*
二分查找
*/
int BinarySearch(int a[], int len, int target)
{
	int left = 0, right = len-1, mid = 0;
	while (left <= right)
	{
		mid = (left+right) / 2;
		if (target == a[mid])
		{
			return mid;
		}
		else if(target < a[mid])
		{
			right = mid - 1;
		}
		else
		{
			left = mid + 1;
		}
	}
	return -1;
}

/*
有序数组从中间折断,前后两部分换位
Search in Rotated Sorted Array
难度在于二分边界的确定
不使用直接查找,使用二分查找
时间 O(log n)
空间 O(1)
*/
int RotateSearch(int a[], int len, int target)
{
	int left = 0, right = len-1, mid = 0;
	while (left <= right)
	{
		mid = (left+right) / 2;
		if (a[mid] == target) return mid;
		if (a[mid] < a[left])//同a[mid] > a[right],因为此时 a[left]>a[right]
		{
			if (target>a[mid] && target<=a[right])
				left = mid + 1;
			else
				right = mid - 1;
		}
		else// if (a[mid] >= a[left])
		{
			if (target>=a[left] && target<a[mid])//同target>=a[left] && target<=a[mid]
				right = mid - 1;
			else
				left = mid + 1;
		}
	}
	return -1;
}

//同上,调用之前的二分查找实现
//int RotateSearch(int a[], int len, int target)
//{
//	int left = 0, right = len-1, mid = 0;
//	while (left < right)
//	{
//		mid = (left+right) / 2;
//		if (a[mid] < a[left])//同a[mid] > a[right],因为此时 a[left]>a[right]
//		{
//			right = mid;
//		}
//		else if (a[mid] > a[right])
//		{
//			left = mid;
//		}
//		if (left == right-1)
//		{
//			mid = a[left]>a[right] ? left : right;
//			break;//pivot = mid
//		}
//	}
//	printf("Pivot index: %d\n", mid);
//	if (a[mid] == target)
//	{
//		return mid;
//	}
//	else if (target > a[0])
//	{
//		return BinarySearch(a, mid+1, target);
//	}
//	else
//	{
//		return (mid+1 + BinarySearch(&a[mid+1], len-mid, target));
//	}
//}

/*
Search in Rotated Sorted Array
允许重复元素,查找数组中是否存在target
注意特殊情况:数组有序但不连续
*/
int RotateSearchDup(int a[], int len, int target)
{
	int left = 0, right = len-1, mid = 0, tmp = -1;
	while (left <= right)
	{
		mid = (left+right) / 2;
		if (a[mid] == target) return mid;
		else if (right-left <= 1)
		{
			return -1;
		}
		if (a[mid] > a[left])//同a[mid] > a[right],因为此时 a[left]>a[right]
		{
			if (target>=a[left] && target<a[mid])//同target>=a[left] && target<=a[mid]
				right = mid - 1;
			else
				left = mid + 1;
		}
		else if (a[mid] < a[right])
		{
			if (target>a[mid] && target<=a[right])
				left = mid + 1;
			else
				right = mid - 1;
		}
		else if (a[mid] == a[left])//special situation
		{
			if (target > a[mid])
			{
				tmp = RotateSearchDup(&a[left], mid-left+1, target);
				if (tmp != -1) return left+tmp;
				tmp = RotateSearchDup(&a[mid+1], right-mid, target);
				if (tmp != -1) return mid+1+tmp;
			}
			else left = mid+1;

		}
		else if (a[mid] == a[right])
		{
			if (target > a[mid])
			{
				tmp = RotateSearchDup(&a[mid], right-mid+1, target);
				if (tmp != -1) return mid+tmp;
				tmp = RotateSearchDup(&a[left], mid-left, target);
				if (tmp != -1) return left+tmp;
			}
			else right = mid-1;
		}
	}
	return -1;
}

  某ACM网站上看到的题目,原题是英文的,今天一回头又找不着了……

时间: 2024-08-21 17:03:38

一些关于数组查找的题目的相关文章

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

poj3067Japan——树状数组查找逆序对

题目:http://poj.org/problem?id=3067 利用树状数组查找逆序对. 代码如下: #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int t,n,m,k; long long f[1005],ans; struct N{ long long aa,bb; }edge[1000005]; bool

leetcode——Search a 2D Matrix 二维有序数组查找(AC)

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

leetcode旋转数组查找 二分查找的变形

http://blog.csdn.net/pickless/article/details/9191075 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return it

PHP性能笔记:PHPExcel 与数组查找

使用 PHPExcel 导入数据,sheet 对象的 getCellByColumnAndRow 比 getCell 性能稍高,对于 1500 行的 Excel 数据,大概快 200 毫秒. 对于数组查找,应当尽可能转换为 isset 操作,避免 in_array 或 array_search 操作,在一个 10W 个元素的数组中查找 1W 个值,isset 操作仅 3 毫秒,in_array, array_search 为 14.5 秒,array_flip 交换健值仅 25 毫秒.

蓝桥杯 算法训练 ALGO-50 数组查找及替换

算法训练 数组查找及替换 时间限制:1.0s   内存限制:512.0MB 问题描述 给定某整数数组和某一整数b.要求删除数组中可以被b整除的所有元素,同时将该数组各元素按从小到大排序.如果数组元素数值在A到Z的ASCII之间,替换为对应字母.元素个数不超过100,b在1至100之间. 输入格式 第一行为数组元素个数和整数b 第二行为数组各个元素 输出格式 按照要求输出 样例输入 7 2 77 11 66 22 44 33 55 样例输出 11 33 55 M 示例代码: 1 #include<

.net字符串数组查找方式效率比较

下面是代码: static void Main(string[] args) { string[] arr = new string[] { "AAA", "BBBB", "CCCC", "DDDD", "EEEEEE", "ffffff", "ggggggg", "hhhhhh", "iii", "", &

KT学算法(二)——循环有序数组查找指定元素

问题描述 一个循环有序的数组是形如:"12,16,18,20,41,100,1,4,6,9" 这样的数组. 问题分析 对于循环有序数组,一种简单的定义是: 循环有序数组是将一个有序数组切成两段,并交换位置得到引用块内容 比如现将1,4,6,9,12,16,18,20,41,100在9和12处切分,得到两段:1,4,6,9和12,16,18,20,41,100,再交换这两段的位置就得到了一开始的循环有序数组. 另一种比较严格的定义是: 对于一个循环有序数组{A1,A2,--An},存在一