📜  跳跃序列 java 布尔值 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:35.505000             🧑  作者: Mango

代码示例1
// Program for Jolly Jumper Sequence
import java.util.*;
 
class GFG
{
 
// Function to check whether given sequence
// is Jolly Jumper or not
static boolean isJolly(int a[], int n)
{
    // Boolean vector to diffSet set of differences.
    // The vector is initialized as false.
    boolean []diffSet = new boolean[n];
 
    // Traverse all array elements
    for (int i = 0; i < n - 1 ; i++)
    {
        // Find absolute difference
        // between current two
        int d = Math.abs(a[i] - a[i + 1]);
 
        // If difference is out of range or repeated,
        // return false.
        if (d == 0 || d > n - 1 ||
            diffSet[d] == true)
            return false;
 
        // Set presence of d in set.
        diffSet[d] = true;
    }
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
    int a[] = {11, 7, 4, 2, 1, 6};
    int n = a.length;
    if(isJolly(a, n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by Rajput-Ji