iT邦幫忙

2021 iThome 鐵人賽

DAY 16
0
AI & Data

想到甚麼打甚麼系列 第 16

找LeetCode上簡單的題目來撐過30天啦(DAY16)

  • 分享至 

  • twitterImage
  •  

ok,今天挑戰同一張證照第三次失敗了,有夠難過的,但至少分述有越來越接近啦,再接再厲囉

題號:55 標題:Jump Game 難度:Medium

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.

Example 1:
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:
Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

Constraints:
• 1 <= nums.length <= 104
• 0 <= nums[i] <= 105

class Solution {
    public boolean canJump(int[] nums) {
        int i=0,j=0,k=0;
        int[] temp = new int[nums.length+1];
        ArrayList<Integer> arrlist = new ArrayList<Integer>();
        
        for(i=0;i<nums.length;i++){
            arrlist.add(nums[i]);
        }
        if(!arrlist.contains(0)){
            return true;
        }
        
        if(nums[0]==0){
            if(nums.length==1){
                return true;
            }else{
                return false;
            }
        }
        int count0=0,chk0=0;
        for(i=nums.length-1;i>=0;i--){
            if(nums[i]==0){
                count0++;
                chk0=1;
               
            }else if(chk0==1 && nums[i]!=0 ){
                k=0;
                for(j=i;j>=0;j--){
                    if(nums[j]>(count0+k)){
                        chk0=0;
                        count0=0;
                        break;
                    }else if(count0+i+1==nums.length && nums[j]==(count0+k)){
                        chk0=0;
                        count0=0;
                        break;
                    }else if(j==0){
                        return false;
                    }
                    k++;
                }
            }
        }
        
        return true;
    }
}

DAY16心得
證照跟鐵人賽還有上班三頭燒,我已經再用測資測試程式了,看看我把CASE一個個判斷出來
1.沒有0的一定到的了=>TRUE
2.第一個值為0,一定到不了=>FALSE
3.接下來就是判斷連續幾個0前面的值要能夠跨過去,能夠跨過去就繼續搜尋有沒有0,跨不過去就代表到不了=>FALSE


上一篇
找LeetCode上簡單的題目來撐過30天啦(DAY15)
下一篇
找LeetCode上簡單的題目來撐過30天啦(DAY17)
系列文
想到甚麼打甚麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言