LeetCode - 67. Add Binary(4ms)

来源:https://www.cnblogs.com/zlian2016/archive/2018/07/31/9398582.html
-Advertisement-
Play Games

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 ...


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", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"
  1 class Solution {
  2 public:
  3     string addBinary(string a, string b) {
  4         int la = a.length();
  5         int lb = b.length();
  6         string res = "";
  7         int car = 0;
  8         if(la <= lb) {
  9             for(int i = la - 1; i >= 0; i--) {
 10                 if(a[i] == '1' && b[lb - (la - i)] == '1') {
 11                     if(car == 1)
 12                         res = '1' + res;
 13                     else {
 14                         res = '0' + res;
 15                         car = 1;
 16                     }
 17                 }
 18                 else if(a[i] != b[lb - (la - i)]) {
 19                     if(car == 1)
 20                         res = '0' + res;
 21                     else
 22                         res = '1' + res;
 23                 }
 24                 else {
 25                     if(car == 1) {
 26                         res = '1' + res;
 27                         car = 0;
 28                     }
 29                     else
 30                         res = '0' + res;
 31                 }
 32             }
 33             for(int i = lb - la - 1; i >= 0; i--) {
 34                 if(b[i] == '1') {
 35                     if(car == 1)
 36                         res = '0' + res;
 37                     else
 38                         res = '1' + res;
 39                 }
 40                 else {
 41                     if(car == 1) {
 42                         res = '1' + res;
 43                         car = 0;
 44                     }
 45                     else
 46                         res = '0' + res;
 47                 }
 48             }
 49             if(car == 1) {
 50                 res = '1' + res;
 51             }
 52             return res;
 53         }
 54         else if(lb < la) {
 55             for(int i = lb - 1; i >= 0; i--) {
 56                 if(b[i] == '1' && a[la - (lb - i)] == '1') {
 57                     if(car == 1)
 58                         res = '1' + res;
 59                     else {
 60                         res = '0' + res;
 61                         car = 1;
 62                     }
 63                 }
 64                 else if(b[i] != a[la - (lb - i)]) {
 65                     if(car == 1)
 66                         res = '0' + res;
 67                     else
 68                         res = '1' + res;
 69                 }
 70                 else {
 71                     if(car == 1) {
 72                         res = '1' + res;
 73                         car = 0;
 74                     }
 75                     else
 76                         res = '0' + res;
 77                 }
 78             }
 79             for(int i = la - lb - 1; i >= 0; i--) {
 80                 if(a[i] == '1') {
 81                     if(car == 1)
 82                         res = '0' + res;
 83                     else
 84                         res = '1' + res;
 85                 }
 86                 else {
 87                     if(car == 1) {
 88                         res = '1' + res;
 89                         car = 0;
 90                     }
 91                     else
 92                         res = '0' + res;
 93                 }
 94             }
 95             if(car == 1) {
 96                 res = '1' + res;
 97             }
 98             return res;
 99         }
100     }
101 };

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1.0 新建運行環境 命令: pyvip@Vip:~$ mkvirtualenv -p /usr/bin/python3 Django2Running virtualenv with interpreter /usr/bin/python3Using base prefix '/usr'New py ...
  • 作者: "Jack47, ZhiYan" 轉載請保留作者和 "原文出處" 性能優化,優化的東西一定得在主路徑上,結合測量的結果去優化。不然即使性能再好,邏輯相對而言執行不了幾次,其實對提示性能的影響微乎其微。記得抖哥以前說多隆在幫忙查廣告搜索引擎的問題,看到了一處代碼,激動的說這裡用他的辦法,性能可 ...
  • if的使用 if 後面接的是表達式 如果 if後面的表達式能成立,就會把 if和 endif之間的代碼編譯進去 if defined的使用 如果x這個巨集又被定義過,則把 if和 endif之間的代碼編譯進去 註意點 1. 兩個都只是用來決定某段代碼是否被編譯 2. 記得加 endif ...
  • 綠茶網貸系統是綠茶科技旗下自主開發的網貸系統,可以支持定製p2p網貸網站,網貸平臺網站開發,小額貸系統,現金網貸系統開發,小貸網站開發建設,p2p貸款網站程式,微信網貸網站源碼,這是一套網貸網站管理系統。可以支持定製電腦版+手機版+微信版+小程式版+APP版,由10年的技術團隊專業定製,需要的朋友可 ...
  • title: 網路流 date: 2018 07 31 22:01:22 tags: acm 演算法 網路流 概述 這篇博客主要是關於網路流的一些基本的知識點以及相應的模板,, 算了,,,還是先貼大佬的博客,,,暑假在補一下。。。。QAQ <! more 網路流 tan90,,,,,,, 習題 Pro ...
  • You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you cl ...
  • 就像有知覺的生物一樣,程式必須在執行的過程中控制它的世界,並做出選擇。java使用執行流程式控制制語句做出選擇。 1、選擇語句 if switch in從case中無法匹配到,如果有default會執行,default可以在任何位置;如果default後無break,會繼續向下執行,否則跳出。 2、迴圈 ...
  • 1. Win7下安裝CodeBlocks: 下載帶有mingw的CodeBlocks:http://www.codeblocks.org/downloads/26#windows 運行所下載程式: 點擊next : 同意安裝協議,點擊 I agree : 選擇預設完全安裝,點擊next : 選擇安裝 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...