谷歌面试题(一)
来源:互联网 发布:java的职业规划 编辑:程序博客网 时间:2024/05/16 15:11
大家都知道谷歌是科技界的领头羊,他的面试题肯定有一些参考意义,所以本人就选了几道代表性面试题。给自己或其他人一些帮助
原题:
// Given two 1d vectors, implement an iterator to return their elements alternately.// For example, given two 1d vectors:// v1 = [1, 2]// v2 = [3, 4, 5, 6]// By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 3, 2, 4, 5, 6].// Follow up: What if you are given k 1d vectors? How well can your code be extended to such cases?/** * Your ZigzagIterator object will be instantiated and called as such: * ZigzagIterator i = new ZigzagIterator(v1, v2); * while (i.hasNext()) v[f()] = i.next(); */
答案:
public class ZigzagIterator { private Iterator<Integer> i; private Iterator<Integer> j; private Iterator<Integer> temp; public ZigzagIterator(List<Integer> v1, List<Integer> v2) { i = v1.iterator(); j = v2.iterator(); } public int next() { if(i.hasNext()) { temp = i; i = j; j = temp; } return j.next(); } public boolean hasNext() { return i.hasNext() || j.hasNext(); }}
阅读全文
0 0
- 谷歌面试题(一)
- 试题一
- 试题一
- 试题一
- 【面试题】五道面试题一
- 面试题(一)
- java面试题一
- 华为试题一
- 面试题一
- 面试题一
- C++试题收集(一)
- 面试题(一)数组
- SQL面试题 (一)
- 上海贝尔面试题一
- Java面试题一
- java面试题一
- java面试题一
- SCJP试题一
- 使用graphviz画二叉树
- IntelliJ says 'cannot run program '/path/to/tomcat/bin/catalina.sh' error=13 permission denied
- Django配置资源文件上传
- R_条件分割图
- Kubernetes集群中部署Node节点
- 谷歌面试题(一)
- httpclient使用实例
- Docker学习系列(一):windows下安装docker
- 3.1.11生产者消费者(n:n)
- C++笔记——动态内存分配
- 谷歌面试题(2)
- JSP学习笔记
- R_等高图
- 2017-10-15离线赛