Posts

Showing posts with the label hard

MS 1069. Remove Comments

Code ( Language :C++) ( Judger :cloudjudge-cluster-6) Edit class Solution { public : /** * @param source: List[str] * @return: return List[str] */ vector < string > removeComments( vector < string > &source) { // write your code here vector < string > res; bool blocked = false ; string out = "" ; for ( string line : source) { for ( int i = 0 ; i < line.size(); ++i) { if (!blocked) { //区分是否 blocked,即是否在/*里面 if (i == line.size() - 1 ) { out += line[i]; } else { string t = line.substr(i, 2 ); if (t == "/*" ) { blocked = true ; ++i; } else if (t == "//" ) break ; ...

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 ...

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...

246. Binary Tree Path Sum II

Code ( Language :C++) ( Judger :cloudjudge-cluster-5) 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: the root of binary tree * @param target: An integer * @return: all valid paths */ vector < vector < int >> binaryTreePathSum2(TreeNode * root, int target) { // write your code here vector < vector < int >> res; vector < int > path; dfs(root, target, res, path); return res; } void dfs (TreeNode *root, int target, vector < vector < int >> &res, vector < int > &path) { //int sum = 0; if (root == NULL ){ return ; } path.push_back(root->val); /...

AM 789. The Maze III

需要多一个状态矩阵表示 方向 Code ( Language :C++) ( Judger :cloudjudge-cluster-9) Edit class Solution { public : /** * @param maze: the maze * @param ball: the ball position * @param hole: the hole position * @return: the lexicographically smallest way */ struct node { int x, y; node( int x, int y){ this ->x = x; this ->y = y; } }; const vector < int > dx = { -1 , 1 , 0 , 0 }; const vector < int > dy = { 0 , 0 , -1 , 1 }; const string movs = "udlr" ; //错误 把这里写错了,debug了半小时,简直不能忍!!!! const int dir = 4 ; bool canStop ( int x, int y, vector < vector < int >> &maze) { if (x < 0 || y < 0 || x >= maze.size() || y >= maze[ 0 ].size() || maze[x][y] == 1 ){ return true ; } return false ; } string findShortestWay ( vector < vector < int >...

L 417. Valid Number

lintcode test case不全 Code ( Language :C++) ( Judger :ip-172-31-5-19) Edit class Solution { public : /** * @param s: the string that represents a number * @return: whether the string is a valid number */ bool isNumber ( string &s) { // write your code here // 这种类型的题,一定要先确定好不valid的情况。然后,统一一个大的思路框架。最后实现。 // non valid: 1, 非数字,'.','e'的字符 2,.和e出现1一次以上 3,e出现在开头或结尾 4, e和.不能再一起 //框架: 把空格都去掉;遍历判断; const int size = s.size(); if (size == 0 ){ return false ; } // 去空格 int len = 0 ; for ( int j = 0 ; j < size; j++){ if (s[j] != ' ' ){ s[len++] = s[j]; } } //空字符 if (len == 0 ){ return false ; } // 单独 . 和 e的情况 if (len == 1 ){ if (s[ 0 ] == '.' || s[ 0 ] == 'e' ){ retur...