LeetCode interview Questions:Add binay
1.int之类的数可以使用small
2.直接在字符串使用二进制的加法
http://www.leetcode.com/onlinejudge 需要
class Solution {
public:
int string_to_int(string str) {
int result = 0;
int level = 1;
for(int i = str.length() - 1; i >=0 ; i--) {
if (str[i] == '1') {
result += level;
}
level <<= 1;
}
return result;
}
string int_to_string(int n) {
string result = "";
while(n) {
if(n%2==1) {
result="1" + result;
}
else {
result="0" + result;
}
n>>=1;
}
if(result.length()==0) {
result="0";
}
return result;
}
string addBinary(string a, string b) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
//small
//int ai = string_to_int(a);
//int bi = string_to_int(b);
//int result = ai + bi;
//return int_to_string(result);
//large
string result;
int carray_bit = 0;
for(int i=a.length()-1,j=b.length()-1;i>=0 && j>=0; i--,j--) {
if(a[i]=='0' && b[j]=='0') {
if(carray_bit==1) {
result = "1" + result;
}
else {
result = "0" + result;
}
carray_bit=0;
}
else if( (a[i]=='0' && b[j]=='1') || (a[i]=='1' && b[j]=='0') ){
if(carray_bit==1) {
//carray_bit still 1
result = "0" + result;
}
else {
//carray_bit still 0
result = "1" + result;
}
}
else if(a[i]=='1' && b[j]=='1') {
if(carray_bit==1) {
result = "1" + result;
}
else {
result = "0" + result;
}
carray_bit = 1;
}
}
if(result.length()==0) {
result="0";
}
return result;
}
};
分享到:
相关推荐
LeetCode-Questions:示例回购包含一些leetcode问题的答案
leetcode的题目:Balanced Binary Tree
leetcode 答案leetcode_questions 把 Leetcode 问题和答案放在这里
leetcode卡 :dizzy: LeetCode for Python :snake: Requirements Python >= 3.8 Installation git clone git@github.com:imajinyun/leetcode-python.git cd leetcode-python Usage python3 -m unittest discover -s ....
Leetcode_questions 目前拥有: 简单的: 1.二和(c) 7.反转整数(c) 9.回文数(c) 14.最长公共前缀(python) 20.有效括号(python) 21.合并两个排序列表(c) 26.从排序数组中删除重复项 (c) 27.删除元素(c) 28.实现...
leetcode 316 算法 这是一个算法问题列表。 力码 LeetCode #1:二和 LeetCode #2:两个数字相加 LeetCode #5:最长回文子串 力扣#15:3Sum LeetCode #20:有效括号 LeetCode #21:合并两个排序列表 LeetCode #24:成...
leetcode数组下标大于间距 :dizzy: LeetCode for PHP :elephant: Requirements PHP >= 8.0 PHPUnit >= 9.5 Installation Install the package through . Run the Composer require command from the Terminal: git ...
leetcode 2 leetcode_prelude 在 LeetCode 中练习的一些有用的宏和定义。 如何使用 将以下行添加到您的 Cargo.toml。 [ dependencies ] leetcode_prelude = " ^0.2 " 例子 二叉树初始化 use leetcode_prelude :: ...
LeetCode 101:和你一起你轻松刷题(C++)
leetcode 答案Leetcode-问题 Leetcode 问题列表和答案
LEETCODE 问题 欢迎来到解决 200 个 Leetcode 问题频道。 请不要忘记给这个 repo 一个 Star :star: 加入我们的 Discord 社区,获得每日 LeetCode 挑战—— 如何贡献? 这个频道是关于什么的? 这是一个学习小组,...
leetcode 答案LeetCode 问题的思考过程 在这个 repo 中,我对一些 LeetCode 问题提出了思考过程以及易于理解的图表。 我还在 LeetCode 上链接了我的原始答案。 问题: 跳跃游戏 二叉树最大路径和 解码方式 叶相似树 ...
vscode提交leetcode leetcodeSolutionsPython 这些是 leetcodes 库的 python 解决方案。 Leetcode 是一种很棒的方式来练习编写面试的套路。 这是我的母语python3的解决方案集合。 这些解决方案是使用 leetcode 插件...
leetcode 答案 leetCode :keyboard:我的 Leetcode 解题答案
leetcode_101:LeetCode 101:和你一起你轻松刷题(C ++)
leetcode 答案LeetCode-问题 这些是我过去做过的各种 LeetCode 问题的答案
产品经理刷leetcode 目录 C/C++ const // 类 class A { private: const int a; // 常对象成员,只能在初始化列表赋值 public: // 构造函数 A() { }; A(int x) : a(x) { }; // 初始化列表 // const可用于对重载函数的...
add-to-list 'load-path ( expand-file-name " ~/.emacs.d/lisp/leetcode " )) 配置 你可以使用'use-package',有两个可配置的变量。 leetcode-path : 指定保存解决方案代码的路径 leetcode-language : 分
leetcode 分类 LeetCode :bouquet::bouquet::bouquet: 介绍 leetcode 题解,Issues 会记录 leetcode 解题之路,并使用 label 进行了分类。 目录 链表
LeetCode题解该仓库记录刷LeetCode的提交答案, 在锻炼自己的编程能力同时, 分享给也在刷题的小伙伴.本人的能力有限, 仅记录自己理解的和实现的解题方式.刷题不是为了找到答案, 而是为了找到方法.笔记参考资料集1. ...