[leetcode]Add Binary

不要忘记在最后append进位的1,如果有的话;
一开始用while写的很快,忘记了i++,死循环了;
a+b的情况下,忘记了-'0'两次;
还有是要a.charAt(lenA-i-1),一开始用a.charAt(i)了,脑子一乱忘记了0是最左边的;

public class Solution {

    public String addBinary(String a, String b) {

        int carrior = 0;

		ArrayList<Character> list = new ArrayList<Character>();

        int lenA = a.length();

        int lenB = b.length();

		for (int i = 0; i < lenA || i < lenB; i++)

		{

			if (i >= lenA)

			{

				int r = carrior + b.charAt(lenB-i-1) - '0';

				carrior = r / 2;

				r = r % 2;

				list.add((char)(r + '0'));

			}

			else if (i >= lenB)

			{

				int r = carrior + a.charAt(lenA-i-1) - '0';

				carrior = r / 2;

				r = r % 2;

				list.add((char)(r + '0'));

			}

			else

			{

				int r = carrior + a.charAt(lenA-i-1) + b.charAt(lenB-i-1) - '0' - '0';

				carrior = r / 2;

				r = r % 2;

				list.add((char)(r + '0'));

			}

		}

		if (carrior != 0) list.add((char)(carrior + '0'));

		StringBuffer sb = new StringBuffer();

		for (int j = list.size() - 1; j >= 0; j--)

		{

			sb.append(list.get(j));

		}

		return sb.toString();

    }

}

  

你可能感兴趣的:(LeetCode)