在给定数组中使用线性搜索找到固定点(值等于索引)- 逻辑问题

huangapple go评论77阅读模式
英文:

Fixed Point (Value equal to index) in a given array using Linear Search - Logic question

问题

以下是您要翻译的部分:

"Can someone provide an explanation why using this method below returns only the first matching value within the array? For example if My array element is 0, 1, 2, 3 and 4. It will return 0, instead of 4. Every value within the array matches to its index 4, should using the for loop return 4 after the last iteration is completed?

static int linearSearch(int arr[], int n)
{
int i;
for(i = 0; i < n; i++)
{
if(arr[i] == i)
return i;
}

/* If no fixed point present  
   then return -1 */
return -1; 

}
//main function"

英文:

Can someone provide an explanation why using this method below returns only the first matching value within the array? For example if My array element is 0, 1, 2, 3 and 4. It will return 0, instead of 4. Every value within the array matches to its index 4, should using the for loop return 4 after the last iteration is completed?

static int linearSearch(int arr[], int n) 
    { 
        int i; 
        for(i = 0; i &lt; n; i++) 
        { 
            if(arr[i] == i) 
                return i; 
        } 
        
        /* If no fixed point present  
           then return -1 */
        return -1; 
    } 
    //main function 

答案1

得分: 3

这是因为在你的 if 语句中,你立即调用了 return,而你的方法应该返回一个单独的 int。如果你想收集所有的固定点,你可以修改你的方法如下:

static List<Integer> linearSearch(int arr[], int n)
{
    List<Integer> fixedPoints = new ArrayList<>();
    for(int i = 0; i < n; i++)
    {
        if(arr[i] == i)
            fixedPoints.add(i);
    }

    return fixedPoints;
}
英文:

It's because in your if you immediately call return, and your method is supposed to return a single int. If you want to collect all fixed points, you can modify your method like this:

static List&lt;Integer&gt; linearSearch(int arr[], int n)
{
    List&lt;Integer&gt; fixedPoints = new ArrayList&lt;&gt;();
    for(int i = 0; i &lt; n; i++)
    {
        if(arr[i] == i)
            fixedPoints.add(i);
    }

    return fixedPoints;
}

答案2

得分: 2

这将返回4,因为它具有每个i匹配到数组中的值的更新索引。

static int linearSearch(int arr[], int n) 
{ 
    int i, index = -1; 
    for (i = 0; i < n; i++) 
    { 
        if (arr[i] == i) 
            index = i; 
    } 
    
    /* 如果没有固定点存在
       则返回-1 */
    return index; 
} 
// 主函数
英文:

This will return 4 as it has an updated index for each i matching to its value in an array.

static int linearSearch(int arr[], int n) 
    { 
        int i,index = -1; 
        for(i = 0; i &lt; n; i++) 
        { 
            if(arr[i] == i) 
                index = i; 
        } 
        
        /* If no fixed point present  
           then return -1 */
        return index; 
    } 
    //main function 

答案3

得分: 1

方法在达到return语句时返回给调用它的代码。根据文档

从方法返回一个值

当方法

  • 完成了方法中的所有语句,
  • 达到了return语句,或者
  • 抛出了异常,

会返回给调用它的代码,以先发生的情况为准。

语句 if(arr[i] == i)for循环的第一步返回true:

i=0
arr[0]=0

然后执行了 return i;

在这种情况下,数组的每个值都与它的索引匹配,但在最后的迭代完成后,使用for循环不会返回4,因为最后的迭代是for循环的第一次迭代,只针对i=0i 的值永远不会达到4。

英文:

> Q: Can someone provide an explanation why using this method below
> returns only the first matching value within the array?

A method returns to the code that invoked it when reaches a return statement.

From doc:
> Returning a Value from a Method
>
> A method returns to the code that invoked it when it
>
> - completes all the statements in the method,
> - reaches a return statement, or
> - throws an exception,
>
> whichever occurs first.

The statement if(arr[i] == i) returns true at the first step of the for loop:

i=0
arr[0]=0

Then the line return i; executed.

> Q: Every value within the array matches to its index 4, should using
> the for loop return 4 after the last iteration is completed?

In this condition the last iteration is the first iteration of the for loop. It runs only for i=0. The value of i never reaches to 4.

huangapple
  • 本文由 发表于 2020年8月6日 21:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63284521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定