【Leetcode周赛】从contest1开始。(一般是10个contest写一篇文章)

注意,以前的比赛我是自己开了 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了。

 1 //转成字符串排序会tle
 2 class Solution {
 3 public:
 4     vector<int> lexicalOrder(int n) {
 5         vector<int> ans;
 6         if (n <= 0) {
 7             return ans;
 8         }
 9         dfs(n, 0, ans);
10         return ans;
11     }
12     void dfs(int n, int temp, vector<int>& ans) {
13         for (int cur = 0; cur <= 9; ++cur) {
14             if (temp == 0 && cur == 0) { continue; }
15             temp = temp * 10 + cur;
16             if (temp <= n) {
17                 if (temp != 0) {ans.push_back(temp);}
18                 dfs(n, temp, ans);
19                 temp = (temp - cur) / 10; //backtracking 这个要调。
20             } else {
21                 return;
22             }
23         }
24     }
25 };
View Code

【387】First Unique Character in a String

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

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

 1 class Solution {
 2 public:
 3     int firstUniqChar(string s) {
 4         const int n = s.size();
 5         if (n == 0) {return -1;}
 6         map<char, vector<int>> mp;
 7         for (int i = 0; i < n; ++i) {
 8             mp[s[i]].push_back(i);
 9         }
10         int ans = n;
11         for (auto ele : mp) {
12             if (ele.second.size() == 1) {
13                 ans = min(ans, ele.second.front());
14             }
15         }
16         return ans == n ? -1 : ans;
17     }
18 };
View Code

【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

原文地址:https://www.cnblogs.com/zhangwanying/p/9831975.html