[LeetCode]55.Jump Game

题目

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.

思路

用distance记录当前i之前跳的最远距离。如果distance< i,即代表即使再怎么跳跃也不能到达i。当到达i时,A[i]+i,代表从i能跳最远的距离。max(distance,A[i]+i)代表能到达的最远距离。

代码

    /*--------------------------------------------
    *   日期:2014-02-28
    *   作者:SJF0115
    *   题目: 55.Jump Game
    *   网址:https://oj.leetcode.com/problems/jump-game/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ------------------------------------------------*/
    #include <iostream>
    #include <vector>
    using namespace std;

    class Solution {
    public:
        bool canJump(int A[], int n) {
            // 记录历史跳最远距离
            int distance = 0;
            for(int i = 0;i < n && i <= distance;++i){
                // A[i]+i当前跳最远距离 distance为i之前跳最远距离
                distance = max(A[i]+i,distance);
            }//for
            if(distance < n-1){
                return false;
            }//if
            return true;
        }
    };

    int main(){
        Solution solution;
        //int A[] = {3,2,1,0,4};
        //int A[] = {2,3,1,1,4};
        int A[] = {2,3,0,1,4};
        cout<<solution.canJump(A,5)<<endl;
        return 0;
    }

运行时间

时间: 2024-09-20 08:42:00

[LeetCode]55.Jump Game的相关文章

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode]135.Candy

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II

[LeetCode] My Calendar II 我的日历之二

Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event will not cause a triple booking. Your class will have one method, book(int start, int end). Formally, this represents a booking on the half open interv

代码分析-leetcode:Scramble String问题

问题描述 leetcode:Scramble String问题 题目:Given a string s1 we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = ""great"": great / gr eat / / g r e at /

c++-leetcode Median of Two Sorted Arrays

问题描述 leetcode Median of Two Sorted Arrays class Solution { public: double findMedianSortedArrays(vector& nums1, vector& nums2) { if(nums1.size()==0){ if(nums2.size()%2==0)return ((double)nums2[nums2.size()/2]+nums2[nums2.size()/2-1])/2.0; else ret

[LeetCode]61.Rotate List

[题目] Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. [题意] 给定一个链表,向右旋转k个位置,其中k是非负的. [分析] 先遍历一遍,得出链表长度 len,注意 k 可能大于

[LeetCode]91.Decode Ways

题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 - 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encode