数据结构练习(09)排序数组中和为给定值的两个数字
http://zhedahht.blog.163.com/blog/static/2541117420072143251809/
关于扩展1,只能想到O(n2)的思路,相信肯定还会有更优的解法,找到了再更新。
bool find2number(int data[], unsigned int len, int sum, int& num1, int& num2) { bool found = false; if (len < 1) return found; int ahead = len - 1; int behind = 0; while (behind < ahead) { if (data[behind] + data[ahead] == sum) { num1 = data[behind]; num2 = data[ahead]; found = true; break; } else if (data[behind] + data[ahead] > sum) --ahead; else ++behind; } return found; }
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------