找终点
描述
一个正整数数组设为 nums,最大为 100 个成员。求从第一个成员开始正好走到数
组最后一个成员所使用的最小步骤数
要求:
1.第一步必须从第一元素起,且 1<=第一步步长<len/2 (len 为数组长
度)
2.从第二步开始只能以所在成员的数字走相应的步数,不能多不能少,如果目标
不可达返回-1,只输出最小的步骤数量
3.只能向数组的尾部走不能向回走
输入描述:有正整数数组,空格分割
输出描述 :最小步数,不存在输出-1
例一:
7 5 9 4 2 6 8 3 5 4 3 9
2
例二:
1 2 3 7 1 5 9 3 2 1
-1
法一
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
String[] strs = str.split("\\s+");
int numStart = Integer.parseInt(strs[0]);
int numStartMax = strs.length / 2;
int size = strs.length;
int count = -1;
if (size == numStart) {
count = 1;
} else {
for (int i = 0 ; i < numStartMax ; i++) {
if (Integer.parseInt(strs[i]) + numStart + 1 == size) {
count = 2;
}
}
}
System.out.println(count);
}
}
}