#leetCode brush title documentary Day4

https://leetcode-cn.com/problems/baseball-game/solution/bang-qiu-bi-sai-by-leetcode/

You are now a baseball game scorer.
Given a list of strings, each string can be one of four types:
1. Integer (a score): Direct represents the number of points you get in this round.
2. "+" (a score): Indicates the current round score obtained is valid round score of the first two rounds combined.
3. "D" (a score): Indicates the current round score obtained is valid round bout before scoring twice.
4. "C" (an operation, this is not a round scores): you get a valid final round score is invalid and should be removed.

Each round of operations are permanent, may have an impact after the previous round and round.
You need to return your score in all rounds combined.

Example 1:

Input: [ "5", "2 ", "C", "D", "+"]
Output: 30
Explanation:
Round 1: You can get 5 points. Is the sum of: 5.
Round 2: You can get 2 points. Is the sum of: 7.
Operation 1: The first two invalid data. Is the sum of: 5.
Round 3: You can get 10 points (2nd round of data has been deleted). The total number is: 15.
Round 4: You can get 5 + 10 = 15 points. The total number is: 30.
Example 2:

Input: [ "5", "- 2", "4", "C", "D", "9", "+", "+"]
Output: 27
Explanation:
Round 1: You can get 5 minutes . Is the sum of: 5.
Round 2: You can get -2 points. The total number is: 3.
Round 3: You can get 4 points. Is the sum of: 7.
Operation 1: The first three of the data is invalid. The total number is: 3.
Round 4: You can get and -4 (third round of data has been deleted). Total: -1.
Round 5: You can get 10 points. The total number is: 8.
Round 6: You can get -4 + 9 = 5 minutes. The total number is 13.
Round 7: you can get 10 + 5 = 14 points. The total number is 27.

 

Independent thinking chicken dishes

The subject is not difficult, carefully read the description of the sample and casual working to solve difficult to think of a stack, pop the top element and the push to evaluate and calculate the nearest value.

 1 class Solution {
 2 public:
 3   int calPoints(vector<string>& ops) {
 4     stack<string> result;
 5     int totalScore = 0;
 6     for (int i = 0; i < ops.size(); i ++) {
 7         if (ops[i] == "C" && result.empty()) {
 8             continue;
 9         } else if (ops[i] == "C " ) {
 10              COUT << " pop element " << result.top () << endl;
 . 11              result.pop ();
 12 is          } the else  IF (OPS [I] == " D " ) {
 13 is              int Score = atoi (result.top () the c_str ().) * 2 ;
 14              COUT << " add a double element " << Score << endl;
 15              result.push (the to_string (Score));
 16          } the else  IF ( OPS [I] == "+") {
17             int a = atoi(result.top().c_str());
18             result.pop();
19             int b = atoi(result.top().c_str());
20             result.pop();
21             int sum = a + b;
22             cout << "计算得到的和是" << sum << endl;
23             result.push(to_string(b));
24             result.push(to_string(a));
25             result.push(to_string(sum));
26         } else {
27             cout << "推入元素" << ops[i] << endl;
28             result.push(ops[i]);
29         }
30     }
31     while (!result.empty()) {
32         totalScore = totalScore + atoi(result.top().c_str());
33         result.pop();
34     }
35     return totalScore;
36   }
37 };

The only little trouble is given by a string, or the need to convert the calculated values ​​and string values, a little bit of trouble.

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/baseball-game
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/xyy999/p/11774401.html