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
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"
#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转换为二进制数字0000 0000 0000 0000 0000 0000 0000 0011,然后把该数字高位(左侧)的两个零移出,其他的数字都朝左平移2位,最后在低位(右侧)的两个空位补零。则得到的最终结果是0000 0000 0000 0000 0000 0000 0000 1100,则转换为十进制是12。
数学意义:
语法格式:
例如11 >> 2,则是将数字11右移2位
计算过程:
数学意义:
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
>>>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