You have an array of logs
. Each log is a space delimited string of words.
For each log, the first word in each log is an alphanumeric identifier. Then, either:
We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.
Reorder the logs so that all of the letter-logs come before any digit-log(让所有字母log出现在数字log前边). The letter-logs are ordered lexicographically(字典序) ignoring identifier, with the identifier used in case of ties. The digit-logs should be put in their original order.(字母log按照字典序排序,数字log相对顺序不变)
Return the final order of the logs.
Input: [ "a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo" ]
Output: [ "g1 act car", "a8 act zoo", "ab1 off key dog", "a1 9 2 3 1", "zo4 4 7" ]
// 为了按照字典序排序
bool cmp(string& l, string& r)
{
int lp = l.find(" ");
int rp = r.find(" ");
return l.substr(lp) < r.substr(rp);
}
vector reorderLogFiles(vector& logs)
{
int d = logs.size() - 1, l = d;
while (l >= 0)
{
if (isdigit(logs[d].back()))
{
--d;
l = d - 1;
}
else if (isalpha(logs[l].back()))
--l;
else
{
string tmp = logs[d];
logs[d] = logs[l];
logs[l] = tmp;
}
}
sort(logs.begin(), logs.begin() + d + 1, cmp);
return logs;
}
Given the root
node of a binary search tree, return the sum of values of all nodes with value between L
and R
(inclusive).
The binary search tree is guaranteed to have unique values.
Input: root = [ 10, 5, 15, 3, 7, null, 18 ], L = 7, R = 15 Output: 32
Input: root = [ 10, 5, 15, 3, 7, 13, 18, 1, null, 6 ], L = 6, R = 10 Output: 23
int dfs(TreeNode* nod, const int& L, const int& R)
{
return ((nod->val >= L && nod->val <= R) ? nod->val : 0)
+ (nod->left == NULL ? 0 : dfs(nod->left, L, R))
+ (nod->right == NULL ? 0 : dfs(nod->right, L, R));
}
int rangeSumBST(TreeNode* root, int L, int R) {
if(!root) return 0;
return dfs(root, L, R);
}
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Input: [ [1, 1], [1, 3], [3, 1], [3, 3], [2, 2] ] Output: 4
Input: [ [1, 1], [1, 3], [3, 1], [3, 3], [4, 1], [4, 3] ] Output: 2
解:
用 set
int minAreaRect(vector>& points) {
bool found = false;
int psize = points.size(), min_area = INT_MAX;
set> setp; // unordered_set会报错 "The C++ Standard doesn't provide a hash for this type."
for (auto p : points)
setp.insert(pair(p[0], p[1]));
for (int i = 0; i < psize; i++)
for (int j = i + 1; j < psize; j++)
{
int x1 = points[i][0], y1 = points[i][1], x2 = points[j][0], y2 = points[j][1];
if (x1 == x2 || y1 == y2) continue;
if (abs(x1 - x2) * abs(y1 - y2) >= min_area) continue; // 一定不是最小矩形
if (setp.find(pair(x1, y2)) != setp.end()
&& setp.find(pair(x2, y1)) != setp.end()) // 另两个点存在
{
found = true;
min_area = min(min_area, abs(x1 - x2) * abs(y1 - y2));
}
}
if (found == false) return 0;
return min_area;
}
Given a string S
, count the number of distinct, non-empty subsequences of S
.
Since the result may be large, return the answer modulo 10^9 + 7
.
Input: "abc" Output: 7 Explanation: The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc". Input: "aba" Output: 6 Explanation: The 6 distinct subsequences are "a", "b", "ab", "ba", "aa" and "aba". Input: "aaa" Output: 3 Explanation: The 3 distinct subsequences are "a", "aa" and "aaa".
解:
"abc" 的情况没有 "ca" 也就是必须是原来的顺序。用一个26大小的数组存储以每个字母为结尾的单词的个数,每次遍历到一个字母的时候,比如 'a',那么以 a 为结尾的单词数是整个数组所有数的和 + 1,加1的意思就是 "a" 这个字符串,原来如果有 "a" 也变成了 "aa" 所以没有问题。
int distinctSubseqII(string S) {
long endsWith[26] = {}, mod = 1e9 + 7;
for (char c : S)
endsWith[c - 'a'] = accumulate(begin(endsWith), end(endsWith), (long int)1) % mod;
return accumulate(begin(endsWith), end(endsWith), 0L) % mod;
}