Posts

132. Word Search II

Code ( Language :C++) ( Judger :ip-172-31-21-252) Edit class trieNode { public : trieNode *child[ 26 ]; string word; trieNode(){ for ( int i = 0 ; i < 26 ; i++){ child[i] = NULL ; } word = "" ; } }; class trieTree { public : trieNode *root; trieTree(){ root = new trieNode(); } void insert ( string word) { trieNode *copy = root; int size = word.size(); if (size == 0 ){ return ; } for ( int i = 0 ; i < size; i++){ int idx = word[i] - 'a' ; if (copy->child[idx] == NULL ){ copy->child[idx] = new trieNode(); } copy = copy->child[idx]; } copy->word = word; return ; } }; class Solution { public : /** * @param board: A list of lists of character * @param words: A list of string ...

MS 978. Longest Turbulent Subarray

DP version: class Solution { public:     int maxTurbulenceSize(vector<int>& A) {         const int size = A.size();         if(size < 2){             return size;         }         vector<int> dp(size, 1);         dp[0] = 1;         int res = 1;         for(int i = 0; i < size - 1; i++){             if(i % 2 == 0){                 if(A[i] > A[i + 1]){                     dp[i + 1] = dp[i] + 1;                 }             }             else{                 if(A[i] < A[i + 1]){         ...

MS 1186. Maximum Subarray Sum with One Deletion

class Solution { public:     int maximumSum(vector<int>& arr) {         const int size = arr.size();         if(size == 0){             return 0;         }         if(size == 1){             return arr[0];         }         vector<int> dp1(size, 0);//max subarray ending with delement i         vector<int> dp2(size, 0);//max subarray ending with deletion of i         dp1[0] = arr[0];         dp2[0] = 0;         int res = arr[0];         for(int i = 1; i < size; i++){             dp1[i] = max(dp1[i - 1] + arr[i], arr[i]);             dp2[i] = max(dp2[i - 1] + arr[i], dp1[i - 1]);       ...

980. Basic Calculator II

Code ( Language :C++) ( Judger :ip-172-31-12-4) Edit class Solution { public : /** * @param s: the given expression * @return: the result of expression */ int calculate ( string s) { long res = 0 , curRes = 0 , num = 0 , n = s.size(); char op = '+' ; for ( int i = 0 ; i < n; ++i) { char c = s[i]; if (c >= '0' && c <= '9' ) { num = num * 10 + c - '0' ; } if (c == '+' || c == '-' || c == '*' || c == '/' || i == n - 1 ) { switch (op) { case '+' : curRes += num; break ; case '-' : curRes -= num; break ; case '*' : curRes *= num; break ; case '/' : curRes /= num; break ; } if (c == '+' || c == ...

849. Basic Calculator III

Description 中文 English Implement a basic calculator to evaluate a simple expression string. The expression string may contain open  (  and closing parentheses  ) , the plus  +  or minus sign  - ,  non-negative  integers and empty spaces . The expression string contains only non-negative integers,  + ,  - ,  * ,  /  operators , open  (  and closing parentheses  )  and empty spaces . The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of  [-2147483648, 2147483647] Do not use the  eval  built-in library function. Have you met this question in a real interview?    Yes Problem Correction Example Example 1: Input:"1 + 1" Output:2 Explanation:1 + 1 = 2 Example 2: Input:" 6-4 / 2 " Output:4 Explanation:4/2=2,6-2=4 Code ( Language...

preOrder, inOrder, postOrder 的recursion和iteration实现 类型

这三种用通用iteration模板解决,好记住。 执行push_back进数组的位置不同。 inOrder(左根右)用的场景最多。 Code ( Language :C++) ( Judger :ip-172-31-12-4) Edit /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { public : /** * @param root: A Tree * @return: Inorder in ArrayList which contains node values. */ vector < int > inorderTraversal(TreeNode * root) { // write your code here vector < int > res; if (root == NULL ){ return res; } TreeNode *p = root; std :: stack <TreeNode *> stk; while (!stk.empty() || p != NULL ){ while (p != NULL ){ stk.push(p); p = p->left; } TreeNode *tmp = stk.top(); stk.pop(); ...

1085. Longest Univalue Path

class Solution { public : /** * @param root: * @return: the length of the longest path where each node in the path has the same value */ int longestUnivaluePath (TreeNode * root) { // Write your code here if (root == NULL ){ return 0 ; } int res = 0 ; find(root, res); return res; } int find (TreeNode *root, int &res) { if (root == NULL ){ return 0 ; } // if(root->left == NULL && root->right == NULL){ // return 1; // } int left = find(root->left, res); int right = find(root->right, res); //这里一定要先执行了 int left1 = 0 , right1 = 0 ; if (root->left != NULL ){ if (root->left->val == root->val){ left1 = 1 + left; } } if (root->right){ if (root->right->val == root->val){ ...