Twitter OA prepare: Visit element of the array

分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数
public int visiting(int[] A, int N) {
if (A==null || A.length==0) return 0;
int cur = 0;
int count = 0;
boolean[] visited = new boolean[N];
while (cur>=0 && cur<A.length && !visited[cur]) {
visited[cur] = true;
cur = cur + A[cur];
count++;
}
return N-count;
}
Twitter OA prepare: Visit element of the array的更多相关文章
- Twitter OA prepare: Equilibrium index of an array
Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to ...
- Twitter OA prepare: Two Operations
准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shor ...
- Twitter OA prepare: Flipping a bit
You are given a binary array with N elements: d[0], d[1], ... d[N - 1]. You can perform AT MOST one ...
- Twitter OA prepare: even sum pairs
思路:无非就是扫描一遍记录奇数和偶数各自的个数,比如为M和N,然后就是奇数里面选两个.偶数里面选两个,答案就是M(M-1)/2 + N(N-1)/2
- Twitter OA prepare: K-complementary pair
2sum的夹逼算法,需要sort一下.本身不难,但是tricky的地方在于允许同一个数组元素自己跟自己组成一个pair,比如上例中的[5, 5].而且数组本身就允许值相等的元素存在,在计算pair时, ...
- Twitter OA prepare: Anagram is A Palindrome
Algorithm: Count the number of occurrence of each character. Only one character with odd occurrence ...
- Twitter OA prepare: Rational Sum
In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q ...
- check the element in the array occurs more than half of the array length
Learn this from stackflow. public class test { public static void main(String[] args) throws IOExcep ...
- Kth Largest Element in an Array
Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...
随机推荐
- iframe内点击a标签禁止滚动到顶部
在iframe内加载的表中,操作下的按钮用a标签布局,但是会出现一个非常不好的体验,就是当页面有滚动条的时候,点击a标签,列表会自动滚动到顶部. 首先看我的a标签: <a href=" ...
- linux alternatives命令详解
alternatives是Linux下的一个功能强大的命令.只能在root权限下执行.如系统中有几个命令功能十分类似,却又不能随意删除,那么可以用 alternatives 来指定一个全局的设置. a ...
- ASP.NET Request.Cookies获取某个Cookie的奇怪问题
公司的某个产品依赖一个Cookie的值,发现在某些情况下即使Request附带了该Cookie(通过Fiddler2监控),服务器端通过HttpContext的Request.Cookies访问该Co ...
- Unity3D笔记 愤怒的小鸟<七> 小鸟群准备动画
要实现的目标: 1.3只小鸟初始动画 2.完善代码slingShot.js 3.完善代码BirdMoving.js 1.实现3个准备动画:Unity3D内置的动画管理器 1.1.先选择GameObje ...
- Java秒杀简单设计四:service层设计
接上一篇 https://www.cnblogs.com/taiguyiba/p/9829191.html 封装了系统传递的数据类和异常类 本文继续设计service层设计: 1.SeckillSe ...
- thinkCMF----如何写标签
ThinkCMF写标签的地方:
- POJ-1414 Life Line (暴力搜索)
Life Line Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 855 Accepted: 623 Description L ...
- 【技术开放日】msup携手HPE揭秘全球测试中心背后的12条技术实践
保证软件产品质量是软件测试永恒的目标. 以控制为出发点的传统IT时代正在快速的向以激活生产力为目的的移动互联时代转变.这不仅是技术的升级,更是思想意识的巨大变革,也对软件技术的发展带来的更高的要求和更 ...
- zero-shor learning 数据集
OSR数据集下载地址: http://people.csail.mit.edu/torralba/code/spatialenvelope/ Relative Attributes Marr Priz ...
- POJ 2342 - Anniversary party - [树形DP]
题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...