LeetCode algorithm solution to a problem [441- coins arranged to be further improvement]

Title Description

answer:

Method 1: enumeration method (not explained)
Method 2: Arithmetic Progression [Method] be appreciated that
Method 3: [dichotomy be appreciated]

Code:

class Solution {
public:
    int arrangeCoins(int n) {
        /* 方法1:枚举 */
        if(n == 0)
        {
            return 0;
        }
        int res = 1;
        long long tmp = 0;
        for(int i = 2; ; i++)
        {
            tmp += i;
            if(tmp >= n)
            {
                return res;
            }
            res++;
        } 
        
        /* 方法2:等差数列公式法【待理解】
        return (int) (-1 + sqrt(1 + 8 * (long) n)) / 2;
        */
		
		/* 方法3:二分法【待理解】*/
    }
};
He published 197 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_41708792/article/details/104566610