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 ...
随机推荐
- Excel中countif函数的使用方法
1.countif函数的含义 在指定区域中按指定条件对单元格进行计数(单条件计数) 建议和countifs函数结合起来学习,可以观看小编的经验Excel中countifs函数的使用方法. END 2. ...
- mysql架构图
整体架构图 访问控制图
- shell 进制转换
包括: i.任意进制转化为十进制((num=base#number)) [base和number必须一致,是同一种进制] ii.十进制转化为任意进制`echo "obase=进制;值&quo ...
- linux下的一些操作命令
1.切换到root账号下: su root 输入密码: 2.修改root账号密码: sudo passwd root 输入密码: 3.cat用法: 查看文件内容 cat 文件名 创建文件 ...
- 应急响应--记录一次漏洞紧急处理中意外发现的挖矿木马(Shiro反序列化漏洞和ddg挖矿木马)
背景 某公司线上服务器意外发现一个Apache Shiro 反序列化漏洞,可以直接GetShell.出于做安全的谨慎,马上出现场应急,确认漏洞.该漏洞存在在cookie字段中的rememberMe字段 ...
- Memcached存Session数据、访问安全性、使用场景总结(3)
最近做了一个单点登录SSO,登陆后的凭证放到Memcached令牌放到Cookies:但是用户经常掉线,开发环境和测试却没有这个问题,最后从Memcached找到原因. Memcached概念.作用. ...
- Core Data 迁移与版本管理
原文 http://chun.tips/blog/2014/11/28/core-data-ban-ben-qian-yi-jing-yan-zong-jie/ 主题 Core DataiOS开发 ...
- CentOS 6.8下更改Apache默认网站安装目录
首先,当我们搭建好LAMP环境后,Apache服务器默认的网站安装目录是/var/www/html 然而我们搭建项目时为了方便,我们是按照自己的意愿更改网站目录的 现在假设,按照意愿设定的网站目录为/ ...
- Equinox P2 介绍(一)Getting Start
一直觉得 Equinox 的 P2 是个神秘的东西,常常使得 Eclipse 或 Equinox 表现出一些奇怪的行为,于是找来官方文档读一读,试图更好地理解与应用 Equinox . 官方文档很多, ...
- 那些你觉得堪称神兵利器的 Chrome 插件
子曾曰:"工欲善其事,必先利其器.居是邦也."--语出<论语·卫灵公>:其后一百多年,荀子也在其<劝学>中倡言道:"吾尝终日而思矣,不如须臾之所学 ...