#include <stdio.h>#include <stdlib.h>#include <stdbool.h>
static inline intmax(int a,int b){return a > b ? a : b;}
static bool canJump(int*nums,int numsSize){int i, pos =0;for(i =0; i < numsSize -1; i++){
___________________
pos =max(i + nums[i], pos);}return pos >= numsSize -1;}int main(int argc, char **argv){int i, count = argc -1;int*nums = malloc(count * sizeof(int));for(i =0; i < count; i++){
nums[i]= atoi(argv[i +1]);}
printf("%s\n", canJump(nums, count) ? "true":"false");return0;}
2,程序代码
# -*- coding: utf-8 -*-"""
Created on Tue Dec 21 09:05:28 2021
Function:
@author: 小梁aixj
"""classSolution:defcanjump(self, nums):
length =len(nums)
begin = length -1for i inreversed(range(length -1)):if i + nums[i]>= begin:
begin = i
returnnot begin
#%%
s = Solution()print(s.canjump(nums =[2,3,1,1,4]))#true
3,运行结果
二、串联所有单词的子串
1,程序简介
给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。
示例 1:
输入: s = “barfoothefoobarman”, words = [“foo”,“bar”]