Valid Sudoku

class Solution { public: bool isValidSudoku(vector<vector > &board) { unordered_set columnCheck[9]; unordered_set rowCheck[9]; unordered_set blockCheck[9]; for (int i = 0; i < 9; ++ i){ for (int j = 0; j < 9; ++ j){ if (rowCheck[i].count(board[i][j])) return false; if...

Valid Parentheses

Description https://oj.leetcode.com/submissions/detail/12337172/ Difficulty: 1.5/5.0 star Analysis and solution We use a stack to check if the current parenthesis is valid. class Solution { public: bool isValid(string s) { stack<char>valid; unordered_map<char, char>match; match['}'] = '{'; match[']'] = '['; match[')'] = '(';...

Sum Root to Leaf Numbers

Description https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Difficulty: 1.5/5.0 Analysis and solution /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution {...

Remove Nth Node From End of List

Description https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ Difficulty: 2.5/5.0 Analysis and solution The problem requires to remove the node from the end of the list within one sweep. So we set two pointers with one being nodes ahead the other. /** * Definition for singly-linked...

Palindrome Number

Description https://oj.leetcode.com/problems/palindrome-number/ Difficulty: 2.5/5.0 Analysis and solution The main thing you want to avoid in this problem is overflow. If you reverse the integer, it overflows for 1111119999. If you write while(x/(10 * unitHigh)), it overflows for any integer greater...