Monday, March 17, 2014

LeetCode - Evaluate Reverse Polish Notation

Evaluate Reverse Polish Notation


Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +-*/. Each operand may be an integer or another expression.
Some examples:
  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
Solution:
public class Solution {
    public int evalRPN(String[] tokens) {
        int res = 0;
        if(tokens.length == 0) return res;
        
        // build a stack for this kind of problem
        String operators = "+-*/";
        Stack<string> stack = new Stack<string>();
        
        for(String s : tokens) {
            // if s is not an valid operator, then s is a num, push in stack
            if(!operators.contains(s)) {
                stack.push(s);
            } 
            // if s is an valid operator, pop two nums, calculate, push back
            else {
                int a = Integer.valueOf(stack.pop());
                int b = Integer.valueOf(stack.pop());
                
                // caculate
                switch(s) {
                    case "+" : stack.push(String.valueOf(a + b)); break;
                    case "-" : stack.push(String.valueOf(b - a)); break;
                    case "*" : stack.push(String.valueOf(a * b)); break;
                    case "/" : stack.push(String.valueOf(b / a)); break;
                }
            }
        }
        
        // after loop, pop the last num as res
        res = Integer.valueOf(stack.pop());
        
        return res;
    }
}
Thanks to: http://www.programcreek.com/2012/12/leetcode-evaluate-reverse-polish-notation/
==========================
The hardest part is not about solving the problem, but to keep learning and last forever... 

Sunday, March 9, 2014

Leetcode - Reverse Words in a String

Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Clarification:
  • What constitutes a word?
    A sequence of non-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?
    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?
    Reduce them to a single space in the reversed string.

Solution:

public class Solution {
    public String reverseWords(String s) {
        if(s.isEmpty() || s.length() == 0)   return s;
        
        StringBuffer res = new StringBuffer();
        
        int t, h;
        for(int i = s.length() - 1; i >= 0; i--) {
            while(i >= 0 && s.charAt(i) == ' ') i--;
            
            // set tail pointer
            if(i < 0) break;
            t = i;
            h = t;
            
            // set head pointer
            while(i >= 0 && s.charAt(i) != ' ') { h = i; i--; }
            
            // append this word (append a space if find more than two words)
            if(h <= t && res.length() > 0) res.append(' ');
            for(int j = h; j <= t; j++) {
                res.append(s.charAt(j));
            }
        }
        
        return res.toString();
    }
}


-------------------------------------
OK, finally i'm back! second round, go~