LeetCode 37~38__LeetCode

來源:互聯網
上載者:User
LeetCode 37. Sudoku Solver 題目:

https://leetcode.com/problems/sudoku-solver/description/ 題意:

寫一個程式通過填充空格來解決數獨。

空格用 ‘.’ 表示。

你可以假設只有唯一解。 思路:

用 dfs d f s dfs暴力搜尋,枚舉每一個空位置應該填什麼數字。
更高效的方法是用跳舞鏈,已經不會寫了 代碼:

class Solution {    static const int N = 10;public:    void solveSudoku(vector<vector<char>>& board) {        bool row[N][N], col[N][N], block[N][N];        memset(row, 0, sizeof(row));        memset(col, 0, sizeof(col));        memset(block, 0, sizeof(block));        for(int i = 0; i < 9; ++i)            for(int j = 0; j < 9; ++j)                if(isdigit(board[i][j]))                {                    int val = board[i][j] - '0';                    row[i][val] = true;                    col[j][val] = true;                    block[(i/3)*3+j/3][val] = true;                }        dfs(board, 0, 0, row, col, block);    }private:    bool dfs(vector<vector<char>> &board, int x, int y, bool row[][N], bool col[][N], bool block[][N])    {        if(x == 9) return true;        int nx = x, ny = y + 1;        if(ny == 9)        {            ++nx;            ny = 0;        }        if(isdigit(board[x][y])) //當前位置已經給定了數字,不能動            return dfs(board, nx, ny, row, col, block);        for(int i = 1; i <= 9; ++i)//當前位置為空白,依次嘗試把1-9這9個數字填入,嘗試可不可以        {            if(!row[x][i] && !col[y][i] && !block[(x/3)*3+y/3][i])            {                row[x][i] = col[y][i] = block[(x/3)*3+y/3][i] = true;                board[x][y] = i + '0';                if(dfs(board, nx, ny, row, col, block)) return true;                board[x][y] = '.';                row[x][i] = col[y][i] = block[(x/3)*3+y/3][i] = false;            }        }        return false;    }};
LeetCode 38. Count and Say 題目:

https://leetcode.com/problems/count-and-say/description/ 題意:

數數並說序列是一個整數序列,第二項起每一項的值為對前一項的計數,其前五項如下: 1 11 21 1211 111221
1 被讀作 “一個一” 即 11。
11 被讀作 “兩個一” 即 21。
21 被讀作 “一個二 和 一個一” 即 1211。

給一個正整數 n ,輸出數數並說序列的第 n 項。

注意:該整數序列的每項都輸出為字串。 思路:

無非是類比罷了,就是看寫的簡潔不簡潔了 代碼:

class Solution {public:    string countAndSay(int n) {        string ans = "1";        for(int i = 1; i < n; ++i)        {            string temp;            for(int j = 0; j < ans.size(); ++j)            {                int num = 1;                while(j+1 < ans.size() && ans[j] == ans[j+1])                {                    ++j;                    ++num;                }                temp += to_string(num) + ans[j];            }            ans = temp;        }        return ans;    }};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.