zoukankan      html  css  js  c++  java
  • #Leetcode# 67. Add Binary

    https://leetcode.com/problems/add-binary/

    iven 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 {
    public:
        string addBinary(string a, string b) {
            string res;
            int na = a.size();
            int nb = b.size();
            int n = max(na, nb);
            bool carry = false;
            if (na > nb) {
                for (int i = 0; i < na - nb; ++i) b.insert(b.begin(), '0');
            }
            else if (na < nb) {
                for (int i = 0; i < nb - na; ++i) a.insert(a.begin(), '0');
            }
            for (int i = n - 1; i >= 0; --i) {
                int tmp = 0;
                if (carry) tmp = (a[i] - '0') + (b[i] - '0') + 1;
                else tmp = (a[i] - '0') + (b[i] - '0');
                if (tmp == 0) {
                    res.insert(res.begin(), '0');
                    carry = false;
                }
                else if (tmp == 1) {
                    res.insert(res.begin(), '1');
                    carry = false;
                }
                else if (tmp == 2) {
                    res.insert(res.begin(), '0');
                    carry = true;
                }
                else if (tmp == 3) {
                    res.insert(res.begin(), '1');
                    carry = true;
                }
            }
            if (carry) res.insert(res.begin(), '1');
            return res;
        }
    };
    

      LeetCode 的格式很难受了

    代码2:

    #include <bits/stdc++.h>
    using namespace std;
    
    char a[50], b[50];
    char ans[1010];
    
    int main() {
        scanf("%s%s", a, b);
        int lena = strlen(a), lenb = strlen(b);
        for(int i = 0; i < lena / 2; i ++)
            swap(a[i], a[lena - i - 1]);
        for(int i = 0; i < lenb / 2; i ++)
            swap(b[i], b[lenb - i - 1]);
    
        int len = min(lena, lenb), maxlen = max(lena, lenb);
        int cnt = 0;
        int k = 0;
        for(int i = 0; i < len; i ++) {
            ans[cnt ++] = (a[i] - '0' + b[i] - '0' + k) % 2 + '0';
            k = (a[i] - '0' + b[i] - '0' + k) / 2;
        }
        for(int i = len; i < maxlen; i ++) {
            if(maxlen == lena) {
                ans[cnt ++] = (a[i] - '0' + k) % 2 + '0';
                k = (a[i] - '0' + k) / 2;
            } else {
                ans[cnt ++] = (b[i] - '0' + k) % 2 + '0';
                k = (b[i] - '0' + k) / 2;
            }
        }
    
        if(k) ans[cnt ++] = '1';
    
        for(int i = 0; i < cnt / 2; i ++)
            swap(ans[i], ans[cnt - i - 1]);
    
        printf("%s
    ", ans);
        return 0;
    }
    

      

  • 相关阅读:
    RabbitMQ修改端口号和心跳时间
    RabbitMQ常用命令行
    机票分页存储过程!
    Sql Server随机取数据
    Entity framewok 如何实现多条记录作为一条取出, for xml path如何实现
    AJAX请求时status返回状态明细表 readyState的五种状态
    最小化到托盘,右键退出
    post注入及提权思路
    MySQL提权之user.MYD中hash破解方法
    鸡肋提权之变态root利用
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9992010.html
Copyright © 2011-2022 走看看