Tuesday, September 27, 2016

Android Studio Project: Experiment Manager

Experiment Manager, focusing on biological research.

This application makes experiments arrangement as its main function, supplemented by columns such as biological information and data searching. What’s more, it can save your experiments experiences. With this application, you will be able to do every experiment more methodically. When not doing an experiment, you can either know about biological related news or seeking for the latest information from professional websites. How flexible and convenient!

Experiment Manager


Click To Download On Amazon Appstore

Project site

Thursday, September 22, 2016

Android Studio Project: Outside News Updated

The new version of Outside News repairs a bug that the application couldn't exit from MainActivity directly.

Installation package

Project site

Thursday, September 8, 2016

LeetCode Online Judge-10. Regular Expression Matching

Implement regular expression matching with support for '.' and '*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

Tell you a secret. I initially added only 1 line and it was accepted!
Well, that's "return s.matches(p);" in Java. Ha-ha!

OK, I did know this answer was not expected for training.
So here comes my recursive program.

//Java Program: Regular Expression Matching
public class Solution {
    public boolean isMatch(String s, String p) {

if (p.length()==0) {
if (s.length()==0) {
return true;
} else {
return false;
}
}

if (p.length()==1) {
if (s.length()==1&&(p.charAt(0)=='.'||s.charAt(0)==p.charAt(0))) {
return true;
}
return false;
}

if (s.length()>0&&p.length()>0&&(p.charAt(p.length()-1)=='.'||s.charAt(s.length()-1)==p.charAt(p.length()-1))) {
return isMatch(s.substring(0, s.length()-1), p.substring(0, p.length()-1));
}

if (p.length()>1&&p.charAt(p.length()-1)=='*') {
if (s.length()==0) {
return isMatch(s, p.substring(0, p.length()-2));
}
if (p.charAt(p.length()-2)!='.'&&s.charAt(s.length()-1)!=p.charAt(p.length()-2)) {
return isMatch(s, p.substring(0, p.length()-2));
}
if (isMatch(s, p.substring(0, p.length()-2))) {
return true;
}
if (isMatch(s.substring(0, s.length()-1), p.substring(0, p.length()-2))) {
return true;
}
return isMatch(s.substring(0, s.length()-1), p);
}

return false;

    }
}

Friday, September 2, 2016

LeetCode Online Judge-9. Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
Acturally, I used extra space when solving this problem. But this answer was accepted. And I went to see the discussions about this problem. It seems impossible to solve the problem without extra space.
However, it's quite easy to solve this problem without the limit of extra space. Maybe there will be a perfect answer without extra space. Why not think about it?
OK, here comes my solution with extra space.

//Java Program: Palindrome Number
public class Solution {
    public boolean isPalindrome(int x) {
        String s = ((Integer)x).toString();
        for(int i=0;i<s.length()/2;++i) {
         if (s.charAt(i)!=s.charAt(s.length()-1-i)) {
         return false;
         }
        }
        return true;
    }
}



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);
    }
}