整型数组(一)
来源:互联网 发布:informix默认端口 编辑:程序博客网 时间:2024/05/21 01:29
Remove Element
Given an array and a value, remove all occurrencesof that value in placeand return thenew length.The order of elements can be changed, and the elements after thenew length don't matter.ExampleGiven an array [0,4,4,0,0,2,4,4], value=4return 4 and front four elements of the array is [0,0,0,2]
def remove(s,value,length): if s is None: return False a=[] for i in range(len(s)): if s[i]!=value and len(a)<length: a.append(s[i]) return as=[0,4,4,0,0,2,4,4]value=4length=4remove(s,value,length)
Zero Sum Subarray
Given an integer array, find a subarray where the sum of numbers is zero.Your code should return the index of the first number and the index of the last number.ExampleGiven [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].NoteThere is at least one subarray that it's sum equals to zero.
def zero_sum(s): c=[] for i in range(int(len(s)/2)+1): a=[] b=[] for j in range(len(s)-1-i): a=s[i:j+i+1] if np.sum(a)==0: b.append([i,j+i]) if b!=[]:c.append(b) return cs=[-3, 1, 2, -3, 4]zero_sum(s)
Note:
1. At first I try to add i+1 to range(len(s)-1+i), but this operation will connect two lists together. Finally I find change to [i:i+j] is useful.
2.About loop: tab means executing the correlated operations.
阅读全文
0 0
- 整型数组(一)
- 整型数组处理算法(一)按照正态分布来排列整型数组元素
- 数组反转(整型数组)
- 整型数组处理算法(十四)不用库函数,用C语言实现将一整型数转换成字符串
- 一维整型字符串数组、二维数组、锯齿数组
- 整型数组
- 第十三周项目一 数组大折腾(4.删除长度为16整型数组中能被3整除的数)
- C语言基础 for循环遍历一维整型数组
- 整型数组处理算法(六)合并两个数组
- fjnu 1730 整型数组
- c++返回整型数组
- python 整型数组
- 整型数组逆置
- 第十三周项目一 数组大折腾(3.输出长度16的整型数组中与3有关的数)
- 将整型数组A中的内容和整型数组B中的内容进行交换。(数组一样大)
- memset函数不能给int(整型)数组初始化赋值
- 判断整型数组a[N]是不是升序排列(递归)
- 整型数组处理算法(七)重排问题
- 利用伪元素制作盒子阴影
- js自定义滚动轴
- 从快速幂到dp 优化:矩阵快速幂
- ios-关于深拷贝和浅拷贝和属性中的copy和strong的区别
- 2017-7-14
- 整型数组(一)
- python---高级特性
- js贪吃蛇
- 解决Mac的virtual box安装centos7的报错问题
- POJ
- 如何转载博客
- 远程登录Linux
- HDU 5813 Elegant Construction 【贪心】
- ace admin日期控件 中文显示