Thursday, September 1, 2016

LeetCode Online Judge-8. String to Integer (atoi)

Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

//Java Program: String to Integer (atoi)
public class Solution {
    public int myAtoi(String str) {
        int i = 0;
while (i<str.length()&&str.charAt(i)==' ') {
++i;
}
if (i==str.length()) {
   return 0;
}
if (!((str.charAt(i)=='-'&&i<str.length()-1&&str.charAt(i+1)>='0'&&str.charAt(i+1)<='9')||(str.charAt(i)=='+'&&i<str.length()-1&&str.charAt(i+1)>='0'&&str.charAt(i+1)<='9')||(str.charAt(i)>='0'&&str.charAt(i)<='9'))) {
   return 0;
}
String s = str.substring(i);
int j;
for(j=1;j<s.length();++j) {
if (s.charAt(j)<'0'||s.charAt(j)>'9') {
break;
}
}
try {
return Integer.parseInt(s.substring(0, j));
} catch (NumberFormatException e) {
   if (str.charAt(i)=='-') {
       return Integer.MIN_VALUE;
   } else {
       return Integer.MAX_VALUE;
   }
}
    }
}

LeetCode Online Judge-7. Reverse Integer

Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321

Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

//Java Program: Reverse Integer
public class Solution {
    public int reverse(int x) {
        int flag = 1, temp = x;
    long r = 0;
    if (x<0) {
    flag = -1;
    temp = -x;
    }
    while (temp>0) {
    r = r*10 + temp%10;
        if (r>Integer.MAX_VALUE) {
        return 0;
        }
    temp /= 10;
    }
return flag*(int)r;
    }
}

LeetCode Online Judge-389. Find the Difference

Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
'e' is the letter that was added.

//Java Program: Find the Difference
public class Solution {
    public char findTheDifference(String s, String t) {
        ArrayList<Character> alC = new ArrayList<Character>();
        for(int i=0;i<s.length();++i) {
            alC.add((Character)s.charAt(i));
        }
        for(int i=0;i<t.length();++i) {
        if (!alC.remove((Character)t.charAt(i))) {
                return t.charAt(i);
            }
        }
        return t.charAt(0);
    }
}