LeetCode Online Judge 题目C# 练习 - Add Binary
2012-06-16 03:11
459 查看
Given two binary strings, return their sum (also a binary string).
For example,
a =
b =
Return
代码分析:
这么简单?我想面试的时候面试官不会让你用这两个微软已经帮你写好的方法吧。
Convert.ToInt32(string value, int fromBase), 这是Convert类里面静态方法ToInt32()的一个overloadding, 参数value是要转换的字符串,fromBase是进制,2 : Binary, 8: Octal, 10: Deciaml, 16: Hexadecimal.
Convert.ToString(int value, int toBase),同上,只不过是由整形转换为二进制字符串.
代码分析:
要注意的几点:
1. 最低位在右边,所有我们从右往左加,从Length - 1到0。
2. 我用了StringBuilder,它使在字符串最前面插入字符更简单,当然我们也可以用一个char[] 数组,在后面加,最后来个reverse,然后转换成string。
3. 我这里碰到非'0' && 非'1'字符,我会抛出一个exception, 但是这里应该更面试官商量,看他想怎么处理。
4. 别忘记了最后一个进位。
For example,
a =
"11"
b =
"1"
Return
"100".
public static string AddBinary(string a, string b) { try { int sum = Convert.ToInt32(a, 2) + Convert.ToInt32(b, 2); return Convert.ToString(sum, 2); } catch (Exception ex) { throw ex; } }
代码分析:
这么简单?我想面试的时候面试官不会让你用这两个微软已经帮你写好的方法吧。
Convert.ToInt32(string value, int fromBase), 这是Convert类里面静态方法ToInt32()的一个overloadding, 参数value是要转换的字符串,fromBase是进制,2 : Binary, 8: Octal, 10: Deciaml, 16: Hexadecimal.
Convert.ToString(int value, int toBase),同上,只不过是由整形转换为二进制字符串.
public static string AddBinaryNoConvertTo(string a, string b) { StringBuilder ret = new StringBuilder(); int aValue, bValue, sum; int carry = 0; for(int i = a.Length - 1, j = b.Length - 1; i > -1 || j > -1; i--, j--) { if (i <= -1) // when i out of range, put 0 in aValue for addition aValue = 0; else { if (a[i] == '1') aValue = 1; else if (a[i] == '0') aValue = 0; else throw new FormatException("Input string has invalid character"); } if (j <= -1) bValue = 0; // when j out of range, put 0 in aValue for addition else { if (b[j] == '1') bValue = 1; else if (b[j] == '0') bValue = 0; else throw new FormatException("Input string has invalid character"); } sum = aValue + bValue + carry; carry = 0; //reset carry if (sum > 1) { carry = 1; //set carry sum -= 2; } ret.Insert(0, sum); } if (carry == 1) // Don't miss the carry at the most significant bit. { ret.Insert(0, '1'); } return ret.ToString(); }
代码分析:
要注意的几点:
1. 最低位在右边,所有我们从右往左加,从Length - 1到0。
2. 我用了StringBuilder,它使在字符串最前面插入字符更简单,当然我们也可以用一个char[] 数组,在后面加,最后来个reverse,然后转换成string。
3. 我这里碰到非'0' && 非'1'字符,我会抛出一个exception, 但是这里应该更面试官商量,看他想怎么处理。
4. 别忘记了最后一个进位。
相关文章推荐
- LeetCode Online Judge 题目C# 练习 - Gray Code
- LeetCode Online Judge 题目C# 练习 - Word Search
- LeetCode Online Judge 题目C# 练习 - Add two number
- LeetCode Online Judge 题目C# 练习 - Binary Tree Inorder Traversal
- LeetCode Online Judge 题目C# 练习 - Implement strStr()
- LeetCode Online Judge 题目C# 练习 - ZigZag Conversion
- LeetCode Online Judge 题目C# 练习 - Combination
- LeetCode Online Judge 题目C# 练习 - Longest Substring Without Repeating Characters
- LeetCode Online Judge 题目C# 练习 - Reverse Nodes in k-Group
- LeetCode Online Judge 题目C# 练习 - 4SUM
- LeetCode Online Judge 题目C# 练习 - N-Queens
- LeetCode Online Judge 题目C# 练习 - Binary Tree Level Order Traversal II
- LeetCode Online Judge 题目C# 练习 - Length of Last Word
- LeetCode Online Judge 题目C# 练习 - Count and Say
- LeetCode Online Judge 题目C# 练习 - Minimum Window Substring
- LeetCode Online Judge 题目C# 练习 - Remove Element
- LeetCode Online Judge 题目C# 练习 - Two Sum
- LeetCode Online Judge 题目C# 练习 - Decode Way
- LeetCode Online Judge 题目C# 练习 - Climbing Stairs
- LeetCode Online Judge 题目C# 练习 - Anagrams