php开源网站,做雇主品牌的网站,设计网站平台风格,mysql8 wordpress思路#xff1a; 从后往前遍历#xff0c;遇到元素为0时#xff0c;记录对应的下标位置#xff0c;再向前遍历元素#xff0c;看最大的跳跃步数能否跳过0的位置#xff0c;不能则继续往前遍历
代码#xff1a;
class Solution {
public:bool canJump(vectorint 从后往前遍历遇到元素为0时记录对应的下标位置再向前遍历元素看最大的跳跃步数能否跳过0的位置不能则继续往前遍历
代码
class Solution {
public:bool canJump(vectorint nums) {if(nums.size()1) return true;int endnums.size()-2;//最后一个下标 的前一个int i0;bool flag false;int temp0;//记录nums[i]0的下标向前遍历看是否能跳过该位置for(int iend;i0;i--){if(flag){//有nums[i]0if(nums[i] temp-i){//判断0的前面的元素能否跳过0的位置flagfalse;temp0;}}else if(nums[i]0) {tempi;//记录元素0的位置flagtrue;}else continue;}//最后判断num[0]的元素值if(nums[0] temp) return true;else return false;}};