码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode -- Day13

时间:2015-07-20 23:10:01      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

Question 1

Add Binary

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

Firslty I wirte a very direct and kind of brute forch method. Just consider each case of sum 1, 0, 2, 3 for aNum + bNum + carry by switch case.

 1 public class Solution {
 2     
 3     String result = "";
 4     int carry = 0;
 5     
 6     public String addBinary(String a, String b) {
 7         if (a.length() == 0 )
 8             return b;
 9         else if (b.length() == 0)
10             return a;
11         
12         
13         int alen = a.length()-1;
14         int blen = b.length()-1;
15         
16         
17         while (alen >= 0 && blen >=0){
18             int aNum = a.charAt(alen) - ‘0‘;
19             int bNum = b.charAt(blen) - ‘0‘;
20             calculator(aNum + bNum + carry);
21             alen --;
22             blen --;
23         }
24         
25         while (alen >= 0){
26             int aNum = a.charAt(alen) - ‘0‘;
27             calculator(aNum + carry);
28             alen --;
29         }
30         
31         while (blen >= 0){
32             int bNum = b.charAt(blen) - ‘0‘;
33             calculator(bNum + carry);
34             blen --;
35         }
36         
37         if (carry == 1)
38             result = "1" + result;
39         
40         return result;
41     }
42     
43     public void calculator(int sum){
44         switch(sum){
45             case 0:
46                 result = "0" + result;
47                 break;
48             case 1:
49                 result = "1" + result;
50                 carry = 0;
51                 break;
52             case 2:
53                 result = "0" + result;
54                 carry = 1;
55                 break;
56             case 3:
57                 result = "1" + result;
58                 carry = 1;
59                 break;
60         }
61     }
62 }

The other way does not use switch case but use %2 or /2 to defind carry and sum, which is much clear and shorter. 

 1 public String addBinary(String a, String b) {
 2         if (a.length() == 0 )
 3             return b;
 4         else if (b.length() == 0)
 5             return a;
 6             
 7         String result = "";
 8         int carry = 0;
 9         
10         int len = Math.max(a.length(), b.length());
11         
12         for (int i = 0; i < len; i ++){
13             int aNum = 0;
14             int bNum = 0;
15             if (i < a.length())
16                 aNum = a.charAt(a.length() - 1 - i) - ‘0‘;
17             if (i < b.length())
18                 bNum = b.charAt(b.length() - 1 - i) - ‘0‘;
19             
20             int sum = aNum + bNum + carry;
21             result = sum%2 + result;
22             carry = sum / 2;
23         }
24         
25         if (carry == 1)
26             result = "1" + result;
27         
28         return result;
29         
30     }

 

 

Leetcode -- Day13

标签:

原文地址:http://www.cnblogs.com/timoBlog/p/4662843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!