Problem Statement
Certain calculators designed for long strings of operations have a special feature. The first time you press the '=' key the current value is outputted. Each additional time you press the '=' key, it will repeat the last addition operation and then output the current value. For example, if the commands were:
cmds = { "+3","+7","=","=","=" }
then, assuming the intial value is 0, the calculator would output 10, 17, and 24 successively after each of the three equals. Note that this feature only applies to contiguous sequences of 2 or more '=' presses (for further clarification see the examples). Each element of cmds will either have the form (quotes for clarity) "+x" or "=" where x is a positive integer. You will return the last outputted value. The initial value will always be 0.Definition
- Class:
- CalculatorRepeat
- Method:
- getTotal
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int getTotal(String[] cmds)
- (be sure your method is public)
Constraints
- cmds will contain between 2 and 50 elements inclusive.
- Each element of cmds will either be (quotes for clarity) "=" or "+x". Here x is a positive integer between 1 and 1000 inclusive with no leading zeros.
- The first element of cmds will have the form (quotes for clarity) "+x".
- The last element of cmds will have the form (quotes for clarity) "=".
Examples
{ "+3","+7","=","=","=" }Returns: 24
The example in the problem statement.{ "+3","+7","=","=","=","+1","+2","=","=" }Returns: 29
Continuing on the previous example, the following values are output in sequence: 10, 17, 24, 27, 29{"+1","+5","=","+2","+3","="}Returns: 11
The values output will be 6 then 11.{"+1","+2","+3","+4","+5","+6","+7","+8","+9","+10","="}Returns: 55
{"+1","=","+2","=","+3","=","+4","=","+5","=", "+6","=","+7","=","+8","=","+9","=","+10","="}Returns: 55
{"+1","=","+2","=","+3","=","+4","=","+5","=", "+6","=","+7","=","+8","=","+9","=","+10","=", "+1000","=","=","=","=","=","=","=","=","="}Returns: 9055