[LeetCode]Add Binary

LeetCode Add Binary 1.题目Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".2.解答class Solution { public: char intToChar(int input){ char strBuffer[1] = {0 阅读详情
class Solution {
//this is a very simple implementation
//should practice a lot
public:
	string addBinary(string a, string b) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		int carry = 0;
		int p = a.size()-1;
		int q = b.size()-1;
		string ans;
		while (p >= 0 || q >= 0 || carry)
		{
			int a_val = (p >= 0 ? a[p]-'0' : 0);
			int b_val = (q >= 0 ? b[q]-'0' : 0);
			int now = carry+a_val+b_val;
			int tmp = now%2;
			carry = now/2;
			ans.insert(ans.begin(), tmp+'0');

			p = (p >= 0 ? --p : p);
			q = (q >= 0 ? --q : q);
		}
		
		return ans;
	}
};

second time

class Solution {
public:
    string addBinary(string a, string b) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        int carry = 0;
        int i = 0;
        int j = 0;
        string ans;
        while(i < a.size() || j < b.size() || carry != 0)
        {
            int tmp1 = 0;
            int tmp2 = 0;
            if(i < a.size()) tmp1 = a[i]-'0', i++;
            if(j < b.size()) tmp2 = b[j]-'0', j++;
            int sum = tmp1+tmp2+carry;
            ans.push_back(sum%2+'0');
            carry = sum/2;
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};


ROS四旋翼无人机快速上手指南(3):工作环境文件框架与各功能包整体讲解 这一章节主要讲解无人机平台的控制文件架构和各个功能包组成和用法。 阅读详情

相关推荐

如何理解反步法设计控制器?

假设以二阶系统为例,被控对象的系统状态方程为: 基于反步控制的具体方法如下: 1.1步骤一 定义期望状态为xd,状态误差e1,为: e1=x1-xd; 则e1的导数为: 1.2步骤二 定义虚拟控制量为: e2定义为: 1.3步骤三 定义Lyapunov能量函数为: 1.4步骤四 定义总Lyapunov能量函数为: 因此设计的控制律为: 设计c>0,则: 满足Lyapunov的稳定性判定条件...

weixin_42736130的博客 2万+

leetcode 随笔 Plus One & Add Binary --两道 leetcode 适合新手的题

今天这两题可以说是很基础了,比较适合新手,lz大概是从大一就听C++老师给我们推荐去leetcode上面刷刷题锻炼编程,当时基本上也就是刚会写hello world的水平吧,看着输入的vector真是一脸懵逼,因此就放弃了。其实当时的基础几乎等于0,大二之前也没有写过超过200行的程序。到了大二下学习数据库,数据库老师又提起让我们没事刷刷leetcode,当时也有了一定的编程能力了,然后靠百度学会...

Runner_of_nku的博客 1万+

对E-R模型向关系模式转换的认识

一:基本E-R图的转换 1     属性处理 派生属性:直接忽略。 复合属性:展平技术。 多值属性:单独建立一个关系表,关系名就是多值属性名,如果是实体集的多值属性,关系的属性就是实体集的码加上多值属性,如果是联系集的多值属性,关系的属性就是各个实体集的码加上多值属性,关系的码要具体情况具体分析。 2     基本E-R图转换为关系模式 强实体集:一个关系,关系名就是强实体集名,关系属

优快云_LYY的专栏 2731

[LeetCode] Add Binary

https://leetcode.com/problems/add-binary/ 模拟加法操作,链表也有类似的问题(Add Two Numbers)。 /* * author : TK * date : 2017-02-14 * problem: LeetCode 67: Add Binary * method : simulate the operation of add...

Sari_2015的博客 154

Doris + Iceberg 实现高效数据湖分层:架构解析与最佳实践

本文深入解析了如何利用Doris与Iceberg构建高效的数据湖冷热分层架构。通过将高频访问的热数据存储在Doris中以实现毫秒级查询,同时将历史冷数据归档至低成本的Iceberg存储,该方案在保障查询性能的同时显著降低了总体拥有成本。文章详细阐述了从架构设计、自动化迁移到统一查询的最佳实践,为应对海量数据下的成本与性能平衡提供了实战指南。

weixin_29207533的博客 135

用 ADT 连接 SAP S/4HANA Public Cloud 开发租户的完整落地指南

摘要:在SAP S/4HANA公有云中进行ABAP开发时,必须使用Development Tenant(客户端080)而非Customizing Tenant(客户端100)连接ADT工具。开发用户需具备SAP_BR_DEVELOPER角色且写权限不受限,开发对象应创建在结构包的子包中。连接前需确认正确的ABAP服务URL(通常为Launchpad URL去掉"/ui"),并通过运行控制台程序验证全链路功能。典型开发场景如通过BAdI实现业务校验时,需遵循Clean Core原则在指定包

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 338

leetcode add_binary 采坑记

尽管add_binary在leetcode中是一个简单难度,但是踩了不少坑,记录一下 描述: 给两个字符串形式的二进制数,要求求和并输出字符串形式的结果,其中a和b均不为空字符串 样例: a=“1010”,b="1011",输出“10101”. 过程: 刚看到题目的时候觉得很简单,只要每一位按位相加就好了,很容易写出第一版代码 public class AddBi...

baban1118的博客 188

STM32F103RCT6的FLASH读写操作

STM32F103RCT6是一款常用的32位微控制器,它具有内置的FLASH存储器,可以用于存储程序代码和数据。在本文中,我们将详细介绍如何进行STM32F103RCT6的FLASH读写操作,并提供相应的源代码。写入数据时,我们通过将数据直接写入FLASH地址来完成。通过使用适当的API和规则,我们可以有效地读取和写入数据。写入FLASH时,我们需要按特定的规则进行编程。首先,我们需要包含适当的头文件和定义相关的宏和变量,以便在代码中使用FLASH API。类型的指针,我们可以直接读取FLASH中的数据。

UplKubernetes的博客 1034

【leetcode】Add Binary

Add BinaryGiven two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”. 思路: 和链表相加比较相似,但是要注意对字符串的处理。最后输出要反向~class Solution { public: string add

胖子jimmy的学习记录~ 358

【AI编程】Vibe Coding 开发环境搭建与项目实战操作详解

Vibe Coding 开发环境搭建与项目实战操作详解

congge 7335

Android功耗系列专题理论之二十一:Power Monitor测功耗详细说明和注意事项

本文详细介绍了PowerMonitor手机功耗测试仪的功能与使用方法。该设备通过硬件连接替代手机电池,实时监测电流、电压等参数,适用于研发测试、软件优化等场景。文章包含设备接线说明(电源、USB、手机供电等接口)、驱动安装步骤及常见问题排查,并详细讲解了功耗测量操作流程,包括电压设置、开机步骤、数据采集与导出方法。特别强调了测量注意事项,如固定屏幕亮度、关闭后台应用、确保稳定连接等,以确保测试数据准确性。最后提供了简易测量步骤和详细测量流程指导。

596

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 {

追风筝的吃货,汪~ 594

【高级雷达感知与学习】 第十三章 可解释性与物理–感知融合(Physics-Informed ML)

本文提出了一种面向雷达感知的物理约束学习方法,将雷达物理先验(RCS、SNR、距离衰减)嵌入神经网络,解决纯数据驱动模型的样本效率低、泛化能力弱和决策不可信问题。通过三种融合范式(混合建模、物理正则化、PINNs)实现机理与数据协同建模,并设计了物理引导显著图解决雷达可解释性语义错位问题。实验表明,物理约束可提升40%样本效率,增强低信噪比场景鲁棒性。代码实现包含物理正则化损失、可解释性分析和消融实验,为雷达感知系统提供了可迁移的工程范本。

VectorShift的博客 186

LeetCode - 67. Add Binary(4ms)

LeetCode - 67. Add Binary(4ms) Given two binary strings, return their sum (also a binary string). The input strings are bothnon-emp...

dichunpu6524的博客 143

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

【leetcode刷题】66 plus one & 67 add binary

原题链接 https://leetcode.com/problems/plus-one/ https://leetcode.com/problems/add-binary/ 解题思路 这两道题其实思路比较类似,都是做一个加法,不过一个是10进制,一个是2进制,本质相同;用一个列表carry记录每一位的进位情况即可。直接上代码。 代码 plus one class Solution(object):...

Arwen的博客 174

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Leetcode: 67. Add Binary

Leetcode: 67. Add Binary 二进制加法 https://discuss.leetcode.com/topic/33693/another-simple-java public String addBinary(String a, String b) { if(a==null||b==null){ ...

哎呀妈呀脑壳疼脑壳疼 158

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

leetcode 67. Add Binary Python快速实现

题目 leetcode 67. Add Binary Python Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = “11...

2Dposeliu的博客 281

LeetCode 67. Add Binary

LeetCode 67. Add Binary Solution1:我的答案 时间复杂度为O(max{m,n})O(max{m,n})O(max\{m,n\}),空间复杂度亦是如此 class Solution { public: string addBinary(string a, string b) { int m = a.size() - 1, n = b.si...

Allenlzcoder的博客 289

LeetCode力扣 67. 二进制求和 Add Binary 题解代码 JavaScript

问题https://leetcode-cn.com/problems/add-binary/ 练习使用JavaScript解答 /** * @param {string} a * @param {string} b * @return {string} */ var addBinary = function(a, b) { var s = ""; var ai,bi,ci=0,si = 0; var i, j; for(i = a.length-1, j = .

Coding home - 漂流瓶jz 604

【Leetcode】Add Binary

题目链接:https://leetcode.com/problems/add-binary/ 题目: Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 思路: easy 算法:

业精于勤,荒于嬉 1673

Add Binary -- LeetCode

原题链接: http://oj.leetcode.com/problems/add-binary/  这道题跟Add Two Numbers很类似,代码结构很接近。从低位开始,一直相加并且维护进位。和Add Two Numbers的区别是这个题目低位在后面,所以要从string的尾部往前加。时间复杂度是O(m+n),m和n分别是两个字符串的长度,空间复杂度是结果的长度O(max(m,n)

Code Ganker 1万+

Leetcode日练笔记24 #67 #28 Add Binary & Implement strStr()

#67 Add Binary Given two binary stringsaandb, returntheir sum as a binary string. 解题思路: 1. 考虑到 string A B 的长度可能不一样所以先补齐零。 2. 然后将string A和B 同一个index的数字求和之后赋予给新的string,new 3. 用for循环去从后往前看new每一个数位是不是大于2。逢二进一。 4.将new转变为string输出。 class Solution: ...

Bittersweet_t的博客 252

leetcode Add Binary

https://oj.leetcode.com/problems/add-binary/ 这个题目不是他别难,但要把string转成int[]却费了一番功夫。当然不转也可以。我的程序一如既往的笨拙。 当中有个while()循环。有一些专门对两个链表或数组操作的题目。 需要注意的是有的时候是或(||)符号,有的时候是并且符号(&&) 主要是看这两个链表要不要一定出现。比如如果是比较的话,确一

guanjianlujing的专栏 235
上一篇: [LeetCode]3Sum Closest
下一篇: [LeetCode]Add Two Numbers
AI记忆
AI记忆 优质创作者: 人工智能技术领域 优质创作者: 人工智能技术领域 领域专家: 算法与数据结构技术领域 领域专家: 算法与数据结构技术领域
博客等级 码龄19年 6685粉丝 429原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值