Why my C++ code got RTE but passed on my Mac, Xcode ?


  • 0
    D
    //
    

    // main.cpp
    // Triangle
    //
    // Created by Lius on 15/7/19.
    // Copyright © 2015年 iipl. All rights reserved.
    //

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <unordered_map>
    #include <set>
    #include <string>

    using namespace std;

    void printvv(vector<vector<int>> v) {
    for (auto i : v) {
    for (auto j : i) {
    printf("%d ", j);
    }
    printf("\n");
    }
    printf("\n");

    }

    class Solution {
    private:
    bool withinBound(int i, int j, vector<vector<int>>& f) {
    if (i < f.size() && i > -1 && j < f[0].size() && j > -1)
    return true;
    cout<<"without"<<i<<" "<<j<<endl;
    return false;
    }
    public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
    vector<vector<int>> f = obstacleGrid;
    if (f.size()==0) return 0;
    if (f.size()==1) return 1;
    int n = f.size();
    int m = f[0].size();

        // for (int i = 0; i < n; i++)
        //     f[i].assign(f[i].size(), 0);
        printvv(obstacleGrid);
        
        f[0][0] = 1;
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (i==0 && j==0) {
                    f[i][j]=1;
                    continue;
                }
                if (1!=obstacleGrid[i][j]) {
                    int a = 0, b = 0;
                    // f(i, j-1)
                    if (withinBound(i,j-1,obstacleGrid) && obstacleGrid[i][j-1]==0) {
                        a = f[i][j-1];
                    }
                    // f(i-1, j)
                    if (withinBound(i-1,j,obstacleGrid) && obstacleGrid[i-1][j]==0) {
                        b = f[i-1][j];
                    }
                    f[i][j] = a + b;
                    cout<<"f["<<i<<"]["<<j<<"] = a + b ="<<f[i][j]<<"  a:"<<a<<" b:"<<b<<endl;
    
                } else {
                    f[i][j] = 0;
                }
                
            }
        }
        printvv(f);
        
        cout<< "result:"<<f[m-1][n-1]<<endl;
        return f[m-1][n-1];
    }
    

    };

    int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    vector<vector<int>> nums = {{0,1,0,0},{1,0,0,0},{0,0,0,0},{0,0,0,0}};
    vector<vector<int>> &ref = nums;
    Solution s;
    s.uniquePathsWithObstacles(ref);

    return 0;
    

    }


Log in to reply
 

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