《编程之美》学习笔记——2.12快速寻找满足条件的两个数

第6关:求满足条件的分数 第 2 part目前在想更好的方法! def isprime(n): if n > 1: for i in range(2,n):#for else语句 执行完for循环内容后才执行else内容 if n%i == 0: #解决了判断2是否为素数的问题 #range(2,2) 会返回空值并跳过此循环语句 return False 阅读详情

一、问题

  能否快速找出一个数组中的两个数字,让这两个数字之和等于一个给定的数字,为了简化起见,我们假设这个数组中肯定存在至少一组符合要求的解。 

问题分析:

  输入:一个长度为N的数组和一个给定的数X。

  输出:数组中的两个数字A和B。

  约束:X = A + B,且A和B至少存在一组。

  其他:题目中只说了数字,说明这些数可能为正整数、负整数、零或浮点数等,不太可能通过给定数字X并抛开数组来遍历各种A和B的组合并判断它们是否在数组中。并且题目也没有告诉我们关于数组的数据规模大小和数据特点。

二、解法

  版本一:暴力破解法——遍历数组

算法:最简单的思路遍历一遍数组,对于数组中的每个值A,求解X-A,并在数组未被遍历的部分中对数组进行第二层遍历来查找是否含有值B=X-A的数,若找到则输出A和B。

时间复杂度:最好的情况是O(1),数组中的前两个数A和B,恰好满足A+B=N;最差的情况是O(n^2),数组中只存在末尾两个数A和B,满足A+B=N。

分析:《编程之美》也提到了这种方法(穷举法,我这里使用的名字"暴力破解法"源自《算法导论》,它们的思路是一致的),算法时间复杂度为O(n^2),效率较低 。

算法C实现:

1.输入时把最后一个输入的值作为题目输入中的一个给定的数X,把它前面的数作为题目输入中的数组数据。

2.尽管题目约束条件讲了满足X=A+B的A和B至少存在一组,算法还是对A和B是否存在进行了检测,它利用函数返回的标志位检测是否找到这两个值。

/**
 * @brief find two numbers value_a and value_b in the array
 * (with limit length) with given sum to make: sum = value_a + value_b
 *
 * @param[in]      array   input array
 * @param[in]      length  array length
 * @param[in]      sum     sum that makes sum = value_a + value_b
 * @param[out]     value_a value_a in the array
 * @param[out]     value_b value_b in the array
 *
 * @return if get value_a and value_b, return 1, else return 0
 */
TYPE find_two_numbers(TYPE* array, TYPE length, TYPE sum,
        TYPE* value_a, TYPE* value_b)
{
    assert(array != NULL && length >= 2 && value_a != NULL && value_b != NULL);
    TYPE i, j;
    for (i = 0; i < length - 1; i++) {
        /// set value a
        *value_a = array[i];
        /// find value b
        *value_b = sum - array[i];
        for (j = i + 1; j < length; j++) {
            if (*value_b == array[j])
                return 1;
        }
    }
    return 0;
}

  版本二:排序+二分查找

自己的思路:在前面问题分析中说过不太可能抛开数组只从给定的数X入手,可以从“对数组进行预处理”这个角度来思考问题。我们可以对数组从小到大进行排序,利用快速排序的时间复杂度是O(nlgn),接下来的我们可以发现,按照版本一的思路遍历数组求解A和B时,对于遍历数组时的一个数a1,我们先找到数组中满足a1 + am >= X的最小的数am,接下来进行第二层遍历寻找b1=X-a1这个数时,我们只需要让b1从am开始进行遍历即可。如果我们采用遍历的方式来搜索am(算法复杂度O(n)),那么算法的总时间复杂度与版本一是一致的,只是这里利用了比较语句而不是判断语句。但若采用更高效的搜索方法(如二分搜索等)来搜索am,那么算法的总时间复杂度肯定会比版本一的总时间复杂度低。

思路补充:《编程之美》中也提出了对排序数组进行二分查找(或其他高效查找方法)的方式,与我所想的思路的区别是:在寻找上面定义的b1时就采用二分搜索,而不是去先二分搜索am再遍历搜索b1,比我上面的思路更加开阔,全面利用了二分查找的快速性。

(《编程之美》原话:学过编程的人都知道,提高查找效率通常可以先将要查找的数组排序,然后用二分查找等方法进行查找,就可以将原来O(N)的查找时间缩短找O(lgN)。)

时间复杂度:快速排序算法时间复杂度O(nlgn);遍历数组中的每个数A的时间复杂度为O(n),而遍历过程中查找对应的数B(满足X = A + B)时使用二分查找算法,二分查找算法时间复杂度为O(lgn),所以查找A和B总的时间复杂度为O(nlgn)。排序算法和查找算法按序进行,因此总的时间复杂度也是O(nlgn)。

算法C实现:

1.对数组进行排序采用快速排序算法。

2.对数组进行查找采用二分查找算法。

/**
 * @brief select the last element as a pivoit.
 * Reorder the array so that all elements with values less than the pivot
 * come before the pivot, while all elements with values greater than the pivot
 * come after it (equal values can go either way). After this partitioning, the
 * pivot is in its final position. 
 *
 * @param[in,out]  array          input and output array
 * @param[in]      index_begin    the begin index of the array(included)
 * @param[in]      index_end      the end index of the array(included)
 *
 * @return the position of the pivot(index from the array)
 */
TYPE partition(TYPE* array, TYPE index_begin, TYPE index_end)
{
    /// pick last element of the array as the pivot
    TYPE pivot = array[index_end];
    /// index of the elments that not greater than pivot
    TYPE i = index_begin - 1;
    TYPE j, temp;
    /// check array's elment one by one
    for (j = index_begin; j < index_end; j++) {
        if (array[j] <= pivot) {
            /// save the elements not greater than pivot to left index of i.
            i++;
            temp = array[j];
            array[j] = array[i];
            array[i] = temp;
        }
    }
    /// set the pivot to the right position
    array[index_end] = array[++i];
    array[i] = pivot;
    /// return the position of the pivot
    return i;
}

/**
 * @brief quick sort method for input array from index_begin to index_end.
 *
 * @param[in,out]  array          input and output array
 * @param[in]      index_begin    the begin index of the array(included)
 * @param[in]      index_end      the end index of the array(included)
 */
void quick_sort(TYPE* array, TYPE index_begin, TYPE index_end)
{
    /// sort only under the index_begin < index_end condition
    if ( index_begin < index_end) {
        /// exchange elements to the pivot position by partition function
        TYPE index_pivot = partition(array, index_begin, index_end);
        /// sort the array before the pivot position
        quick_sort(array, index_begin, index_pivot - 1);
        /// sort the array after the pivot position
        quick_sort(array, index_pivot + 1, index_end);
    }
}

/**
 * @brief search the value in the array of the index by binary search method.
 *
 * @param[in]      array  input array
 * @param[in]      count  array length
 * @param[in]      value  search value
 *
 * @warning array index begin from 0
 *
 * @return index if success, else return -1
 */
TYPE binary_search(TYPE* array, TYPE count, TYPE value)
{
    assert(array != NULL && count >= 0);
    TYPE middle;
    TYPE index_begin = 0;
    TYPE index_end = count - 1;
    while (index_begin <= index_end) {
        middle = index_begin + ((unsigned)(index_end - index_begin) >> 1);
        if (array[middle] == value)
            return middle;
        else if (array[middle] < value)
            index_begin = middle + 1;
        else
            index_end = middle - 1;
    }
    return -1;
}

/**
 * @brief find two numbers value_a and value_b in the array
 * (with limit length) with given sum to make: sum = value_a + value_b
 *
 * @param[in]      array   input array
 * @param[in]      length  array length
 * @param[in]      sum     sum that makes sum = value_a + value_b
 * @param[out]     value_a value_a in the array
 * @param[out]     value_b value_b in the array
 *
 * @return if get value_a and value_b, return 1, else return 0
 */
TYPE find_two_numbers(TYPE* array, TYPE length, TYPE sum,
        TYPE* value_a, TYPE* value_b)
{
    assert(array != NULL && length >= 2 && value_a != NULL && value_b != NULL);
    /// sort array by quick sort method
    quick_sort(array, 0, length - 1);
    /// iterative for all value in array
    TYPE i;
    for (i = 0; i < length - 1; i++) {
        /// set value a
        *value_a = array[i];
        /// find value b
        *value_b = sum - array[i];
        /// search value by binary search method
        if (binary_search(array, length, *value_b) != -1)
            return 1;
    }
    return 0;
}

  版本三:哈希查找

思路(引自《编程之美》):查找使用hash表。因为给定一个数字,根据hash映射查找另一个数字是否在数组中,只需用O(1)时间。这样的话,总体的算法复杂度可以降低到O(N),但这种方法需要额外增加O(N)的hash表存储空间。在有的情况下,用空间换时间也并不失为一个好方法。

时间复杂度:O(n);空间复杂度O(n)。

分析:和其他算法相比,这个hash表算法是典型地使用空间换时间的一种查找算法。

  版本四:排序+两个指针两端扫描法

思路(引自《编程之美》):可以直接对两个数字的和进行一个有序的遍历,从而降低算法的时间复杂度。

步骤:

1.首先对数组进行排序,时间复杂度为O(nlgn)。

2.然后令i = 0,j = n-1,看arr[i] + arr[j] 是否等于Sum,如果是,则结束。如果小于Sum,则i = i + 1;如果大于Sum,则 j = j – 1。这样只需要在排好序的数组上遍历一次,就可以得到最后的结果,时间复杂度为O(n)。

时间复杂度:O(nlgn + n) = O(nlgn);空间复杂度O(1)。

分析:算法关键的一步是利用排序后指向数组两端的两个指针,通过判断Sum的大小来移动指针,在查找过程每次只移动其中一个指针,从而实现了O(n)的效率来查找满足条件的两个数(数组中利用两个下标进行遍历)。如果原来输入数组就是有序的,那么使用这个算法解决本问题的效率是最佳的。

算法C实现:

/**
 * @brief select the last element as a pivoit.
 * Reorder the array so that all elements with values less than the pivot
 * come before the pivot, while all elements with values greater than the pivot
 * come after it (equal values can go either way). After this partitioning, the
 * pivot is in its final position. 
 *
 * @param[in,out]  array          input and output array
 * @param[in]      index_begin    the begin index of the array(included)
 * @param[in]      index_end      the end index of the array(included)
 *
 * @return the position of the pivot(index from the array)
 */
TYPE partition(TYPE* array, TYPE index_begin, TYPE index_end)
{
    /// pick last element of the array as the pivot
    TYPE pivot = array[index_end];
    /// index of the elments that not greater than pivot
    TYPE i = index_begin - 1;
    TYPE j, temp;
    /// check array's elment one by one
    for (j = index_begin; j < index_end; j++) {
        if (array[j] <= pivot) {
            /// save the elements not greater than pivot to left index of i.
            i++;
            temp = array[j];
            array[j] = array[i];
            array[i] = temp;
        }
    }
    /// set the pivot to the right position
    array[index_end] = array[++i];
    array[i] = pivot;
    /// return the position of the pivot
    return i;
}

/**
 * @brief quick sort method for input array from index_begin to index_end.
 *
 * @param[in,out]  array          input and output array
 * @param[in]      index_begin    the begin index of the array(included)
 * @param[in]      index_end      the end index of the array(included)
 */
void quick_sort(TYPE* array, TYPE index_begin, TYPE index_end)
{
    /// sort only under the index_begin < index_end condition
    if ( index_begin < index_end) {
        /// exchange elements to the pivot position by partition function
        TYPE index_pivot = partition(array, index_begin, index_end);
        /// sort the array before the pivot position
        quick_sort(array, index_begin, index_pivot - 1);
        /// sort the array after the pivot position
        quick_sort(array, index_pivot + 1, index_end);
    }
}

/**
 * @brief find two numbers value_a and value_b in the array
 * (with limit length) with given sum to make: sum = value_a + value_b
 *
 * @param[in]      array   input array
 * @param[in]      length  array length
 * @param[in]      sum     sum that makes sum = value_a + value_b
 * @param[out]     value_a value_a in the array
 * @param[out]     value_b value_b in the array
 *
 * @return if get value_a and value_b, return 1, else return 0
 */
TYPE find_two_numbers(TYPE* array, TYPE length, TYPE sum,
        TYPE* value_a, TYPE* value_b)
{
    assert(array != NULL && length >= 2 && value_a != NULL && value_b != NULL);
    /// sort array by quick sort method
    quick_sort(array, 0, length - 1);
    /// search by two pointers at the begining and end of the array
    TYPE i = 0, j = length - 1, sum_temp;
    while (i < j) {
        sum_temp = array[i] + array[j];
        if (sum_temp < sum) {
            i++;
        } else if (sum_temp > sum) {
            j--;
        } else {
            *value_a = array[i];
            *value_b = array[j];
            return 1;
        }
    }
    return 0;
}

三、拓展

1.如果把这个问题中的“两个数字”改为“三个数字”或“任意个数字”时,如何求解?

思路:

  首先分析“两个数字”问题时解法:如果数组本身是有序的,那么不必对数组进行排序,此时采用版本一暴力破解法时时间复杂度为O(n^2),采用版本五两个指针两端扫描法时时间复杂度为O(n),两个方法相比后一个方法时间复杂度降了一个数量级n。

  进一步分析可以得到“三个数字”问题时解法:如果数组本身是有序的,那么不必对数组进行排序,此时采用暴力破解法时时间复杂度为O(n^3),采用两个指针两端扫描法,此时遍历时(设三个待求解的数分别为A,B,C)固定一个数A,对另外两个数B和C采用两个指针两端扫描,时间复杂度为O(n^2),两个方法相比后一个方法时间复杂度也是降了一个数量级n。

  对于“任意个数字”问题时解法:根据上面的思路,可以逐步增加数字的个数m并结合两个指针两端扫描法综合求解,数字个数为1时时间复杂度O(1),数字个数为2时时间复杂度O(n^2),,数字个数为3时时间复杂度O(n^3),当数字个数m大于数组长度(N)一半时,我们可以先求整个数组的和S(注意溢出),遍历时改为遍历N-m个数并结合两个指针两端扫描法对(N-m)个数求和,与(S - X) 判断,相等时得解,但此时解时另外m个数(数组中剔除遍历时用到的N-m个数)。这样,“任意个数字”时算法的时间复杂度最高情况下为O(n^(k)),k = n / 2。可以预计到时间复杂度会比直接采用暴力破解法降低一个数量级n。

优化:

1.“三个数字“或“任意个数字”采用两端指针扫描法时可以采用递归实现,递归接口包含了数组个数这个参数,简化代码。(递归实现想法参考自:http://blog.csdn.net/hackbuteer1/article/details/6699642)

注意:

1.数组只用排序一次。

2.“三个数字”问题在使用递归实现时,数组排好序后,从做到右遍历A,对于B和C,只需在A右端的剩余数组中进行递归实现。

2.如果完全相等的一对数字对找不到,能否找出和最接近的解?

思路:找最接近的解,可转化为求使得abs(A+B - X)最小的值,通过遍历A和B时记录使前面等式达到更小时A和B的值来实现。遍历过程中,当等式值为0时,说明存在,直接输出;若等式一直不为零,则完全遍历A和B后再输出A和B的值,此时A和B就是最接近的解了。

算法C实现:

注意:

1.算法实现基于上面排序+两个指针两端扫描法实现。

1.函数返回1时表示找到完全相等的解value_a和value_b,函数返回0时表示找到最接近的解value_a和value_b。

/**
 * @brief select the last element as a pivoit.
 * Reorder the array so that all elements with values less than the pivot
 * come before the pivot, while all elements with values greater than the pivot
 * come after it (equal values can go either way). After this partitioning, the
 * pivot is in its final position. 
 *
 * @param[in,out]  array          input and output array
 * @param[in]      index_begin    the begin index of the array(included)
 * @param[in]      index_end      the end index of the array(included)
 *
 * @return the position of the pivot(index from the array)
 */
TYPE partition(TYPE* array, TYPE index_begin, TYPE index_end)
{
    /// pick last element of the array as the pivot
    TYPE pivot = array[index_end];
    /// index of the elments that not greater than pivot
    TYPE i = index_begin - 1;
    TYPE j, temp;
    /// check array's elment one by one
    for (j = index_begin; j < index_end; j++) {
        if (array[j] <= pivot) {
            /// save the elements not greater than pivot to left index of i.
            i++;
            temp = array[j];
            array[j] = array[i];
            array[i] = temp;
        }
    }
    /// set the pivot to the right position
    array[index_end] = array[++i];
    array[i] = pivot;
    /// return the position of the pivot
    return i;
}

/**
 * @brief quick sort method for input array from index_begin to index_end.
 *
 * @param[in,out]  array          input and output array
 * @param[in]      index_begin    the begin index of the array(included)
 * @param[in]      index_end      the end index of the array(included)
 */
void quick_sort(TYPE* array, TYPE index_begin, TYPE index_end)
{
    /// sort only under the index_begin < index_end condition
    if ( index_begin < index_end) {
        /// exchange elements to the pivot position by partition function
        TYPE index_pivot = partition(array, index_begin, index_end);
        /// sort the array before the pivot position
        quick_sort(array, index_begin, index_pivot - 1);
        /// sort the array after the pivot position
        quick_sort(array, index_pivot + 1, index_end);
    }
}

/**
 * @brief find two numbers value_a and value_b in the array
 * (with limit length) with given sum to make: sum closest to value_a + value_b
 *
 * @param[in]      array   input array
 * @param[in]      length  array length
 * @param[in]      sum     sum that makes sum = value_a + value_b
 * @param[out]     value_a value_a in the array
 * @param[out]     value_b value_b in the array
 *
 * @return if get value_a and value_b, return 1, else return 0
 */
TYPE find_best_two_numbers(TYPE* array, TYPE length, TYPE sum,
        TYPE* value_a, TYPE* value_b)
{
    assert(array != NULL && length >= 2 && value_a != NULL && value_b != NULL);
    /// sort array by quick sort method
    quick_sort(array, 0, length - 1);
    /// search by two pointers at the begining and end of the array
    TYPE i = 0, j = length - 1, sum_diff_min = INT_MAX, sum_diff;
    while (i < j) {
        sum_diff = abs(array[i] + array[j] - sum);
        /// record closet value_a and value_b
        if (sum_diff < sum_diff_min) {
            sum_diff_min = sum_diff;
            *value_a = array[i];
            *value_b = array[j];
        }
        if (sum_diff < 0) {
            i++;
        } else if (sum_diff > 0) {
            j--;
        } else {
            /// get value_a and value_b with no error
            return 1;
        }
    }
    /// get value_a and value_b with error
    return 0;
}

3.把上面的两个问题综合起来,就得到这样一个题目:给定一个数N,和一组数字集合S,求S中和最接近N的子集。

蓝桥杯第十三届国赛PythonB组题解 蓝桥杯第十三届国赛PythonB组题解。包含:试题A: 斐波那契与7 试题B: 小蓝做实验 试题C: 取模 试题D: 内存空间 试题E: 近似GCD 试题I: owo 试题J: 替换字符 的题解 阅读详情

相关推荐

编程之美2.12——快速寻找满足条件的两个数或三个数

问题: 1. 快速找出一个数组中的两个数,让这两个数之和等于一个给定的值。 2. 快速找出一个数组中的三个数,让这三个数之和等于一个给定的值。 1. 解法:算法复杂度为O(nlogn)。先用快速排序对数组排序,让后用双指针(双索引)法对排序好的数组进行反向遍历,并且遍历的方向不变。(若是计算两个数的和,则初始化为i=0,j=n-1,若是计算两个数的差,则初始化为i=0,j=1) 之

林云zju的专栏 5236

面向对象程序设计-第一章:面向对象程序设计基础

1. 分数类 分数相加,两个分数分别是1/51/51/5和7/207/207/20,它们相加后得11/2011/2011/20。方法是先求出两个分数分母的最小公倍数,通分后,再求两个分子的和,最后约简结果分数的分子和分母(如果两个分数相加的结果是4/84/84/8,则必须将其约简成最简分数的形式1/21/21/2),即用分子分母的最大公约数分别除分子和分母。假定mmm、nnn的最大公约数是vvv,则它们的最小公倍数就是m∗n/vm*n/vm∗n/v。试建立一个分数类FractFractFract,完成两个分

zombotany的博客 815

AI 大模型在电商精准营销中的应用

例如,对于用户 A 和用户 B,通过比较他们对共同评价过的商品的评分差异来确定相似度。其中,(Gain(D,a))表示特征(a)对数据集(D)的信息增益,(Ent(D))表示数据集(D)的信息熵,(V)表示特征(a)的取值个数,(D^{v})表示特征(a)取值为(v)的样本子集。其中,(sim(A,B))表示用户 A 和用户 B 的相似度,(I)表示用户 A 和用户 B 共同评价过的商品集合,(R_{A,i})和(R_{B,i})分别表示用户 A 和用户 B 对商品(i)的评分。(三)营销活动效果预测。

alittlehippo的博客 1641

2022蓝桥杯学习——8.数论

数论 学习数论,首先就要知道一些基本的数学知识,比如质数,约数,欧拉函数,快速幂,欧几里得等,在下面涉及到的数学知识,除了给出题解,也会给出相应的代码模板,正所谓,工欲善其事必先利其器。 例题 1.X的因子链 题目描述 输入正整数 X,求 X 的大于 1 的因子组成的满足任意前一项都能整除后一项的严格递增序列的最大长度,以及满足最大长度的序列的个数。 输入格式 输入包含多组数据,每组数据占一行,包含一个正整数表示 X。 输出格式 对于每组数据,输出序列的最大长度以及满足最大长度的序列的个数。 每个结果占一行

m0_51474171的博客 5822

Altium Designer 21 DRC检查:从新手到高手,这10个常见错误你踩过几个?

本文详细解析Altium Designer 21 DRC检查中的10个高频错误,包括间距违规、线宽违规、未连接网络等常见问题,并提供精准修复方案和实战技巧。通过分层诊断、智能规则设置和三维排查等方法,帮助工程师从新手进阶为高手,提升PCB设计效率和质量。

weixin_26873711的博客 231

图解ADAS电源系统架构:看懂这张图,搞懂前装摄像头的供电秘密!

别以为ADAS系统就是“摄像头 + SoC”,其实最难搞的是供电系统,尤其是要满足车规级ASIL-D安全等级时。供电时序、电压稳定性、电源监控、冗余通信链路,一个都不能少。😤一旦电源选型不对,轻则系统卡顿,重则全车失控,不是闹着玩的。

weixin_43199439的博客 693

编程之美——快速寻找满足条件的两个数或三个数

编程之美——快速寻找满足条件的两个数或三个数 问题: 1. 快速找出一个数组中的两个数,让这两个数之和等于一个给定的值。 2. 快速找出一个数组中的三个数,让这三个数之和等于一个给定的值。 1. 解法:算法复杂度为O(nlogn)。先用快速排序对数组排序,让后用双指针(双索引)法对排序好的数组进行反向遍历,并且遍历的方向不变。(若是计算两个数的和,则初始化为

chenglinhust的专栏 1119

9篇9章1节:认识 SHARE 数据库

SHARE数据库是欧洲最大的跨国老龄化追踪调查项目,覆盖28个欧洲国家和以色列的50岁及以上人群。自2004年启动以来,已完成14-16万受访者的50-60万次访谈,形成跨度20年的纵向数据。该数据库采用标准化的多阶段抽样方法,收集健康、经济和社会关系等领域的多维度信息,具有高度的跨国可比性。SHARE与美国HRS、英国ELSA等调查对接,构成全球老龄化研究网络。数据免费向科研人员开放,但需注册并承诺仅用于非商业性研究。该项目由欧盟委员会等多方资助,为老龄化研究和政策制定提供了重要实证依据。

MD Code 936

[编程之美]快速寻找满足条件的两个数

在《编程之美》一书的第2.12个题目描述如下: 能否快速找出一个数组中的两个数字,让这个两个数字之和等于一个给定的值,为了简化起见,我们假设这个数组中肯定至少存在一组满足要求的解。 作者给出了3种方案: [方案一]穷举法:从数组任意取出两个数字,计算两者之和是否为给定的数字。时间复杂度O(N^2),效率较低。 [方案二]将问题变通为一个查找问题:求两个数之和,假定为sum。对于数组中的每个

andyliuxs的专栏 1563

如何选择深度强化学习算法:MuZero/SAC/PPO/TD3/DDPG/DQN/等算法

如何选择深度强化学习算法:MuZero/SAC/PPO/TD3/DDPG/DQN/等算法

丨汀、的博客 5233

sas协议(SAS PL-3)之Zoning功能

1 Zoning overview SAS zoning is implemented by a set of zoning expander devices with zoning enabled that define a zoned portion of a service delivery subsystem (ZPSDS)服务子系统. The zoning expander devices control whether a phy is permitted to participate in

sinat_37817094的博客 1537

编程之美-快速寻找满足条件的两个数

<br />题目的大概意思是:快速找出在一个数组内的两个数,让这两个数之和等于一个给定的值。书中给出的解法三觉得应该是(NlogN)复杂度中比较快的,但这种解法为什么完备还要仔细推导一下才知道。因为是找两个数之和,解法二还可以再优化。排序后可以把数组分成两段,以和的一半作为分割点,这样就在二分查找时只需找出前半部分的Sum-arr[i]是否在后半部分中。我用SLT的binary_search实现了一下,但是此法由于排序,只能返回具体的解,如果返回解在原数组中的位置,那要复杂得多。<br />//给定条件为S

林榆耿的专栏 1733

TMS320F28335调试跟踪的问题:DSP从上电复位,一直到执行main()的第一条语句之间,都干了些什么,以及CCS调试时程序查找的问题

🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。

**My Coding Family** 1251

编程之美2.12 快速寻找满足条件的两个数

这道题目的意思是,在一个数组中寻找两个数,使这两个数的和等于给定的数(找到任意一组就可以了)。      题目读完之后,感觉这道题目还是很简单的,就是遍历数组呗,走两遍,即可以在O(n2)时间复杂度内解决这个问题。不过,仔细想想之后,复杂度还是可以降低的。      首先,我们可以对数组进行排序,这样,得到的数组就是一个有序数组(假设数组是递增的),那么,我们可以利用两个指针,一个指针指向数组的第

张勤一 1732

ZYNQ BRAM实现PS与PL数据交互

有时CPU需要与PL进行小批量的数据交换,可以通过BRAM模块,也就是Block RAM实现此要求。本章通过Zynq的GP Master接口读写PL端的BRAM,实现与PL的交互。在本实验中加入了自定义的FPGA程序,并利用AXI4总线进行配置,通知其何时读写BRAM。以下为本实验原理图,CPU通过AXI BRAM Controller读取BRAM数据,CPU仅配置自定义的PL BRAM Controller 的寄存器,不通过它读写数据。

baidu_34971492的博客 2307

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

[编程之美] PSet2.12 快速寻找满足条件的两个数

 第14题(数组): 题目:输入一个数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。 要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。 例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11。

飞扬的青春的专栏 935

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

编程之美1.12 —— 寻找满足条件的两个数、三个数

http://blog.csdn.net/linyunzju/article/details/7720413 这篇文章总结的比较好,代码也写的比较清晰。 原创部分: 上面的博文,在应用“两个下标夹逼”解决“三个数字”问题、能否“重复使用数字”两个方面,给出了非常好的解释,代码清晰明了,不愧是浙大才子。下面,自己补充一些东西。 1、用排序+查找思路解决“三个数字”问题。 先确定

xiongjinshui的专栏 1518

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

编程练习-快速寻找满足条件的两个数

题目-来自编程之美 给定一个数组,快速从其中找出两个数满足其之和等于给定的数,这里假设其中至少存在一组符合要求的解; 分析 这里的关键在于快速 最为愚钝的方式当然是:穷举,从数组中任意取出两个数字进行判断,但是时间复杂度为$O(N^2)$; 一般是数组,首先进行排序,采用时间复杂度为$O(Nlog_2N)$, 然后通过一次遍历求出所需的解,时间复杂度为$O(N)$, 所以整个时间复杂度为$O...

weixin_30674525的博客 211

快速寻找满足条件的两个数或三个数

问题: 1. 快速找出一个数组中的两个数,让这两个数之和等于一个给定的值。 2. 快速找出一个数组中的三个数,让这三个数之和等于一个给定的值。 1. 解法:算法复杂度为O(nlogn)。先用快速排序对数组排序,让后用双指针(双索引)法对排序好的数组进行反向遍历,并且遍历的方向不变。(若是计算两个数的和,则初始化为i=0,j=n-1,若是计算两个数的差,则初始化为i=0,j=1) 之

唛哩唛哩轰 714

编程之美2.12——快速寻找满足条件的两个数

前奏 希望此编程艺术系列能给各位带来的是一种方法,一种创造力,一种举一反三的能力。本章依然同第四章一样,选取比较简单的面试题,恭祝各位旅途愉快。同样,有任何问题,欢迎不吝指正。谢谢。 第一节、寻找满足条件的两个数 第14题(数组): 题目:输入一个数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。 要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任

wxl3105的专栏 3442

快速寻找满足条件的两个数

快速寻找满足条件的两个数 提出问题: 能否快速找出一个数组中的两个数字,让这两个数字的和等于一个给定的值,为了简化起见我们假设这个数组中至少存在一组满足要求的解。 例如有如下两个数组, 5,6,1,4,7,9,8 给定sum=10 1,5,6,7,8,9给定sum=10 解法一: 这个题目也不是很难,也很容易理解。但是要得出高效的解法,还是需要一番思考的。

奋起的Coder。。。 1094

程序员面试100题之十:快速寻找满足条件的两个数

能否快速找出一个数组中的两个数字,让这两个数字之和等于一个给定的值,为了简化起见,我们假设这个数组中肯定存在至少一组符合要求的解。      假如有如下的两个数组,如图所示:     5,6,1,4,7,9,8     给定Sum= 10     1,5,6,7,8,9     给定Sum= 10    分析与解法     这个题目不是很难,也很容易理解。但是要得出高效率的解法,还是

Hackbuteer1的专栏 9410

编程之美2.12快速寻找满足条件的2个数

题目: 对于一个数组,快速找出两个数字,让这两个数字的和等于一个给定的值,默认假设数组中肯定有至少一组符合要求。 数组a[]={5,6,1,4,7,8,9},sum=10. 解法一: 穷举,遍历数组中所有的2个数字,相加之和看是否等于给定的数字,时间复杂度为N(N-1)/2,既O(N^2),这种方法效率不高。 1 #include "iostream" 2 using...

171
上一篇: 《编程之美》学习笔记——2.6精确表达浮点数
下一篇: 数据结构专题——线性表
chensilly8888
博客等级 码龄12年 25粉丝 59原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值