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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  4. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 《Cracking the Coding Interview》——第14章:Java——题目5

    2014-04-26 19:06 题目:Java中的对象反射机制是什么?有鼠么用? 解法:完全不了解,因为java编程经验太少,完全没用过.查了一些资料后,感觉反射机制是个强大并需要边用边体会的强大工 ...

  8. 《Cracking the Coding Interview》——第14章:Java——题目4

    2014-04-26 19:02 题目:解释下C++里模板和java里泛型的区别? 解法:我很少用java,属于连语法都不过关的程度.所以这个题还真没法详细答,查了些资料以后写了以下几点. 代码: / ...

  9. 《Cracking the Coding Interview》——第14章:Java——题目3

    2014-04-26 18:59 题目:final.finally.finalize有什么区别? 解法:烂大街之java语法题.此题被多少公司考过我不知道,反正我确实遇见过一次了. 代码: // 14 ...

随机推荐

  1. C++指针、引用、const

    ; int *p = &a; //定义指针p指向变量a *p = ; //*p代表a的值 ; p = &b; //p指向变量b *p = ; //此时*p代表b的值 ] = {,,}; ...

  2. MySQL数据库实验五:数据更新

    实验五   数据更新 一.实验目的 掌握数据更新操作的用法. 二.实验环境 三.实验示例 1.往基本表SC中插入元组. ①    INSERT INTO S(S#,SNAME,AGE,SEX) VA ...

  3. less通用pc移动库

    // less 文件 (移动端通用less文件) // 作者 marchen // 时间 2014/9/1 // 协议 MIT // 只考虑webkit内核手机浏览器和火狐内核浏览器 // 自定义le ...

  4. ELF文件中section与segment的区别

    http://blog.csdn.net/joker0910/article/details/7655606 1. ELF中的section主要提供给Linker使用, 而segment提供给Load ...

  5. 【转】android中的Style与Theme

    Android默认情况下提供了一些实用的主题样式,比如说Theme.Dialog可以让你的Activity变成一个窗口风格,而Theme.Light则让你的整个Activity具有白色的背景,而不是黑 ...

  6. HTTP head meta

    HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...

  7. 第44章 MPU6050传感器—姿态检测—零死角玩转STM32-F429系列

    第44章     MPU6050传感器—姿态检测 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.co ...

  8. JS无限添加HTML到指定位置

    用JS把HTML添加到指定位置有两种写法,一种是用字符串,一种是用javascript中的方法 第一种: 用字符串写 <h2>利用JS无限添加一个相同部分</h2> <h ...

  9. iOS 多线程 之 GCD(大中枢派发)(二)

    本文接着上一篇讲.主要讲:dispatch_source. dispatch_source主要用户监听事件,可以监听如下事件 DISPATCH_SOURCE_TYPE_DATA_ADD DISPATC ...

  10. Servlet学习笔记03——什么是DAO?

    1.案例 (1)添加用户 step1.建表 create table t_user( id int primary key auto_increment, username varchar(50) u ...