python_lintcode_698数组中最大的差值_3. 统计数字_12. 带最小值操作的栈
2018-01-03 14:26
686 查看
698数组中最大的差值
题目
http://www.lintcode.com/zh-cn/problem/maximum-difference-in-arrays/给 m 个数组, 每一个数组均为升序. 现在你可以从两个不同的数组中挑选两个整数(每一个数组选一个)并且计算差值. 我们将两个整数 a 和 b 之间的差定义为它们的绝对差 |a - b|. 你的任务是去找到最大的差值.
注意事项
每一个给出的数组长度至少为 1. 至少有两个不为空的数组
m 个数组中所有整数的个数和在 [2, 10000]范围内.
m 个数组中所有的整数均将在[-10000, 10000]范围内.
样例
给一个数组 [[1,2,3], [4,5], [1,2,3]], 返回 4
获得最大差值的一种方式是在第一个数组或第三个数组中取 1, 在第二个数组中取 5.
代码
class Solution: """ @param: : an array of arrays @return: return the max distance among arrays """ def maxDiff(self, arrs): # write your code here #数组都是按升序的 mina = arrs[0][0] maxa = arrs[0][-1] res = 0 for i in range(1,len(arrs)): res = max(res,abs(maxa - arrs[i][0])) res = max(res,abs(arrs[i][-1] - mina)) mina = min(mina,arrs[i][0]) maxa = max(maxa,arrs[i][-1]) return res
3. 统计数字
题目
http://www.lintcode.com/zh-cn/problem/digit-counts/计算数字k在0到n中的出现的次数,k可能是0~9的一个值
样例
例如n=12,k=1,在 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1 4000 0, 11, 12],我们发现1出现了5次 (1, 10, 11, 12)
代码
class Solution: """ @param: : An integer @param: : An integer @return: An integer denote the count of digit k in 1..n """ def digitCounts(self, k, n): # write your code here if k==0 and n==0: return 1 x =str(k) j=0 for i in range(n+1): m = str(i) if x in m: j = m.count(x)+j return j
12. 带最小值操作的栈
实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。
注意事项
如果堆栈中没有数字则不能进行min方法的调用
样例
如下操作:push(1),pop(),push(2),push(3),min(), push(1),min() 返回 1,2,1
代码
class MinStack: def __init__(self): # do intialization if necessary self.x = [] self.y = [] """ @param: number: An integer @return: nothing """ def push(self, number): # write your code here self.x.append(number) if len(self.y) == 0: self.y.append(number) else: self.y.append(min(self.y[-1],number)) """ @return: An integer """ def pop(self): # write your code here self.y.pop() return self.x.pop() """ @return: An integer """ def min(self): # write your code here return self.y[-1]
相关文章推荐
- [LintCode 12] 带最小值操作的栈(Python)
- python - LintCode 3. 统计数字
- LintCode 12 带最小值操作的栈
- [LintCode]12.带最小值操作的栈
- 1.数组--最大值 2.数组--选择排序与图解 3.数组--冒泡排序图解 4.数组--1.自带排序2.遍历功能方便 5.数组-常见操作-排序位置置换代码提取
- lintcode-带最小值操作的栈-12
- LintCode练习-12. 带最小值操作的栈
- Exe 9 统计文件中的整型数字的平均值(去掉一个最大和最小值)
- LintCode-----12.带最小值操作的栈
- 数组排序数字大小,与筛选最大值最小值。冒泡比较法。。研究了好久才写出来与大家分享
- 数组升序降序最大值最小值统计的类模板及实例
- python_lintcode_109数字三角形_50数组剔除元素后的乘积
- LintCode 12. 带最小值操作的栈
- 关于使用定义函数来实现对于数组c中的元素进行筛选最大值最小值,计算平均值等操作
- lintcode 12.带最小值操作的栈
- Lintcode 3. 统计数字
- python_lintcode_简单题 _44最小子数组_41最大子数组
- 编程之美:第二章 数字之魅 2.10寻找数组中的最大值和最小值
- lintcode 12. 带最小值操作的栈
- 最大最小值以及前驱后继操作最坏情况都为O(1)的顺序统计树