Posts

Showing posts from July, 2019

1185. Complex Number Multiplication

Code ( Language :C++) Edit class Solution { public : /** * @param a: a string * @param b: a string * @return: a string representing their multiplication */ string complexNumberMultiply ( string &a, string &b) { // Write your code here int sizeA = a.size(); int i = 0 ; while (i < sizeA && a[i] != '+' ){ i++; } int ai = stoi(a.substr( 0 , i)); int aq = stoi(a.substr(i + 1 , sizeA - 1 - (i + 1 ))); int size = b.size(); i = 0 ; while (i < size && b[i] != '+' ){ i++; } int bi = stoi(b.substr( 0 , i)); int bq = stoi(b.substr(i + 1 , size - 1 - (i + 1 ))); int newI = ai * bi - aq * bq; int newQ = ai * bq + aq * bi; string res = to_string(newI) + "+" ; res += to_string(newQ) + "i" ; return r...

1454. Word Frequency Count

Description 中文 English Input a string  s  and a string list  excludeList  to find all the most frequent words in  s  that do not exist in  excludeList . Your results will be sorted by lexicographic order by online judge. The words are case-insensitive and finally return lowercase words Non-alphabetic characters in the string are considered as spaces, and spaces are word separators The length of all words does not exceed  10^5 1 0 ​ 5 ​ ​  and the length a word does not exceed  100 1 0 0 Have you met this question in a real interview?    Yes Problem Correction Example Example 1: Input:s="I love Amazon.", excludeList=[] Output:["i","love","amazon"] Explanation: "i", "love", and "amazon" are the words that appear the most. Example 2: Input:s="Do not trouble trouble.", excludeList=["do"] Output:["trouble"] Explanation: "trouble" is ...

1457. Search Subarray

Given an array  arr  and a nonnegative integer  k , you need to find a continuous array from this array so that the sum of this array is  k . Output the length of this array. If there are multiple such substrings, the return end position is the smallest, and if there are more than one, the return start position is the smallest. If no such subarray is found,  -1  is returned. The length of the array does not exceed  10^6 1 0 ​ 6 ​ ​ , each number in the array is less than or equal to  10^6 1 0 ​ 6 ​ ​ , and  k does not exceed  10^6 1 0 ​ 6 ​ ​ . Have you met this question in a real interview?    Yes Problem Correction Example Example 1 : Input:arr=[1,2,3,4,5] ,k=5 Output:2 Explanation: In this array, the earliest contiguous substring is [2,3]. Example 2 : Input:arr=[3,5,7,10,2] ,k=12 Output:2 Explanation: In this array, the earliest consecutive concatenated substrings with a sum of 12 are [5,...

1198. Most Frequent Subtree Sum

Code ( Language :C++) 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 * @return: all the values with the highest frequency in any order */ unordered_map < int , int > mp; vector < int > findFrequentTreeSum(TreeNode * root) { // Write your code here vector < int > res; if (root == NULL ){ return res; } findSum(root); int maxV = INT_MIN; for ( auto i : mp){ maxV = max(maxV, i.second); } for ( auto i: mp){ if (i.second == maxV){ res.push_back(i.first); } } return res; } int findSum (TreeNode *root) { if...

1037. Global and Local Inversions

Code ( Language :C++) Edit class Solution { public : /** * @param A: an array * @return: is the number of global inversions is equal to the number of local inversions */ bool isIdealPermutation ( vector < int > &A) { // Write your code here int maxV = A[ 0 ]; for ( int i = 2 ; i < A.size(); i++){ if (A[i] < maxV){ return false ; } maxV = max(maxV, A[i - 2 ]); } return true ; } };

878. Boundary of Binary Tree

class Solution { public : /** * @param root: a TreeNode * @return: a list of integer */ vector < int > boundaryOfBinaryTree(TreeNode * root) { // write your code here vector < int > res; if (root == NULL ){ return res; } vector < int > left; //findLeftBoundary(root->left, left); TreeNode *copy = root->left; //这种方法是左右两边边界最后一个会没有取到,最后连起来时,leaves已经包括了,不需要再去重复了。 //这个题开始没有搞出来的原因是,处理left,right时没有从left和right node开始,而是从root开始的,没有分治啊,一定严格分治思想!!!!!! if (copy != NULL ){ while (!(copy->left == NULL && copy->right == NULL )){ left.push_back(copy->val); if (copy->left != NULL ){ copy = copy->left; } else { copy = copy->right; } } } vector < int > right; //findRightBoundary(root->right, right...