注意,以前的比赛我是自己开了 virtual contest。这个阶段的目标是加快手速,思考问题的能力和 bug-free 的能力。

前面已经有了100个contest。计划是每周做三个到五个contest。每次计算时间一个半小时。

Warm Up Contest (Contest 1)(2018年10月22日,周一)

链接:https://leetcode.com/contest/warm-up-contest

【386】Lexicographical Numbers

给了一个数字 n,要求返回 1 ~ n的字典顺排序。

For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].

题解:我一开始用了转成字符串然后用字符串比较的方式排序,但是不行,tle。后来直接dfs自己生成,AC了。

 //转成字符串排序会tle
class Solution {
public:
vector<int> lexicalOrder(int n) {
vector<int> ans;
if (n <= ) {
return ans;
}
dfs(n, , ans);
return ans;
}
void dfs(int n, int temp, vector<int>& ans) {
for (int cur = ; cur <= ; ++cur) {
if (temp == && cur == ) { continue; }
temp = temp * + cur;
if (temp <= n) {
if (temp != ) {ans.push_back(temp);}
dfs(n, temp, ans);
temp = (temp - cur) / ; //backtracking 这个要调。
} else {
return;
}
}
}
};

【387】First Unique Character in a String

给了一个字符串,要求返回第一个出现只一次字符的下标,不存在这样的字符返回 -1。

题解:直接用了一个map,能过。

 class Solution {
public:
int firstUniqChar(string s) {
const int n = s.size();
if (n == ) {return -;}
map<char, vector<int>> mp;
for (int i = ; i < n; ++i) {
mp[s[i]].push_back(i);
}
int ans = n;
for (auto ele : mp) {
if (ele.second.size() == ) {
ans = min(ans, ele.second.front());
}
}
return ans == n ? - : ans;
}
};

【388】Longest Absolute File Path

Contest 2(2018年10月23日,周二)

链接:https://leetcode.com/contest/leetcode-weekly-contest-2

【389】Find the Difference

【390】Elimination Game

【391】Perfect Rectangle

05-22 06:38