G面经prepare: Jump Game Return to Original Place
第二题 算法 给你一个arr 返回 T 或者 F
arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素
比如[1,1,1,1,1,1] => T
[0,1,1,1,1,1]=> F
[7, 5, 2, 3] => F
[2,2,3,1] => T. From 1poi
scan array once, get the index that can be reached by each array element for one step. check if every index can be reached by using HashSet
package JumpGameReturnToOrigin;
import java.util.*; public class Solution {
public boolean check(int[] arr) {
HashSet<Integer> set = new HashSet<Integer>();
for (int i=0; i<arr.length; i++) {
set.add((i + arr[i]) % arr.length);
}
if (set.size() == arr.length) return true;
else return false;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
boolean res = sol.check(new int[]{1,1,1,1,1,1,1});
if (res) System.out.println("True");
else System.out.println("False");
} }
G面经prepare: Jump Game Return to Original Place的更多相关文章
- G面经prepare: Set Intersection && Set Difference
求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...
- G面经prepare: Data Stream Average
给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...
- G面经prepare: Android Phone Unlock Pattern
1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...
- G面经prepare: Sort String Based On Another
Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...
- G面经Prepare: Valid Preorder traversal serialized String
求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...
- G面经prepare: Chucked Palindrome
给定一个字符串,找出最多有多少个chunked palindrome, 正常的palindrome是abccba, chunked palindrome的定义是:比如volvo, 可以把vo划分在一起 ...
随机推荐
- SELECT 'www' = 0; 1
http://dev.mysql.com/doc/refman/5.7/en/type-conversion.html MySQL 5.7 Reference Manual / Functions ...
- single-chip microcomputer Microcontroller 单片机 单片微型计算机 微控制器
https://zh.wikipedia.org/wiki/单片机 单片机,全称单片微型计算机(英语:single-chip microcomputer),又称微控制器(microcontroller ...
- JS中基本window对象操作
---恢复内容开始--- 一.使用window中的属性时 window.属性,直接跟属性名.而调用window的函数时 window.hanshu(): 要在其函数名后面加括号. 二.windo ...
- PureBasic 读取文件中一行的两个数据例子
, "Test1.txt") ; if the file could be read, we continue... , "Test2.txt") ) = ; ...
- 关于xampp使用不同端口的虚拟机
1.打开apache/conf/httpd.conf文件加入listen 8080(监听的端口号) 然后加入 # Virtual hostsInclude "conf/extra/httpd ...
- java分形树
import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; /** * * @ ...
- [LeetCode] Maximal Rectangle(good)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- Spark Programming--Actions
first def first(): T first返回RDD中的第一个元素,不排序. 例子: count def count(): Long count返回RDD中的元素数量 例子: reduce ...
- magento去掉小数点后面的0
<?php echo $_product->getPrice()?> PHP number_format() 函数 <?php echo number_format($_p ...
- UITabBar 设置字体的颜色(选中状态/正常状态)setTitleTextAttributes
UITabbar有个setTintColor这个方法,可以理解为,高亮的时候,或者点击后的颜色设置. UITabBarItem有个setTitleTextAttributes的方法,是用来设置字体的颜 ...