Posts

1367. Police Distance

Description 中文 English Given a matrix size of  n x m , element  1  represents policeman,  -1  represents wall and  0  represents empty. Now please output a matrix size of  n x m , output the minimum distance between each empty space and the nearest policeman Given a matrix size of  n x m ,  n <= 200,m <= 200 . We guarantee that each empty space can be reached by one policeman at least. Have you met this question in a real interview?    Yes Problem Correction Example Given mat = [ [0, -1, 0], [0, 1, 1], [0, 0, 0] ] return  [[2,-1,1],[1,0,0],[2,1,1]] . The distance between the policeman and himself is 0, the shortest distance between the two policemen to other empty space is as shown above Given mat = [ [0, -1, -1], [0, -1, 1], [0, 0, 0] ] return  [[5,-1,-1],[4,-1,0],[3,2,1]] 和walls and gates一样,这样额外做一个状态matrix,把police点定义为0,空闲点定义为inf。 ...

1005. Largest Triangle Area

class Solution { public : /** * @param points: List[List[int]] * @return: return a double */ double largestTriangleArea ( vector < vector < int >> &points) { // write your code here int size = points.size(); if (size < 3 ){ return 0 ; } double res = 0 ; for ( int i = 0 ; i < size - 2 ; i++){ for ( int j = i + 1 ; j < size - 1 ; j++){ for ( int k = i + 2 ; k < size; k++){ res = max(res, getArea(points[i], points[j], points[k])); } } } return res; } double getArea ( vector < int > &pt1, vector < int > &pt2, vector < int > &pt3) { return abs ((pt1[ 0 ] * (pt2[ 1 ] - pt3[ 1 ]) + pt2[ 0 ]*(pt3[ 1 ] - pt1[ 1 ]) + pt3[ 0 ]*(pt1[ 1 ] - pt2[ 1 ])) / 2.0 ); } };

check 1256. Nth Digit

class Solution { public : /** * @param n: a positive integer * @return: the nth digit of the infinite integer sequence */ int findNthDigit ( int n) { // write your code here // 1 - 9 : 个数 9 * 1 // 10-99: 个数 90*2 // 100-999: 个数 900 * 3 //firt find target in how many digts // recover that digit int digitNum = 1 ; long digitRange = 9 ; while (n > digitNum * digitRange){ n -= digitNum * digitRange; digitNum++; digitRange *= 10 ; } int idx = (n - 1 ) % digitNum; int Numidx = (n - 1 ) / digitNum; int targetNum = digitRange / 9 + Numidx; string target = to_string(targetNum); return target[idx] - '0' ; } };

1188. Minimum Absolute Difference in BST

Description 中文 English Given a binary search tree with non-negative values, find the minimum  absolute difference  between values of any two nodes. There are at least two nodes in this BST. Have you met this question in a real interview?    Yes Problem Correction Example Input: 1 \ 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3). int getMinimumDifference (TreeNode * root) { // Write your code here // find the max value among values smaller than root->val; // find the min value among values larger than root->val; int diff = INT_MAX; if (root->left != NULL ){ TreeNode * temp = root->left; while (temp->right != NULL ){ temp = temp->right; } diff = min(diff, abs (root->val - temp->val)); //先...

655. Add Strings

Description 中文 English Given two non-negative integers  num1  and  num2  represented as string, return the sum of  num1  and  num2 . The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero. You must not use any built-in BigInteger library or convert the inputs to integer directly. Have you met this question in a real interview?    Yes Problem Correction Example Example 1: Input : num1 = "123", num2 = "45" Output : 168 class Solution { public : /** * @param num1: a non-negative integers * @param num2: a non-negative integers * @return: return sum of num1 and num2 */ string addStrings ( string &num1, string &num2) { // write your code here int size1 = num1.size(); int size2 = num2.size(); if (size1 == 0 ){ return num2; ...