LeetCode Weekly Contest 51
2017-09-24 22:48
357 查看
682. Baseball Game
题目描述
You’re now a baseball game point recorder.Given a list of strings, each string can be one of the 4 following types:
Integer (one round’s score): Directly represents the number of points you get in this round.
“+” (one round’s score): Represents that the points you get in this round are the sum of the last two valid round’s points.
“D” (one round’s score): Represents that the points you get in this round are the doubled data of the last valid round’s points.
“C” (an operation, which isn’t a round’s score): Represents the last valid round’s points you get were invalid and should be removed.
Each round’s operation is permanent and could have an impact on the round before and the round after.
You need to return the sum of the points you could get in all the rounds.
简单思路
根据题意依次遍历输入,注意sum值和point值的变化。代码
class Solution { public: int calPoints(vector<string>& ops) { vector<int> validnum; int sum = 0; for(string str : ops) { if ((str[0] >= '0' && str[0] <= '9') || str[0] == '-') { int tmp = atoi(str.c_str()); sum += tmp; validnum.push_back(tmp); } else if (str[0] == '+') { int len = validnum.size(); int tmp = validnum[len-1] + validnum[len-2]; validnum.push_back(tmp); sum += tmp; } else if (str[0] == 'C') { int tmp = validnum[validnum.size()-1]; validnum.pop_back(); sum -= tmp; } else if (str[0] == 'D') { int tmp = validnum[validnum.size()-1]; sum += tmp*2; validnum.push_back(tmp*2); } //cout<<sum<<endl; } return sum; } };
681. Next Closest Time
题目描述
Given a time represented in the format “HH:MM”, form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.You may assume the given input string is always valid. For example, “01:34”, “12:09” are all valid. “1:34”, “12:9” are all invalid.
简单思路
用四个数字组成10个两位数,依次遍历这些数,判断与当前的小时和分钟的大小关系,找到合法的输出即可。注意数字小于10时的输出问题。代码
class Solution { public: int validm = -1, validh = -1, minminute = 60, minhour = 24; void valid(int *num, int hour, int minute) { for (int i = 0; i < 4; i ++) { for (int j = 0; j < 4; j ++) { int tmp = num[i] * 10 + num[j]; if (tmp < 60) { if (tmp > minute) { if (validm == -1 || validm > tmp) validm = tmp; } if (minminute > tmp) minminute = tmp; } if (tmp < 24) { if (minhour > tmp) minhour = tmp; if (validh == hour && tmp > hour) validh = tmp; else if (tmp > hour && validh > tmp) validh = tmp; } } } } string nextClosestTime(string time) { int hour = (time[0]-'0') * 10 + (time[1]-'0'); validh = hour; int minu = (time[3]-'0') * 10 + (time[4]-'0'); int num[] = {time[0]-'0', time[1]-'0', time[3]-'0', time[4]-'0'}; valid(num, hour, minu); char re[5]; int reh, rem; if (validm != -1) { reh = hour; rem = validm; } else if (validh != hour) { reh = validh; rem = minminute; } else { reh = minhour; rem = minminute; } if (reh < 10 && rem < 10) { sprintf(re, "0%d:0%d", reh, rem); } else if (reh < 10) { sprintf(re, "0%d:%d", reh, rem); } else if (rem < 10) { sprintf(re, "%d:0%d", reh, rem); } else { sprintf(re, "%d:%d", reh, rem); } return re; } };
684. Redundant Connection
题目描述
We are given a “tree” in the form of a 2D-array, with distinct values for each node.In the given 2D-array, each element pair [u, v] represents that v is a child of u in the tree.
We can remove exactly one redundant pair in this “tree” to make the result a tree.
You need to find and output such a pair. If there are multiple answers for this question, output the one appearing last in the 2D-array. There is always at least one answer.
简单思路
并查集算法,当join时判断祖先是否一致,如果一致,输出当前数组即为所求。并查集代码
class Solution { public: int pre[1001] = {0}; int find(int x) { if (pre[x] == 0) { pre[x] = x; return x; } int r = x; while( pre[r] != r ) { r = pre[r]; } int i = x, j; while (i != r) { j = pre[i]; pre[i] = r; i = j; } return r; } bool join(int x, int y) { int fx = find(x), fy = find(y); if (fx != fy) pre[fx] = fy; else { return false; } return true; } vector<int> findRedundantConnection(vector<vector<int>>& edges) { for (vector<int> tmp : edges) { if(join(tmp[1], tmp[0])) continue; else return tmp; } } };
683. K Empty Slots
题目描述
There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one in N days. In each day, there will be exactly one flower blooming and it will be in the status of blooming since then.Given an array flowers consists of number from 1 to N. Each number in the array represents the place where the flower will open in that day.
For example, flowers[i] = x means that the unique flower that blooms at day i will be at position x, where i and x will be in the range from 1 to N.
Also given an integer k, you need to output in which day there exists two flowers in the status of blooming, and also the number of flowers between them is k and these flowers are not blooming.
If there isn’t such day, output -1.
简单思路
参考leetcode讨论区思路683. K Empty Slots代码
public int kEmptySlots(int[] flowers, int k) { TreeSet<Integer> treeSet = new TreeSet<>(); for (int i = 0; i < flowers.length; i++) { int current = flowers[i]; Integer next = treeSet.higher(current); if (next != null && next - current == k + 1) { return i + 1; } Integer pre = treeSet.lower(current); if (pre != null && current - pre == k + 1) { return i + 1; } treeSet.add(current); } return -1; }
相关文章推荐
- LeetCode Weekly Contest 51
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
- LeetCode Weekly Contest 6
- LeetCode Weekly Contest 25 之 537.Complex Number Multiplication
- LeetCode Weekly Contest 32
- leetcode weekly contest 61 ( 740. Delete and Earn )
- LeetCode Weekly Contest 9 第九周周赛
- LeetCode Weekly Contest 31解题思路
- LeetCode Weekly Contest 27解题思路
- LeetCode Weekly Contest 40(1) - Average of Levels in Binary Tree
- LeetCode Weekly Contest 23 之 539. Minimum Time Difference
- 73th LeetCode Weekly Contest Domino and Tromino Tiling
- LeetCode Weekly Contest 36解题思路
- (Trie)LeetCode Weekly Contest 42 Q4 648. Replace Words
- LeetCode Weekly Contest 47
- LeetCode Weekly Contest 56 Find K-th Smallest Pair Distance
- LeetCode Weekly Contest 27
- LeetCode Weekly Contest 32 解题报告
- LeetCode Weekly Contest 40(2) - Solve the Equation
- LeetCode Weekly Contest 74 793. Preimage Size of Factorial Zeroes Function【二分】