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