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 ...
随机推荐
- LeetCode 79 Word Search(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
- Nginx写IO占用高故障处理
文章来源:<https://www.centos.bz/2015/04/handle-nginx-write-io-problem/> 故障现象 突然收到一台服务器负载过高告警,紧接着网站 ...
- windows中安装pip,setuptools,django等
1,安装Python3.6 (下载exe文件,双击安装) 注意设置环境变量,让Python的在任意位置都可以执行 .Python 下载地址:https://www.python.org/do ...
- 解决启动Distributed Transaction Coordinator服务出错的问题
解决启动Distributed Transaction Coordinator服务出错的问题 "Windows 不能在 本地计算机 启动 Distributed Transaction ...
- Html5游戏框架createJs组件--EaselJS(一)
现在html5小游戏越来越火爆了,由于公司业务的需要,也开发过几款微信小游戏,用canvas写的没有利用什么框架,发现性能一直不怎么好,所以楼主就只能硬着头皮去学习比较火的Adobe公司出的Creat ...
- easyui---基础组件:dialog
依赖下面两个组件 window linkbutton linbutton组件:就是超链接变成按钮 $(function(){ $("#linkbuttonid").linkbutt ...
- PAT甲1031 Hello World for U【字符串】
1031 Hello World for U (20 分) Given any string of N (≥5) characters, you are asked to form the chara ...
- OpenCV学习笔记之课后习题练习3-5
OpenCV学习笔记之课后习题练习2-5 练习使用感兴趣区域(ROI).创建一个210*210的单通道图像并将其归0.在图像中使用ROI和cvSet()建立一个增长如金字塔状的数组. 参考博文:www ...
- HDFS体系结构(NameNode、DataNode详解)
hadoop项目地址:http://hadoop.apache.org/ NameNode.DataNode详解 (一)分布式文件系统概述 数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配 ...
- JavaScript 包管理工具npm 和yarn 对比