Arrays--codility
lesson 2: Arrays
- exercise:
- Problem: Given array A consisting of N integers, return the reversed array.
def myreverse(A):
length = len(A)
for i in xrange(length//2):
A[i], A[length -i-1] = A[length - i-1], A[i]
return A
1. CyclicRotation------[100%]
Rotate an array to the right by a given number of steps.
A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.
For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.
def re_enumerate(seq):
n = -1
length = len(seq)
for elem in reversed(seq):
yield length + n, elem
n = n - 1
def solution(A, K):
# write your code in Python 2.7
length = len(A)
if length < 2:
return A
shift_times = K%length
#print shift_times
tmp= A[-shift_times:]
#print tmp
tmp.extend(A[:- shift_times])
return tmp
#for idx, ai in re_enumerate(A):
# if idx >= shift_times:
# A[idx] = A[idx-shift_times]
#
# else:
# A[idx] = tmp[idx]
#return A
- 要注意边界条件,考虑数组的长度少于旋转的次数,以及只有一个元素和数组为空
- 当有除法的时候,注意是否除以零
def solution(A, K):
# write your code in Python 2.7
length = len(A)
#print length
#print K
if K == 0 or length == 0 or length == 1:
return A
if K > length:
K %= length
#print K
tmp = [0]*K
#print tmp
for i in xrange(K):
tmp[i] = A[length - K + i]
tmp.extend(A[:-K])
return tmp
2. OddOccurrencesInArray ---[100%]
Find value that occurs in odd number of elements.
A non-empty zero-indexed array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.
For example, in array A such that:
A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
- the elements at indexes 0 and 2 have value 9,
- the elements at indexes 1 and 3 have value 3,
- the elements at indexes 4 and 6 have value 9,
- the element at index 5 has value 7 and is unpaired.
解题思路:
- 将数组排序,相同的数会在一起,比较前后两个数是否相同
- 偶数个数的,继续测试下一个数,找到单独的一个数,返回
- 当直到数组最后,还有没有找到单独的一个元素,我们得返回最后一个元素,因为我们统计的一直的是last 元素
- 注意临界条件,只有一个元素
- O(N)时间复杂度
def solution(A):
# write your code in Python 2.7
if len(A) == 1:
return A[0]
A.sort()
#print A
last_a,cnt = A[0],1
#print last_a
for ai in A[1:]:
if ai == last_a:
cnt += 1
else:
if cnt%2 != 0:
return last_a
last_a,cnt = ai,1
return A[-1]
def solution(A):
# write your code in Python 2.7
if len(A) < 2:
return A[0]
A.sort()
last_a, cnt = A[0], 1
for a in A[1:]:
if last_a == a:
cnt += 1
else:
if cnt%2 == 0 :
last_a, cnt = a, 1
else:
return last_a
return A[-1]
Arrays--codility的更多相关文章
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- Java程序员的日常—— Arrays工具类的使用
这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...
- 使用 Arrays 类操作 Java 中的数组
Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索等(关于类和方法的相关内容在后面的章节中会详细讲解滴 ...
- 【转】java.util.Arrays.asList 的用法
DK 1.4对java.util.Arrays.asList的定义,函数参数是Object[].所以,在1.4中asList()并不支持基本类型的数组作参数. JDK 1.5中,java.util.A ...
- System.arraycopy()和Arrays.copyOf()的区别
先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...
- 计算机程序的思维逻辑 (31) - 剖析Arrays
数组是存储多个同类型元素的基本数据结构,数组中的元素在内存连续存放,可以通过数组下标直接定位任意元素,相比我们在后续章节介绍的其他容器,效率非常高. 数组操作是计算机程序中的常见基本操作,Java中有 ...
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
随机推荐
- [nodejs]npm国内npm安装nodejs modules终极解决方案
此方案用于设置代理和修改镜像地址都不能解决问题使用 1.npm root 确认node模块的根文件夹,全局要加-g. osx同样是此命令,先清除缓存. npm cache clean C:\Users ...
- ASP.NET MVC 路由系统类
RouteData public class RouteData { private RouteValueDictionary _dataTokens; private IRouteHandler _ ...
- 添加resx文件过程笔记
为了测试资源文件,今天上午耗掉了.终于实现了,原来是要按照它自动建文件夹,不可以手动建.下图 见其他语言的时候一定要:Mytest.zh-CN.resx 这样,加上后缀名字 代码中获取资源 http: ...
- URAL 1557 Network Attack 图论,连通性,tarjain,dfs建树,分类讨论 难度:2
http://acm.timus.ru/problem.aspx?space=1&num=1557 1557. Network Attack Time limit: 2.0 secondMem ...
- Prism 4 文档 ---第2章:初始化Prism应用程序
这一章节介绍Prism应用程序启动和运行时发生的内容.Prism应用程序在启动时需要有注册和配置的过程,这就是所谓的自自启动程序. 什么是自启动引导程序? 引导程序是一个类,它负责使用Pri ...
- ftp的虚拟用户的搭建
虚拟的搭建在安装的根匿名的一样,在改配置文件之前需要创建虚拟的账号 路径为/etc/vsftpd/ 创建pam认证文件 然后修改配置文件其余的文件配置跟本地的一样. 然后重启vsftpd服务 想要实现 ...
- 使用Unity创建塔防游戏(Part3)—— 项目总结
之前我们完成了使用Unity创建塔防游戏这个小项目,在这篇文章里,我们对项目中学习到的知识进行一次总结. Part1的地址:http://www.cnblogs.com/lcxBlog/p/60759 ...
- Windows消息队列(优先队列,结构体中放比较函数)
Windows消息队列 消息队列是Windows系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当中.同时,如果队列不是空的 ...
- LeetCode OJ:Implement Queue using Stacks(栈实现队列)
比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...
- 剑指offer--49.矩阵中的路径
时间限制:1秒 空间限制:32768K 热度指数:156998 题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩 ...