55. Jump Game leetcode
55. Jump Game
- Total Accepted: 95819
- Total Submissions: 330538
- Difficulty: Medium
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
贪心算法:正向
class Solution {
public:
bool canJump(const vector<int>& nums) {
; //reach the rightest position
; i < reach && reach < nums.size(); ++i)
reach = max(reach, i + + nums[i]);
return reach >= nums.size();
}
};
//贪心算法,逆向,比正向稍费时
class Solution {
public:
bool canJump (const vector<int>& nums) {
if (nums.empty()) return true;
//
;
; i >= ; --i)
if (i + nums[i] >= left_most)
left_most = i;
;
}
};
bool canJump(int* nums, int numsSize) {
;
int flag = true;
; i < numsSize ; ++i) {
int j = i;
for (; j <= idx + nums[idx]; ++j) {
if(idx + nums[idx] <= j + nums[j]){
idx = j;
flag = true;
break;
}else {
flag = false;
}
}
}
)
return true;
return false;
}
9月份人人net来某校宣讲,其中一个编程题即为此题.
#include <iostream>
#include <string.h>
#include <mcheck.h>
using namespace std;
bool test(int *arr, int size){
;
) {
//cout << idx << endl;
) return false;
idx += arr[idx];
//cout << idx << endl;
}
) return true;
else
return false;
}
int main(int argc, char* argv[])
{
,,,,,};
]);
bool flag = test(arr, len);
cout << flag << endl;
,,,,,};
]);
cout << test(a, lena) << endl;
;
}
55. Jump Game leetcode的更多相关文章
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- Jump Game - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Jump Game - LeetCode 注意点 解法 解法一:贪心算法,只关注能到达最远距离,如果能到达的最远距离大于结尾说明能到达,否则不能.并且如果 ...
- 刷题55. Jump Game
一.题目说明 题目55. Jump Game,给定一组非负数,从第1个元素起,nums[i]表示你当前可以跳跃的最大值,计算能否到达最后一个index.难度是Medium. 二.我的解答 非常惭愧,这 ...
- [LeetCode] 55. Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Jump Game 的三种思路 - leetcode 55. Jump Game
Jump Game 是一道有意思的题目.题意很简单,给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置. 比如: A = ...
- LeetCode 55. Jump Game (跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【一天一道LeetCode】#55. Jump Game
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
随机推荐
- select根据text选择项与select其它操作
// 6.设置select中text="paraText"的第一个Item为选中 function jsSelectItemByValue(objSelect, objItemTe ...
- BZOJ1212——L语言
题目大意:每一个字符串都可以分解成一些个单词组成,现在给你一些单词,再给你一个字符串, dp吧,设f[i]为从0开始,到i结束的字符串前缀是否可以被分解,因为单词长度很小,所以,这就T了, (什么逻辑 ...
- 剑指Offer 链表中倒数第k个结点
题目描述 输入一个链表,输出该链表中倒数第k个结点. 思路: 法1:设置2个指针p,q.p先移动k次,然后pq同时后移,p到链表尾尾的时候,q指向倒数第k个节点. 注意://需要考虑k=0,以 ...
- python查找并删除相同文件-UNIQ File-wxPython版本
今天用wxPython做了一个GUI程序,我称之为UNIQ File,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有 ...
- ios中json解析出现的null问题
http://my.oschina.net/iq19900204/blog/408034 在iOS开发过程中经常需要与服务器进行数据通讯,Json就是一种常用的高效简洁的数据格式. 问题现象 但是几个 ...
- Vim保存只读模式下的修改
用 vim 打开没有写权限的文件而忘记用 sudo 时,文件变成 read-only这时候用 :w!, SHIFT+ZZ,或者 qw! 都无法保存 :w !sudo tee % :w : Write ...
- ios Tabbar Item 的图标
1,tabBarItem图片的推荐尺寸和最大支持尺寸 下面是标签栏(UITabBar)中tab按钮图标分别在1x.2x.3x下不会压缩变形的尺寸: @1x : 推荐 25 x 25 (最大: 48 ...
- ios 在程序中使用iCloud
注意,这里说的使用icould不是用icloud进行系统备份,那个功能不需要我们写代码,备份到icloud的东西我们也不能操作.我们指的是以下这3种icloud使用方法: 这里有3中使用方法, Key ...
- C# 读写十六进制bin 文件
读一个十六进制的bin文件,在bin文件添加四行头,生成新的bin文件.bin文件可以用vs打开查看. using System; using System.Collections.Generic; ...
- VIM常用操作总结
VIM常用的模式网上说法不太一样,但是大同小异,这里大致分一下,便于后面总结 1.一般模式 默认打开文件后的模式,这个时候只能移动光标,不能编辑 2.输入模式 一般模式按下i,就可以进入输入模式,输入 ...