本文主要是介绍**Leetcode 754. Reach a Number | 拼多多笔试第三题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://leetcode.com/problems/reach-a-number/description/
开始一直手残,,target=-target这行代码 写了,但是符号忘记带了。。
class Solution {
public:long long cal( long long n, long long target ) {long long ret = (n * n + n - 2 * target);// cout << "ret:" << ret << endl;return ret/2;}int reachNumber(int t) {long long target = t;if (target < 0) target = -target;long long n = (-1 + sqrt( 1 + 8 * target )) / 2;n--;
// cout << "pre n:" << n << "->" << cal(n, target) << endl;while (cal(n, target) < 0) n++;
// cout << "bk n:" << n << endl;int v = cal(n, target);
// cout << "v:" << v << endl;if (v == 0) {// cout << n << endl;return n;} else {while (cal(n, target) % 2) {n ++;}// cout << n << endl; return n;}}
};
这篇关于**Leetcode 754. Reach a Number | 拼多多笔试第三题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!