LeetCode开心刷题三十七天EasyDayTommorrowHarder——66. Plus One67 Add Binary

66. Plus One
Easy
9641703FavoriteShare

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

BONUS:
range(start,end,step) end not reach
for i in range(3,-1,-1):
    print(i)

3
2
1
0
class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        stop in time reduce time
        abandon carry instead of directly return or default carry
        """
        for i in range(len(digits)-1,-1,-1):
            if digits[i]<9:
                digits[i]+=1
                return digits
            else:
                digits[i]=0
        digits.insert(0,1)
        return digits

solu=Solution()
print(solu.plusOne(digits=[9,9,9]))

Compare to directly follow people's mind,This more cleaner

 
67. Add Binary
Easy
1107211FavoriteShare

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"
 BONUS:
1.
#include <iostream>

using namespace std;

int main()
{
    int n1=3,n2=2,flag=0,n;
    n=n1>n2?(n2,flag=1):n1;
    cout << n << flag<<endl;
    return 0;
}

Ternary Operator:

can add flag in (),special usage I found it myself

2.

左移运算符(<<)规则

编辑

  按二进制形式把所有的数字向左移动对应的位数,高位移出(舍弃),低位的空位补零。

语法格式:

需要移位的数字 << 移位的次数
  例如: 3 << 2,则是将数字3左移2位

计算过程:

3 << 2
  首先把3转换为二进制数字0000 0000 0000 0000 0000 0000 0000 0011,然后把该数字高位(左侧)的两个零移出,其他的数字都朝左平移2位,最后在低位(右侧)的两个空位补零。则得到的最终结果是0000 0000 0000 0000 0000 0000 0000 1100,则转换为十进制是12。

数学意义:

在数字没有溢出的前提下,对于正数和负数,左移一位都相当于乘以2的1次方,左移n位就相当于乘以2的n次方。

右移运算符(>>)规则:

编辑
按二进制形式把所有的数字向右移动对应位移位数,低位移出(舍弃),高位的空位补符号位,即正数补零,负数补1。

语法格式:

需要移位的数字 >> 移位的次数
  例如11 >> 2,则是将数字11右移2位

计算过程:

11的二进制形式为:0000 0000 0000 0000 0000 0000 0000 1011,然后把低位的最后两个数字移出,因为该数字是正数,所以在高位补零。则得到的最终结果是0000 0000 0000 0000 0000 0000 0000 0010。转换为十进制是2。

数学意义:

右移一位相当于除2,右移n位相当于除以2的n次方。
3.
&是按位与运算
1&1=1,1&0=0,0&0=0,0&1=0;

int型变量在一般内存中占用4个字节,
2=00000000 00000000 00000000 0000010
1=00000000 00000000 00000000 0000001
2&1=0
 
Python version:
4.int(number,base(进制))
>>>int()               # 不传入参数时,得到结果0
0
>>> int(3)
3
>>> int(3.6)
3
>>> int('12',16)        # 如果是带参数base的话,12要以字符串的形式进行输入,12 为 16进制
18
>>> int('0xa',16)  
10  
>>> int('10',8)  
8

bin() 返回一个整数 int 或者长整数 long int 的二进制表示。

>>>bin(10)
'0b1010'
>>> bin(20)
'0b10100'

rstrip()方法语法:lstrip()方法语法:

语法

lstrip()方法语法:

str.lstrip([chars])

参数

  • chars --指定截取的字符。

返回值

返回截掉字符串左边的空格或指定字符后生成的新字符串。

实例

以下实例展示了lstrip()的使用方法:

#!/usr/bin/python

str = "     this is string example....wow!!!     "; print str.lstrip(); str = "88888888this is string example....wow!!!8888888"; print str.lstrip('8');

以上实例输出结果如下:

this is string example....wow!!! this is string example....wow!!!8888888

描述

Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格).

语法

rstrip()方法语法:

str.rstrip([chars])

参数

  • chars -- 指定删除的字符(默认为空格)

返回值

返回删除 string 字符串末尾的指定字符后生成的新字符串。

实例

以下实例展示了rstrip()函数的使用方法:

#!/usr/bin/python

str = "     this is string example....wow!!!     "; print str.rstrip(); str = "88888888this is string example....wow!!!8888888"; print str.rstrip('8');

以上实例输出结果如下:

     this is string example....wow!!! 88888888this is string example....wow!!!
PS:strip normal use to delete meaningless character ,because it will delete all char element in the collection.
Python神仙算法
BONUS:
C++,java有三元运算符
但是Python中可以这样替代
如果a>b的结果为真,h="变量1",如果为假,h="变量2"
h = "变量1" if a>b else "变量2"
 
 

 

 
class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        ans="0" if (bin(int(a, 2) + int(b, 2)).lstrip('0b')=='') else (bin(int(a, 2) + int(b, 2)).lstrip('0b'))
        return ans

 

 

转载于:https://www.cnblogs.com/Marigolci/p/11421761.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值