49、n个骰子的点数
扔 n 个骰子,向上面的数字之和为 S。给定 Given n,请列出所有可能的 S 值及其相应的概率。
给定 n = 1
,返回 [ [1, 0.17], [2, 0.17], [3, 0.17], [4, 0.17], [5, 0.17], [6, 0.17]]
。
方法一:递归
【思路】设n个骰子某次投掷点数和为s的出现次数是F(n, s),那么,F(n, s)等于n - 1个骰子投掷的点数和为s - 1、s - 2、s - 3、s -4、s - 5、s - 6时的次数的总和:F(n , s) = F(n - 1, s - 1) + F(n - 1, s - 2) + F(n - 1, s - 3) + F(n - 1, s - 4) + F(n - 1, s - 5) + F(n - 1, s - 6)。
方法二、循环
和值分布是对称的,这里只生产一半数据,后面数据复制过去即可。
数组result[i],因为第 i 个元素代表 i+1个骰子,所以长度为 5*(i+1) +1,
顶峰值为第3n个元素,由于 i 为奇数时,数组长度为偶数,顶峰值有两个,3n和3n+1,同样是对称的。
和值可能性分布数组:
n=1 : [1, 1, 1, 1, 1, 1]
n=2 : [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
n=3 : [1, 3, 6, 10, 15, 21, 25, 27, 27, 25, 21, 15, 10, 6, 3, 1]
n=4 : [1, 4, 10, 20, 35, 56, 80, 104, 125, 140, 146, 140, 125, 104, 80, 56, 35, 20, 10, 4, 1]
n=5 : [1, 5, 15, 35, 70, 126, 205, 305, 420, 540, 651, 735, 780, 780, 735, 651, 540, 420, 305, 205, 126, 70, 35, 15, 5, 1]
# -*- coding:utf-8 -*-
def dicesSum(n):
# Write your code here
if n == 0: return None
result = [
[1, 1, 1, 1, 1, 1],
]
# if n == 1: return result[0]
# 计算n个骰子出现的各个次数和
for i in range(1, n):
x = 5 * (i + 1) + 1
result.append([0 for _ in range(x)])
for j in range(x):
if j < 6:
result[i][j] = (sum(result[i - 1][0:j + 1]))
elif 6 <= j <= 3 * i + 2:
result[i][j] = (sum(result[i - 1][j - 5:j + 1]))
else:
break
left = 0
right = len(result[i]) - 1
while left <= right:
result[i][right] = result[i][left]
left += 1
right -= 1
res = result[-1]
all = float(sum(res))
other = []
# 第i个元素代表骰子总和为n+i
for i, item in enumerate(res):
# pro = self.round(item/all)
# 自己写的四舍五入算法和LintCode有出入,其实网站自身会处理数据,这里不再做处理
pro = item / all
other.append([n + i, pro])
return other
def round(num):
# 将概率值四舍五入
num = num * 100
num = int(2 * num) / 2 + int(2 * num) % 2
num = num / 100.0
return num
50、求1+2+3+4+...+n
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.sum = 0
def Sum_Solution(self, n):
# write code here
def qiusum(n):
self.sum += n
n -= 1
return n>0 and self.Sum_Solution(n)
qiusum(n)
return self.sum
利用逻辑与的短路特性实现递归终止:a&&b,当a为False(==0),则b短路,不运算。
当n==
0
时,(n>
0
)&&((sum+=Sum_Solution(n-
1
))>
0
)只执行前面的判断,为
false
,然后直接返回
0
;
当n>
0
时,执行sum+=Sum_Solution(n-
1
),实现递归计算Sum_Solution(n)。
class Solution {
public:
int Sum_Solution(int n) {
int ans = n;
ans && (ans += Sum_Solution(n - 1));
return ans;
//return ((int)pow(n,2) + n) >> 1; 这种方法其实还是再用公式
}
};
实现乘法可以用sizeof多维数组。
class Solution {
public:
int Sum_Solution(int n) {
bool a[n][n+1];
return sizeof(a)>>1;
}
};
51、不用加减乘除做加法
5-101,7-111
第一步:相加各位的值,不算进位,得到010,二进制每位相加就相当于各位做异或操作,101^111。 第二步:计算进位值,得到1010,相当于个位做与操作得到101,再向左移一位得到1010,(101&111)<<1。 第三步重复上述两步, 个位相加 010^1010=1000,进位值为100=(010&1010)<<1。 继续重复上述两步:1000^100 = 1100,进位值为0,跳出循环,1100为最终结果。
# -*- coding:utf-8 -*-
class Solution:
def Add(self, num1, num2):
# write code here
while num2!=0:
numsum=num1^num2
num2 = (num1&num2)<<1
num1=numsum
return num1
52、把字符串转换成整数
输入描述:
输入一个字符串,包括数字字母符号,可以为空
输出描述:
如果是合法的数值表达则返回该数字,否则返回0
【思路】
# -*- coding:utf-8 -*-
class Solution:
def StrToInt(self, s):
# write code here
if len(s)==0:
return 0
else:
if s[0]>'9' or s[0]<'0':
a=0
else:
a=int(s[0])*10**(len(s)-1)
if len(s)>1:
for i in range(1,len(s)):
if s[i]>='0' and s[i]<='9':
a=a+int(s[i])*10**(len(s)-1-i)
else:
return 0
if s[0]=='+':
return a
if s[0]=='-':
return -a
return a
53、树中两个结点的最低公共祖先(二叉搜索树)
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
分析:二叉排序树,设p,q的最小公共祖先是r,那么应满足p.val<=r.val and q.val >=r.val,即p,q应在r的两侧
所以从根节点开始按照某种遍历搜索,检查当前节点是否满足这种关系,如果满足则返回。如果不满足,例如
p,q都大于r,那么继续搜索r的右子树
"""
if root:
if root.val > p.val and root.val > q.val:
return self.lowestCommonAncestor(root.left, p, q)
elif root.val < p.val and root.val < q.val:
return self.lowestCommonAncestor(root.right, p, q)
else:
return root
return None
54、数组中重复的数字
# -*- coding:utf-8 -*-
class Solution:
# 这里要特别注意~找到任意重复的一个值并赋值到duplication[0]
# 函数返回True/False
def duplicate(self, numbers, duplication):
# write code here
# 这里要特别注意~找到任意重复的一个值并赋值到duplication[0]
# 函数返回True/False
if numbers==[]:
return False
vec=[]
for i in numbers:
if i in vec:
duplication[0]=i #找到任意重复的一个值并赋值到duplication[0]
return True
else:
vec.append(i)
return False
