Created
April 2, 2014 02:07
-
-
Save rayjcwu/9926810 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ZipInt { | |
| public static void main(String[] argv) { | |
| ZipInt zi = new ZipInt(); | |
| System.out.println(zi.solution(12, 56)); | |
| System.out.println(zi.solution(56, 12)); | |
| System.out.println(zi.solution(12345, 678)); | |
| System.out.println(zi.solution(123, 67890)); | |
| System.out.println(zi.solution(10, 100)); | |
| System.out.println(zi.solution(0, 0)); | |
| System.out.println(zi.solution(2, 0)); | |
| System.out.println(zi.solution(0, 2)); | |
| System.out.println(zi.solution(100000000, 100000000)); | |
| } | |
| public int solution(int A, int B) { | |
| long result = 0; | |
| int baseA = base(A); | |
| int baseB = base(B); | |
| while (baseA > 0 && baseB > 0) { | |
| result = result * 100 + (A/baseA) * 10 + (B/baseB); | |
| A %= baseA; | |
| B %= baseB; | |
| baseA /= 10; | |
| baseB /= 10; | |
| } | |
| while (baseA > 0) { | |
| result = result * 10 + A/baseA; | |
| A %= baseA; | |
| baseA /= 10; | |
| } | |
| while (baseB > 0) { | |
| result = result * 10 + B/baseB; | |
| B %= baseB; | |
| baseB /= 10; | |
| } | |
| return (result > 100000000) ? -1: (int)result; | |
| } | |
| public int base(int x) { | |
| int b = 1; | |
| while (10 * b <= x) { | |
| b *= 10; | |
| } | |
| return b; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment