C++ 0ms solution using only int type


  • 1
    G
    class Solution {
    public:
    	vector<int> getRow(int rowIndex) {
    		vector<int> ret = { 1 };
    		for (int i = 0; i < rowIndex; i++)
    		{
    		    int a = rowIndex - i, b = i + 1;
    		    int g = gcd(a, b);
    		    a /= g;
    		    b /= g;
    			ret.push_back(ret[i] / b * a);
    		}
    		return ret;
    	}
    private:
        int gcd(int a, int b)
        {
            int m = a % b;
            while (m != 0)
            {
                a = b - m;
                b = m;
                m = a % b;
            }
            return b;
        }
    };
    

    Divide before multiplication in case of overflow.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.