227. Basic Calculator II

Difficulty:  Medium

Frequency:  N/A

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.

You may assume that the given expression is always valid.

Some examples:

"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5

Note: Do not use the eval built-in library function.

My solution:
Data structure:
string
Steps:
Complexity:
Runtime: O(n)
Space: O(1)
Test cases:
Corner cases:
Code:
class Solution {
public:
int calculate(string s) {
istringstream input('+' + s + '+');
int result = 0, curr = 0, next;
char op;
while (input >> op) {
if (op == '+' || op == '-') {
result += curr;
input >> curr;
curr = op == '+' ? curr : -curr;
} else {
input >> next;
if (op == '*') curr *= next;
else curr /=next;
}
}
return result;
}
};

Another solution:
Data structure:
steps:
Complexity:
Runtime:
Space:
Code:

Things to learn:

273. Integer to English Words

Difficulty: Medium

Frequency:

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 – 1.

For example,

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Hint:

1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)

My solution:
Data structure:
string, unordered_map
Steps:
Complexity:
Runtime: O(n)
Space: O(1)
Test cases:
Corner cases:
1. 0
2. 1000010
3. 10
4. 30
5. 100
6. 1000
Code:
class Solution {
public:
string digits = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
string tens = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

string int2string(int n) {
if (n >= 1000000000) {
return int2string(n / 1000000000) + " Billion" + int2string(n % 1000000000);
} else if (n >= 1000000) {
return int2string(n / 1000000) + " Million" + int2string(n % 1000000);
} else if (n >= 1000) {
return int2string(n / 1000) + " Thousand" + int2string(n % 1000);
} else if (n >= 100) {
return int2string(n / 100) + " Hundred" + int2string(n % 100);
} else if (n >= 20) {
return  " " + tens[n / 10] + int2string(n % 10);
} else if (n >= 1) {
return " " + digits[n];
} else {
return "";
}
}

string numberToWords(int num) {
if (num == 0) {
return "Zero";
} else {
string ret = int2string(num);
return ret.substr(1, ret.length() - 1);
}
}
};

Another solution:
Data structure:
steps:
Complexity:
Runtime:
Space:
Code:

Things to learn: