《Cracking the Coding Interview》——第14章:Java——题目6
2014-04-26 19:11
题目:设计一个循环数组,使其支持高效率的循环移位。并能够使用foreach的方式访问。
解法:foreach不太清楚,循环移位我倒是实现了一个,用带有偏移量的数组实现。修改元素不一定能做到O(1)时间,但循环移位能在O(1)时间解决。不得不说,用不熟的语言写面试题,很难~~~
代码:
// 14.6 Implement a circular array, which allows easy rotation and array access.
// Combination of a vector and a rotation index.
import java.io.PrintStream;
import java.util.Vector; public class CircularArray<T> {
public int rotateIndex;
public Vector<T> v; public CircularArray() {
v = new Vector<T>();
rotateIndex = 0;
} public T elementAt(int index) {
if (index < 0 || index > v.size() - 1) {
throw new ArrayIndexOutOfBoundsException();
}
return v.elementAt((index + rotateIndex) % v.size());
} public void add(T val) {
if (v.size() > 0) {
v.insertElementAt(val, (rotateIndex + v.size() - 1) % v.size() + 1);
if (rotateIndex > 0) {
++rotateIndex;
}
} else {
v.insertElementAt(val, 0);
}
} public void removeElementAt(int index) {
if (rotateIndex > 0) {
if (index < 0 || index > v.size() - 1) {
throw new ArrayIndexOutOfBoundsException();
}
int tmp = v.size();
v.removeElementAt((index + rotateIndex) % v.size());
if (index >= tmp - rotateIndex && index <= tmp - 1) {
--rotateIndex;
}
} else {
v.removeElementAt(index);
}
} public void rotate(int index) {
if (v.size() == 0) {
return;
}
index = (v.size() - (v.size() - index) % v.size()) % v.size();
rotateIndex = (rotateIndex + index) % v.size();
} public static void main(String[] args) {
CircularArray<Integer> c = new CircularArray<Integer>();
PrintStream cout = System.out; c.add(3);
c.add(4);
cout.println(c.v);
c.add(7);
cout.println(c.v);
c.rotate(2);
c.add(12);
c.add(25);
cout.println(c.v);
c.rotate(-1);
c.add(77);
cout.println(c.v);
c.removeElementAt(2);
cout.println(c.v);
cout.println(c.elementAt(0));
}
}
《Cracking the Coding Interview》——第14章:Java——题目6的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第14章:Java——题目5
2014-04-26 19:06 题目:Java中的对象反射机制是什么?有鼠么用? 解法:完全不了解,因为java编程经验太少,完全没用过.查了一些资料后,感觉反射机制是个强大并需要边用边体会的强大工 ...
- 《Cracking the Coding Interview》——第14章:Java——题目4
2014-04-26 19:02 题目:解释下C++里模板和java里泛型的区别? 解法:我很少用java,属于连语法都不过关的程度.所以这个题还真没法详细答,查了些资料以后写了以下几点. 代码: / ...
- 《Cracking the Coding Interview》——第14章:Java——题目3
2014-04-26 18:59 题目:final.finally.finalize有什么区别? 解法:烂大街之java语法题.此题被多少公司考过我不知道,反正我确实遇见过一次了. 代码: // 14 ...
随机推荐
- 【转载】#330 - Derived Classes Do Not Inherit Constructors
A derived class inherits all of the members of a base class except for its constructors. You must de ...
- 使用Axure管理团队项目图文教程 团队协作操作步骤
Axure RP团队版和企业版都支持团队协作,可以创建和管理团队项目,即多人共同创作一个原型.本文通过图文教程的形式,讲解了如何基于Axure Share服务创建和管理团队项目.因为Axure Sha ...
- 前端高质量知识(三)-JS变量对象详解
在JavaScript中,我们肯定不可避免的需要声明变量和函数,可是JS解析器是如何找到这些变量的呢?我们还得对执行上下文有一个进一步的了解. 在上一篇文章中,我们已经知道,当调用一个函数时(激活), ...
- 立体最短路径,广搜(POJ2251)
题目链接:http://poj.org/problem?id=2251 参考了一下大神们的解法.也有用深搜的.然而,之前不久看到一句话,最短路径——BFS. 参考:http://blog.csdn.n ...
- ORA-00392: log 4 of thread 2 is being cleared, operation not allowed
alter database open resetlogs或者 alter database open resetlogs upgrade报错:ORA-00392 在rman restore 还原数 ...
- vector的几种初始化和遍历
随着C++11标准的出现,vector出现了新的初始化和遍历用法,但是vs2010和较高版本并没有能完全支持C++11标准,所以我就将它的所有的用法归纳了一下. vector的初始化 vector基本 ...
- python_5_password
#1.python2中raw_input与python3中的input是相同的,python2中也有input但是别用(不好用,忘记它) #密码是明文的 username=input("us ...
- 使用QT开发GoogleMap瓦片显示和下载工具(1)——QT开发环境准备
由于是第一次使用qt,光是QT的安装和调试就费了好大功夫,汗一个,下面记录下过程和遇到的问题的解决方法吧. 下载QT 直接Google搜索“QT”,进入官网http://qt-project.org/ ...
- centos安装django
1.如果默认安装的是python2.6,先升级至python2.7 参考:http://www.cnblogs.com/tiger2soft/p/5677843.html 2.安装pip 先下载get ...
- List<T>转换为二维数组
public <T> Object[][] toArrays(List<T> data){ Object[][] o=new Object[data.size()][20]; ...