第二题 算法 给你一个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的更多相关文章

  1. 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 ...

  2. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  3. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  4. G面经prepare: Pattern Match

    设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...

  5. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  6. 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 ...

  7. 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 ...

  8. G面经Prepare: Valid Preorder traversal serialized String

    求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...

  9. G面经prepare: Chucked Palindrome

    给定一个字符串,找出最多有多少个chunked palindrome, 正常的palindrome是abccba, chunked palindrome的定义是:比如volvo, 可以把vo划分在一起 ...

随机推荐

  1. html 符号大全

    ░ ▒ ▬ ♦ ◊ ◦ ♠ ♣ ▣ ۰•● ❤ ●•۰► ◄ ▧ ▨ ♨ ◐ ◑ ↔ ↕ ▪ ▫ ☼ ♦ ♧♡♂♀♠♣♥❤☜☞☎☏⊙◎ ☺☻☼▧▨♨◐◑↔↕▪ ▒ ◊◦▣▤▥ ▦▩◘ ◈◇♬♪♩♭♪の ...

  2. oracle创建dblink问题

    1.如果在本地"D:\oracle\product\11.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora"中设置 服务器数据库连接,必须再服务器设置 ...

  3. java effective 读书笔记

    java effective 读书笔记 []创建和销毁对象 静态工厂方法 就是“封装了底层 暴露出一个访问接口 ” 门面模式 多参数时 用构建器,就是用个内部类 再让内部类提供构造好的对象 枚举 si ...

  4. Freemarker的第二次使用~list的元素是数组

    在上次初次使用Freemarker作为模版后,我再次使用它.这次的需求是: xml文档的某个节点的属性A和其一个子节点的某个属性B有关联,属性B的值需要随着属性A的值变化.而属性A的值有4个值,现在需 ...

  5. 一个Delphi7的BUG

    combobox有个属性DropDownCount可以控制显示的下拉数量, 但是 在Delphi7中, TCombobox或者任何从TCustomComboBox继承下来的类, 在windows7环境 ...

  6. 蓝牙BLE ATT剖析(二)-- PDU

    一.Error Handling Error Response The Error Responseis used to state that a given request cannot be pe ...

  7. 如何将cmd中命令输出保存为TXT文本文件

    在使用Windows 中的cmd.exe工具时,有时候我们想要把我们的输入命令及结果保存起来, 但是用复制的方法过于麻烦:有时输出数据条数过大,会造成内容自动滚出屏幕,无法阅读,我们可将命令运行的结果 ...

  8. 【Android测试】【随笔】模拟双指点击

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5258660.html 手势 看到这个标题,很多人会想一想 ...

  9. 规则html表单对象赋值

    function grid_load_callback(data, status) {            if (data.rows.length > 0)            {     ...

  10. Magento: How to reset admin pssword

    Magento: How to reset admin pssword If you forget your admin password for Magento and you can’t reme ...