Leetcode#67Add Binary

Total Accepted: 40264 Total Submissions: 161822My Submissions

Question Solution

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) {

int la=a.length();

int lb=b.length();

int jinwei=0;

int ca=la-1;

int cb=lb-1;

String oc="";

while(ca>=0&&cb>=0)

{

int x=a.charAt(ca)-‘0‘;

int y=b.charAt(cb)-‘0‘;

if(x==0)

{

x=x+jinwei;

jinwei=0;

}

else if(y==0)

{

y=y+jinwei;

jinwei=0;

}

else

{

if(jinwei>0)

{

x=0;

y=1;

jinwei=1;

}

}

if(x==0&&y==0)

oc=String.valueOf(0)+oc;

else if(x==0||y==0)

oc=String.valueOf(1)+oc;

else

{

oc=String.valueOf(0)+oc;

jinwei=1;

}

ca--;

cb--;

}

if(ca==-1&&cb!=-1)

{

while(cb>=0)

{

if(jinwei==0)

{

oc=String.valueOf(b.charAt(cb))+oc;

cb--;

}

else

{

if(b.charAt(cb)==‘1‘)

{

oc=String.valueOf(0)+oc;

jinwei=1;

}

else

{

oc=String.valueOf(1)+oc;

jinwei=0;

}

cb--;

}

}

}

else if(ca!=-1&&cb==-1)

{

while(ca>=0)

{

if(jinwei==0)

{

oc=String.valueOf(a.charAt(ca))+oc;

ca--;

}

else

{

if(a.charAt(ca)==‘1‘)

{

oc=String.valueOf(0)+oc;

jinwei=1;

}

else

{

oc=String.valueOf(1)+oc;

jinwei=0;

}

ca--;

}

}

}

if(jinwei==1)

oc=String.valueOf(1)+oc;

return oc;

}

}

时间: 2024-11-05 11:54:20

Leetcode#67Add Binary的相关文章

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode: Validata Binary Search Tree

LeetCode: Validata 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 o

LeetCode: Recover Binary Search Tree

LeetCode: Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space s

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

[Leetcode][Tree][Binary Tree Postorder Traversal]

二叉树的后续遍历 1.递归版本 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void dfsPostorderTraversal(TreeNode *now, vec

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

leetcode day4 -- Binary Tree Postorder(Preorder) Traversal &amp;&amp; Edit Distance

 1.Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 分析:后续遍历

LeetCode:Binary Tree Paths - 获取一棵树从顶点到每个叶节点的路径

1.题目名称 Binary Tree Paths(获取一棵树从顶点到每个叶节点的路径) 2.题目地址 https://leetcode.com/problems/binary-tree-paths/ 3.题目内容 英文:Given a binary tree, return all root-to-leaf paths. 中文:给定一颗二叉树,返回所有的根节点到叶节点的路径 例如:现有一颗二叉树    1  /   2     3    5 所有由根节点到叶节点的路径如下: ["1->2-