LeetCode Weekly Contest

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

A.Longest Harmonious Subsequence

思路:hash加查找

 1 class Solution {
 2 public:
 3     int findLHS(vector<int>& nums) {
 4         if (nums.empty())
 5             return 0;
 6             
 7         int res = 0;
 8         int len = nums.size();
 9         unordered_map<int, int> hash;
10         for (int i = 0; i < len; i++)
11             hash[nums[i]]++;
12         for (auto it: hash)
13         {
14             if (hash.count(it.first + 1))
15                 res = max(res, it.second + hash[it.first + 1]);
16         }
17         
18         return res;
19     }
20 };
View Code:

B.Valid Square

思路:怎么判断是正方形呢(输入四个点无序)

只要四边边长均相等然后两个对角线长为边长的根号2倍即可

(注意距离大于0不然就是同一个点了,WA了一次)

 1 class Solution {
 2 public:
 3     bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
 4         vector<long long> dis;
 5         long long d;
 6         d = (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0]);
 7         dis.push_back(d);
 8         d = (p3[1] - p1[1]) * (p3[1] - p1[1]) + (p3[0] - p1[0]) * (p3[0] - p1[0]);
 9         dis.push_back(d);
10         d = (p2[1] - p4[1]) * (p2[1] - p4[1]) + (p2[0] - p4[0]) * (p2[0] - p4[0]);
11         dis.push_back(d);
12         d = (p4[1] - p3[1]) * (p4[1] - p3[1]) + (p4[0] - p3[0]) * (p4[0] - p3[0]);
13         dis.push_back(d);
14         d = (p4[1] - p1[1]) * (p4[1] - p1[1]) + (p4[0] - p1[0]) * (p4[0] - p1[0]);
15         dis.push_back(d);
16         d = (p2[1] - p3[1]) * (p2[1] - p3[1]) + (p2[0] - p3[0]) * (p2[0] - p3[0]);
17         dis.push_back(d);
18         sort(dis.begin(), dis.end());
19         
20         if (dis[0] && dis[0] == dis[1] && dis[1] == dis[2] && dis[2] == dis[3] && dis[4] == 2 * dis[3] && dis[5] == 2 * dis[3])
21             return true;
22         else
23             return false;
24     }
25 };
View Code:

C.Fraction Addition and Subtraction

思路:分数加减法,先单独算出每个分数的值,用c/d表示,然后结果用a/b表示,注意正负号,初始化a = 0, b = 1

(注意c,d数字可能大于9,所以要用while进行判断,WA了一次,b初始化为1,因为0不能做分母,0与任何数的最大公约数就是这个数,最小公倍数 = x*y/最大公约数)

 1 class Solution {
 2 public:
 3     long long gcd(long long x, long long y)
 4     {
 5         return y == 0 ? x : gcd(y, x % y); 
 6     }
 7     string fractionAddition(string expression) {
 8         long long p, q;
 9         long long flag = 1;
10         long long a = 0, b = 1, c = 0, d = 0;
11         int len = expression.size();
12         string res;
13 
14         for (int i = 0; i < len; i++)
15         {
16             if (expression[i] == '-')
17             {
18                     flag = -1;
19             }
20             else if (expression[i] == '+')
21             {
22                     flag = 1;
23             }
24             else if ( '0' <= expression[i] && expression[i] <= '9')
25             {
26                 while ('0' <= expression[i] && expression[i] <= '9')
27                     c = c * 10 + (expression[i++] - '0');
28                 c *= flag;
29                 i--;
30             }
31             else if (expression[i] == '/')
32             {
33                 i++;
34                 while ('0' <= expression[i] && expression[i] <= '9')
35                     d = d * 10 + (expression[i++] - '0');
36                 i--;
37 
38                 q = gcd(b, d);
39                 p = b * d / q;
40                 a = a * d / q;
41                 c = c * b / q;
42                 
43                 a = a + c;
44                 q = gcd(abs(a), p);
45                 a = a / q;
46                 b = p / q;
47                 c = d = 0;
48             }
49             else
50                 ;
51         }
52         
53         res += to_string(a) + "/" + to_string(b);
54         
55         return res;
56     }
57 };
View Code:

 (总结:思路比较清晰,就是写起来还得注意细节方面,WA了好几次,特例特判还是得多做题啊,加油!)

 

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

1.Range Addition II

思路:直接算出最小的那个区间相乘就是结果

(注意判断为空的情况)

 1 class Solution {
 2 public:
 3     int maxCount(int m, int n, vector<vector<int>>& ops) {
 4         if (ops.empty())
 5             return m * n;
 6             
 7         int a, b;
 8         a = b = INT_MAX;
 9         for (int i = 0; i < ops.size(); i++)
10         {
11             a = min(a, ops[i][0]);
12             b = min(b, ops[i][1]);
13         }
14         return a * b;
15     }
16 };
View Code:

下边这个写法比较巧妙:

 1 class Solution {
 2 public:
 3     int maxCount(int m, int n, vector<vector<int>>& ops) {
 4         for (auto op: ops)
 5         {
 6             m = min(m, op[0]);
 7             n = min(n, op[1]);
 8         }
 9         return m * n;
10     }
11 };
View Code:

2.Minimum Index Sum of Two Lists

思路:hash下第一个列表,然后再第二个列表中查找相同的字符串,并且计算下标的和,如果小,则清空数组压进去此时的字符串,如果相等则直接压入,如果大于不处理

 1 class Solution {
 2 public:
 3     vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
 4         unordered_map<string, int> hash;
 5         vector<string> res;
 6         int sum, num = INT_MAX;
 7         
 8         if (list1.empty() || list2.empty())
 9             return res;
10         
11         for (int i = 0; i < list1.size(); i++)
12             hash[list1[i]] = i;
13         
14         for (int j = 0; j < list2.size(); j++)
15         {
16             if (hash.count(list2[j]))
17             {
18                 sum = hash[list2[j]] + j;
19                 if (num == INT_MAX)
20                 {
21                     num = sum;
22                     res.push_back(list2[j]);
23                 }
24                 else if (sum == num)
25                     res.push_back(list2[j]);
26                 else if (sum < num)
27                 {
28                     num = sum;
29                     res.clear();
30                     res.push_back(list2[j]);
31                 }
32                 else
33                     ;
34             }
35         }
36         return res;
37     }
38 };
View Code:

稍简洁版本:

 1 class Solution {
 2 public:
 3     vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
 4         unordered_map<string, int> hash;
 5         vector<string> res;
 6         int sum;
 7         int num = INT_MAX;
 8         
 9         if (list1.empty() || list2.empty())
10             return res;
11         
12         for (int i = 0; i < list1.size(); i++)
13             hash[list1[i]] = i;
14         
15         for (int j = 0; j < list2.size(); j++)
16         {
17             if (hash.count(list2[j]))
18             {
19                 sum = hash[list2[j]] + j;
20                 if (sum < num)
21                 {
22                     num = sum;
23                     res.clear();
24                     res.push_back(list2[j]);
25                 }
26                 else if (sum == num)
27                     res.push_back(list2[j]);
28                 else
29                     ;
30             }
31         }
32         return res;
33     }
34 };
View Code:

3.Array Nesting

思路:这个是hihocoder做过的一道题,直接每个进行判断,判断完了之后去找值对应的id

 1 class Solution {
 2 public:
 3     int arrayNesting(vector<int>& nums) {
 4         int n = nums.size();
 5         int count = 0;
 6         int res = 0;
 7         vector<int> vis;
 8         
 9         vis.resize(n, 0);
10         int i = 0;
11         while (i < n)
12         {
13             if (vis[i])
14             {
15                 res = max(res, count);
16                 count = 0;
17                 i++;
18             }
19             else
20             {
21                 vis[i] = 1;
22                 count++;
23                 i = nums[i];
24             }
25         }
26         return res;
27     }
28 };
View Code:

4.Non-negative Integers without Consecutive Ones

思路:不连续的1,用dp描述就是:dp[i][0] = dp[i - 1][0] + dp[i - 1][1]; dp[i][1] = dp[i - 1][0];然后再处理每一位的数字即可

 1 class Solution {
 2 public:
 3     int dp[32][2];
 4     void init()
 5     {
 6         dp[0][0] = dp[0][1] = 1;
 7         for (int i = 1; i <= 31; i++)
 8         {
 9             dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
10             dp[i][1] = dp[i - 1][0];
11         }
12     }
13     int dfs(int len, int num)
14     {
15         if (len <= 0)
16             return 1;
17         
18         int val = 1 << (len - 1);
19         if (num >= val)
20             return dp[len - 1][0] + dfs(len - 2, num - val);
21         else
22             return dfs(len - 1, num);
23         
24     }
25     int findIntegers(int num) {
26         init();
27         int len = 0;
28         int n = num;
29         
30         while (n)
31         {
32             len++;
33             n >>= 1;
34         }
35         
36         return dfs(len, num);
37     }
38 };
View Code:

 

posted on 2017-05-21 12:20  dxy1993  阅读(177)  评论(0编辑  收藏  举报