调节-功能改变



我可以通过这个简单的测试,问题是发现函数中的错误并对其进行调整,使其正常工作。

传递给函数的数组是{1,3,3},K =2。如果在数组中没有找到K,该函数应该返回false,但它返回true。你只被允许修改2行代码。

public static bool solution(int[] A, int K)
    {
        int n = A.Length;
        for (int i = 0; i < A.Length - 1; i++)
        {
            if (A[i] + 1 < A[i + 1])
                return false;
        }
        if (A[0] == 1 && A[n - 1] != K)
            return false;
        else
            return true;
    } 

在正常情况下,我只会重写这个函数,因为我知道它应该做什么:

public static bool solution(int[] A, int K)
    {
        int n = A.Length;
        int count = 0;
        for (int i = 0; i < A.Length - 1; i++)
        {
            if (A[i] == K)
            {
                count++;
            }
        }
        if (count == 0)
            return false;
        else
            return true;
    }

我相信这是一个解决方案。

public static bool solution(int[] A, int K)
{
    int n = A.Length;
    for (int i = 0; i < A.Length - 1; i++)
    {
        if (A[i] + 1 < A[i + 1])
            return false;
    }
    if (A[0] == 1 || A[n - 1] != K)//Change here && to ||
        return false;
    else
        return true;
} 

好了,伙计们,我知道了!虽然不是100%,但条件是2行,我改变了3行,所以如果你看到我们如何将其减少到两行,射击:

public class Solution {
public boolean solution(int[] A, int K) {
    int n = A.length;
    for (int i = 0; i < n - 1; i++) {
        if (A[i] + 1 == A[i + 1] && A[i]+1 == K){ // for wouldn't stop at K, now it does.
            // System.out.println("true"); for testing
            return true;
        }
    }
    if (A[0] != 1 || A[n - 1] != K) { //changed && to || for 0 value in array if we don'tcan't have 0 in array then we can keep && and this solution would meet req of 2 lines changed
        // System.out.println("false"); for testing
        return false;
    } else {
        // System.out.println("true"); for testing
        return true;
    }
}

正确答案下面的代码:

public boolean solution(int[] A, int k) {
    int n = A.length;
    for (int i = 0; i < n - 1; i++) {
        if (!(A[i] == A[i + 1]) && !(A[i] == A[i + 1] - 1)) {
            System.out.println(A[i] + "-->" + A[i + 1]);
            return false;
        }
    }
    if (A[0] != 1 || A[n - 1] != k) {
        return false;
    } else {
        return true;
    }
}

if在for循环中定义的条件可以修改如下以使代码工作(如果K值与Array的元素匹配则返回true)

public static bool solution(int[] A, int K)
{
    int n = A.length;
    for (int i = 0; i < A.length; i++)
    {
        if (A[i] == K) // This is the changed condition 
            return true;
    }
    if (A[0] == 1 || A[n - 1] != K)
        return false;
    else
        return true;
} 

这在Java 8中是可能的

你需要做以下更改

1 -改变for循环中的if条件-创建一个Integer类型的列表,然后检查包含注意- int类型的列表将不起作用-包含将不起作用

2 -第二个返回true

的返回语句

这就是你如何通过改变两行来完成它的方法

public static  boolean sol (int[] a , int k){
    int n = a.length;
    for(int i=0;i<n-1;i++){
        if(! Arrays.stream( a ).boxed().collect( Collectors.toList() ).contains(k)){
            return false;
        }
    }
    if(a[0]!=1 && a[n-1]!=k){
        return true;
    }
    else{
        return true;
    }
}

最新更新