Stack ---- baseball game

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:

  • 输入: ["5","2","C","D","+"]
  • Output: 30

Explanation:

  • The first one: 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:

  • 输入: ["5","-2","4","C","D","9","+","+"]
  • Output: 27

Explanation:

  • The first one: You can get 5 points. 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.

note:

The input list size between 1 and 1000.
Each integer in the list will be between 30,000 and -30,000.

. 1  class Solution {
 2  public :
 . 3      int calPoints (Vector < String > & OPS) {
 . 4          IF (ops.empty ()) return  0 ;
 . 5          Stack < int > RES; // store each of a score 
. 6          int Result = 0 ; // final score 
. 7          for ( int I = 0 ; I <ops.size (); I ++ ) {
 . 8              // C ++. 11 adds a new vector in .data () usage, returns an array of vector referred a pointer to the first memory element
 . 9              // char * P = ops.data (); pointer to the first element
10              // char * OPS = P [i] .data (); i position the pointer points to, p ++ jumps to the next position 
. 11              const  char * P = OPS [i] .data ();
 12 is              Switch (* P ) {
 13 is                  Case  ' + ' : // score is the sum of the first two rounds effective score
 14                      // if the first is +, because there is no previous points, so that the wheel score of 0;
 15                      // be optimized, because The first is a common sense is not 0, the advantage of this extra step 
16                      IF (res.empty ()) res.push ( 0 );
 . 17                      // if the second ++ is, because only the first round, a small round, before the round before the default is 0, so the score is round the previous round score + 0 = round before scoring. 
18 is                      the else  IF (res.size () == . 1 ) res.push (res.top ());
 . 19                     // This is the situation prior to the existence of two, in fact, if the first two bit redundant
 20                      // front two scores together to give the score of the round, and then push the stack such as 
21 is                      the else {
 22 is                          int TEMP = RES. Top ();
 23 is                          res.pop ();
 24                          int SUMT res.top = () + TEMP;
 25                          res.push (TEMP);
 26 is                          res.push (SUMT);
 27                      }
 28                      Result + = res.top () ;
 29                      Continue ;
 30                  Case  ' D ' : // the score is a front wheel of a valid double score
 31                     // empty, meaning no previous round, 0 direct Push 
32                      IF (res.empty ()) res.push ( 0 );
 33 is                      the else {
 34 is                          int tmp = res.top ();
 35                          res.push (tmp * 2 );
 36                          Result + = res.top ();
 37 [                      }
 38 is                      Continue ;
 39                  Case  ' C ' : // last valid round void fraction, to delete 
40                      IF ! ( res.empty ()) {
 41 is                          Result - = res.top ();
 42 is                         res.pop();
43                     }
44                     continue;
45                 default:
46                     stringstream ss(ops[i]);
47                     int t;
48                     ss >> t;
49                     res.push(t);
50                     result+=res.top();
51             }
52         }
53         return result;
54     }
55 };

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/11032701.html