Java for LeetCode 067 Add Binary

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

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

解题思路:

JAVA实现如下:

static public String addBinary(String a, String b) {
		if (a.length() < b.length()) {
			String temp = a;
			a = b;
			b = temp;
		}
		boolean carry = false;
		StringBuilder sb = new StringBuilder(a);
		for (int i = 0; i < b.length(); i++) {
			if (b.charAt(b.length() - 1 - i) == ‘0‘) {
				if (sb.charAt(a.length() - 1 - i) == ‘0‘ && carry) {
					sb.replace(a.length() - 1 - i, a.length() - i, "1");
					carry = false;
				} else if (sb.charAt(a.length() - 1 - i) == ‘1‘ && carry)
					sb.replace(a.length() - 1 - i, a.length() - i, "0");
			} else {
				if (sb.charAt(a.length() - 1 - i) == ‘0‘ && !carry)
					sb.replace(a.length() - 1 - i, a.length() - i, "1");
				else if (sb.charAt(a.length() - 1 - i) == ‘1‘ && !carry) {
					sb.replace(a.length() - 1 - i, a.length() - i, "0");
					carry = true;
				}
			}
		}
		if (!carry)
			return sb.toString();
		for (int i = a.length() - b.length() - 1; i >= 0; i--)
			if (sb.charAt(i) == ‘0‘) {
				sb.replace(i, i + 1, "1");
				return sb.toString();
			} else
				sb.replace(i, i + 1, "0");
		sb.insert(0, ‘1‘);
		return sb.toString();
	}
时间: 2024-10-13 17:45:55

Java for LeetCode 067 Add Binary的相关文章

Java for LeetCode 095 Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 解题思路: 参考Java for LeetCode 096 Unique Binary Search Trees思路,本题很容易解决.注意,

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

Leetcode 数 Add Binary

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Add Binary Total Accepted: 9509 Total Submissions: 37699 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题意:

Java for LeetCode 098 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

[LeetCode][JavaScript]Add Binary

https://leetcode.com/problems/add-binary/ Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 长整型二进制加法. 1 /** 2 * @param {string} a 3 * @param {string} b 4 *

leetCode 67. Add Binary 字符串

67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 1.将两个字符串按数组相加得到新数组. 2.将新数组转换成结果. 代码如下: class Solution { public:     string addBinary(string a, str

Java for LeetCode 211 Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word)bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

Java for LeetCode 099 Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. 解题思路: 先中序遍历找到mistake,然后替换即可,JAVA实现如下: public void recoverTree(TreeNode root) { List<Integer> list = inorderTraversal(root); int left

Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 解题思路一: preorder[0]为root,以此分别划分出inorderLeft.preorderLeft.inorderRight.preorderRight四个数组,然后root.left=buildTree(pre