LeetCode 240. Search a 2D Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
For example,

Consider the following matrix:

[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.

Given target = 20, return false.

题目大意:编写一个有效的算法,在m×n矩阵中搜索一个值。 该矩阵具有以下属性:
每行中的整数从左到右依次排列。每列中的整数按从上到下的顺序排列。
分析:对每一行进行binary_serch(),如果能够找到则return true,否则返回false

 

LeetCode 74. Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
For example,

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.

题目大意:编写一个有效的算法,在m×n矩阵中搜索一个值。 该矩阵具有以下属性:
每行中的整数从左到右排序。每行的第一个整数大于前一行的最后一个整数。
分析:对每一行进行二分搜索,如果找到了就返回true否则返回false~

LeetCode 655. Print Binary Tree

Print a binary tree in an m*n 2D string array following these rules:

The row number m should be equal to the height of the given binary tree.
The column number n should always be an odd number.
The root node’s value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don’t need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don’t need to leave space for both of them.
Each unused space should contain an empty string “”.
Print the subtrees following the same rules.
Example 1:
Input:
1
/
2
Output:
[[“”, “1”, “”],
[“2”, “”, “”]]
Example 2:
Input:
1
/ \
2 3
\
4
Output:
[[“”, “”, “”, “1”, “”, “”, “”],
[“”, “2”, “”, “”, “”, “3”, “”],
[“”, “”, “4”, “”, “”, “”, “”]]
Example 3:
Input:
1
/ \
2 5
/
3
/
4
Output:

[[“”, “”, “”, “”, “”, “”, “”, “1”, “”, “”, “”, “”, “”, “”, “”]
[“”, “”, “”, “2”, “”, “”, “”, “”, “”, “”, “”, “5”, “”, “”, “”]
[“”, “3”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”]
[“4”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”]]
Note: The height of binary tree is in the range of [1, 10].

题目大意:按照以下规则在一个m * n的二维字符串数组中打印一个二叉树:
行号m应该等于给定二叉树的高度。
列号n应始终为奇数。
根节点的值(以字符串格式)应该放在第一行的正中间。 根节点所属的行和列将剩余空间分成两部分(左下部分和右下部分)。 您应该在左下角打印左侧子树,并在右下侧打印右侧子树。 左下部分和右下部分应该具有相同的尺寸。 即使一个子树不存在,而另一个子树不存在,您也不需要为无子树打印任何东西,但仍然需要留下与另一子树相同的空间。 但是,如果两个子树都没有,那么你就不需要为它们留下空间。
每个未使用的空间应该包含一个空字符串“”。
按照相同的规则打印子树。
分析:首先计算出树的高度h,然后建立h行、(2的h次方-1)列的字符串数组,进行先序遍历,设立l和r表示当前填充的子树对应的字符串数组的左右下标区域,h表示当前的层数,每次将root->val的值存储入ans[h][mid]中,其中mid=(l+r)/2,然后继续遍历左子树和右子树,将区间分为0~mid-1和mid+1~r,高度h加1,直至遍历完成后返回ans数组~

 

LeetCode 537. Complex Number Multiplication

Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note i2 = -1 according to the definition.

Example 1:
Input: “1+1i”, “1+1i”
Output: “0+2i”
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
Example 2:
Input: “1+-1i”, “1+-1i”
Output: “0+-2i”
Explanation: (1 – i) * (1 – i) = 1 + i2 – 2 * i = -2i, and you need convert it to the form of 0+-2i.
Note:

The input strings will not have extra blank.
The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.

题目大意:给两个复数,求两个数的乘积
分析:使用sscanf和ssprinf,(m+ni)*(p+qi)=(m*p-n*q)*(n*p+m*q) i

 

LeetCode 654. Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

The root is the maximum number in the array.
The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
Construct the maximum tree by the given array and output the root node of this tree.

Example 1:
Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:

6
/ \
3 5
\ /
2 0
\
1
Note:
The size of the given array will be in the range [1,1000].

分析:用递归,l和r分别表示使用在nums[l]到nums[r]区间内的数字进行建树,每次通过idx找到l到r之间的最大值对应的下标idx,使用nums[idx]值new一个新TreeNode结点root,然后将root的左边用l~idx-1建树,右边用idx+1~r建树,然后返回root结点即完成了建树~

 

pair的用法 make_pair

pair<string, int> p1(“123”, 99), p2, p3;

p2.first = “abc”, p2.second = 2;

p3 = make_pair(“dce”, 1);

cout << p1.first << ” “ << p1.second;

pair<string, int> 相当于一个类型名称,如果要创建一个这个类型的数组,可以写vector<pair<string, int>>