已排序数组 a,求其元素的绝对值 共有多少个不同的值

本文介绍了一种高效算法,用于计算已排序数组中不同绝对值的数量。通过双指针技术,实现O(n)的时间复杂度和O(1)的空间复杂度。

已排序数组 a,求其元素的绝对值 共有多少个不同的值?

 

思路:

      2个 index,从最接近0的2个元素开始向2端遍历,每次先比较2个index所在的元素的绝对值,取小值,与最后1次的值比较,如果大于,则 count++,并将该值设为 最后一次的值,然后将小的那个 index 向前进的方向加1,

 

算法的复杂度:

      因为只要遍历1次,因此复杂度是 o(n),n是数组的个数,

 

内存占用:

     需要2个 index,1个 last,1个 count,因此除数组外,额外占用的内存是固定的,即 o(1)

 

代码:

package test;

public class Test {
	public static void main(String[] args) {
		int[] is = new int[] { -10, -9, -9, -5, -4, -3, -3, -2, -1, 0, 1, 2, 5, 6, 7, 8, 9, 11, 13, 14 };
		System.out.println(funOne(is, locateNumInSortedArray(0, is)));
	}

	/**
	 * 求 已排序数组中,不同的 绝对值的个数,这里假设数组中有0,
	 * 如果数组中没有 0 可以另写方法找最接近0的2个数 的 index,并稍修改下方法对 count 值的初始化,
	 * @param arr
	 * @param zeroIndex
	 * @return
	 */
	public static int funOne(int[] arr, int zeroIndex) {
		int plusIndex = zeroIndex + 1, negativeIndex = zeroIndex - 1;
		int last = arr[zeroIndex];
		int count = 1;
		while (negativeIndex >= 0 || plusIndex < arr.length) {
			if (negativeIndex >= 0 && plusIndex < arr.length) {// both side continue
				// x = small abs(...)
				int x1;
				int absNeg = Math.abs(arr[negativeIndex]);
				if (absNeg > arr[plusIndex]) {
					x1 = arr[plusIndex];
					plusIndex += 1;

				} else if (absNeg < arr[plusIndex]) {
					x1 = absNeg;
					negativeIndex -= 1;
				} else {
					x1 = arr[plusIndex];
					plusIndex += 1;
					negativeIndex -= 1;
				}
				if (x1 > last) {
					count++;
					last = x1;
				}
			} else if (negativeIndex >= 0) { // only negative site continue
				int x2 = Math.abs(arr[negativeIndex]);
				negativeIndex -= 1;
				if (x2 > last) {
					count++;
					last = x2;
				}
			} else { // only plus site continue
				int x3 = arr[plusIndex];
				plusIndex += 1;
				if (x3 > last) {
					count++;
					last = x3;
				}
			}

		}
		return count;
	}

	/**
	 * locate num in a sorted array
	 * @param num number to locate
	 * @param arr sorted array in asc order
	 * @return index of the num in array.If not find,-1 will be return.
	 */
	public static int locateNumInSortedArray(int num, int[] arr) {
		if (arr.length == 0 || arr[0] > num || arr[arr.length - 1] < num) {
			return -1;
		}

		int startIndex = 0, endIndex = arr.length - 1;
		while (startIndex <= endIndex) {
			int middleIndex = (startIndex + endIndex) >> 1;
			if (num == arr[middleIndex]) {
				return middleIndex;
			} else if (num > arr[middleIndex]) {
				startIndex = middleIndex + 1;
			} else {
				endIndex = middleIndex - 1;
			}
		}
		return -1;
	}
}
 
### 实现整数数组绝对值升序排序的算法 以下是一个完整的C语言程序,用于实现整数数组绝对值升序排序的功能,并在绝对值相等时按照实际升序排序。程序还包括输入输出格式化的处理。 ```c #include <stdio.h> #include <stdlib.h> // 定义比较函数 int compare(const void *a, const void *b) { int x = *(const int *)a; int y = *(const int *)b; if (abs(x) == abs(y)) { // 如果绝对值相等,则按实际升序排序 return x - y; } return abs(x) - abs(y); // 按绝对值升序排序 } int main() { int n; // 输入数组大小 printf("请输入数组的大小: "); scanf("%d", &n); // 动态分配数组内存 int *arr = (int *)malloc(n * sizeof(int)); if (arr == NULL) { printf("内存分配失败!\n"); return 1; } // 输入数组元素 printf("请输入 %d 个整数:\n", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // 使用 qsort 函数进行排序 qsort(arr, n, sizeof(int), compare); // 输出排序后的数组 printf("排序后的数组如下:\n"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); // 释放动态分配的内存 free(arr); return 0; } ``` #### 程序说明 - **输入**:用户需要输入数组的大小`n`以及`n`个整数。 - **排序规则**: - 首先按照绝对值升序排序[^3]。 - 如果两个数字的绝对值相等,则按照实际升序排序[^3]。 - **核心逻辑**: - 使用`qsort`函数进行排序,提供自定义的比较函数`compare`[^3]。 - 在`compare`函数中,首先比较两个数字的绝对值,如果绝对值相等,则进一步比较它们的实际[^3]。 - **内存管理**:通过`malloc`动态分配数组内存,并在程序结束前使用`free`释放内存[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值