目录
1. 数组逐位判断
2. 交错字符串
3. 二进制求和
4. 四舍六入五成双规则
每日一练刷题专栏
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
比如有以下数组:
a1: 1,0,0,1,0,0,0,1
a2: 0,0,0,0,1,1,1,1
a3: 0,1,0,1,0,1,0,0
a4: 1,0,1,1,1,1,0,0
a5: .......
抓取三个数组进行判断, if ((a1第一位or a2第一位 or a3第一位=1 )and (a1第二位 or a2 第二位 or a3第二位=1)and.... 直到判断完所有位数为止,所有位都有了1的话就输出当前这三个数组,已输出的数组不参与之后的判断。
出处:
https://edu.csdn.net/practice/26046536
代码:
# -*- coding: UTF-8 -*-
from itertools import combinations
a1=[ 1,0,0,1,0,0,0,1]
a2=[ 0,0,0,0,1,1,1,1]
a3=[ 0,1,0,1,0,1,0,0]
a4=[ 1,0,1,1,1,1,0,0]
a5=[ 1,1,1,1,1,1,1,0]
a6=[ 0,0,0,0,0,0,0,1]
a=[a1,a2,a3,a4,a5,a6]
al = list(combinations(a,3))
for i in al:
flag = True
for j in range(len(i[0])):
if (i[0][j] + i[1][j] + i[2][j] == 0):
flag = False
break
if flag:
print(i)
输出:
([1, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0])
([1, 0, 0, 1, 0, 0, 0, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])
([1, 0, 0, 1, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])
([1, 0, 0, 1, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])
([0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 0, 1, 1, 1, 1, 0, 0])
([0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])
([0, 0, 0, 0, 1, 1, 1, 1], [1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])
([0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])
([0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])
([1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])
给定三个字符串 s1
、s2
、s3
,请你帮忙验证 s3
是否是由 s1
和 s2
交错 组成的。
两个字符串 s
和 t
交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串:
s = s1 + s2 + ... + sn
t = t1 + t2 + ... + tm
|n - m| <= 1
s1 + t1 + s2 + t2 + s3 + t3 + ...
或者 t1 + s1 + t2 + s2 + t3 + s3 + ...
提示:a + b
意味着字符串 a
和 b
连接。
示例 1:
输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" 输出:true
示例 2:
输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" 输出:false
示例 3:
输入:s1 = "", s2 = "", s3 = "" 输出:true
提示:
0 <= s1.length, s2.length <= 100
0 <= s3.length <= 200
s1
、s2
、和 s3
都由小写英文字母组成出处:
https://edu.csdn.net/practice/26046537
代码:
class Solution(object):
def isInterleave(self, s1, s2, s3):
"""
:type s1: str
:type s2: str
:type s3: str
:rtype: bool
"""
if len(s1) + len(s2) != len(s3):
return False
queue = [(0, 0), (-1, -1)]
visited = set()
isSuccess = False
index = 0
while len(queue) != 1 or queue[0][0] != -1:
p = queue.pop(0)
if p[0] == len(s1) and p[1] == len(s2):
return True
if p[0] == -1:
queue.append(p)
index += 1
continue
if p in visited:
continue
visited.add(p)
if p[0] < len(s1):
if s1[p[0]] == s3[index]:
queue.append((p[0] + 1, p[1]))
if p[1] < len(s2):
if s2[p[1]] == s3[index]:
queue.append((p[0], p[1] + 1))
return False
# %%
s = Solution()
print(s.isInterleave(s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"))
print(s.isInterleave(s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"))
print(s.isInterleave(s1 = "", s2 = "", s3 = ""))
输出:
True
False
True
给你两个二进制字符串,返回它们的和(用二进制表示)。
输入为 非空 字符串且只包含数字 1
和 0
。
示例 1:
输入: a = "11", b = "1" 输出: "100"
示例 2:
输入: a = "1010", b = "1011" 输出: "10101"
提示:
'0'
或 '1'
组成。1 <= a.length, b.length <= 10^4
"0"
,就都不含前导零。出处:
https://edu.csdn.net/practice/26046539
代码:
class Solution(object):
def addBinary(self, a, b):
res = ''
lsa, lsb = len(a), len(b)
pos, plus, curr = -1, 0, 0
while (lsa + pos) >= 0 or (lsb + pos) >= 0:
if (lsa + pos) >= 0:
curr += int(a[pos])
if (lsb + pos) >= 0:
curr += int(b[pos])
res = str(curr % 2) + res
curr //= 2
pos -= 1
if curr == 1:
res = '1' + res
return res
# %%
s = Solution()
print(s.addBinary(a = "11", b = "1"))
print(s.addBinary(a = "1010", b = "1011"))
输出:
100
10101
输入:1234
输出:1234
12 12.0
4 4.00
0.2 0.200
0.32 0.320
1.3 1.30
1.235 1.24
1.245 1.24
1.2451 1.25
出处:
https://edu.csdn.net/practice/26046539
代码:
nums = [1234,12,4,0.2,0.32,1.3,1.235,1.245,1.2451]
for n in nums:
a = str(n)
if '.' in a:
a = float(a)
if a*1000%10!=5:
a = '%.2f'%(a)
else:
if len(str(a).split(".")[1])>3:
a = '%.2f'%(a)
else:
if int(a*100%10%2)==1:
a = float('%.2f'%float(int(a*100)/100))+0.01
else:
a = '%.2f'%float(int(a*100)/100)
print(a)
else:
a = int(a)
if a>99:
print(a)
else:
if 0 < a < 10:
print('%.2f'%a)
else:
print(float(a))
输出:
1234
12.0
4.00
0.20
0.32
1.30
1.24
1.24
1.25
✨ 持续,努力奋斗做强刷题搬运工!
点赞,你的认可是我坚持的动力!
收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
Golang每日一练 专栏 |
|
Python每日一练 专栏 |
|
C/C++每日一练 专栏 |
|
Java每日一练 专栏 |