面试问题:整数指数问题 [英] Interview question: Integer index question

查看:71
本文介绍了面试问题:整数指数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在采访中被问到这个问题:

数组中的整数是唯一的并且越来越多。编写一个函数来查找数组中与其索引相等的整数。例如。在数组{-3,-1,1,3,5}中,数字3等于其索引3.



有什么想法吗?

I was asked about this question in interview:
Integers in an array are unique and increasingly sorted. Write a function to find an integer from the array who equals to its index. E.g. in the array {-3, -1, 1, 3, 5}, the number 3 equals its index 3.

Any ideas?

推荐答案

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        Dim numbers = New Integer() {-3, -1, 1, 3, 5}


        MsgBox(Find_Index_Value_Same_As_Array_Value(numbers))



    End Sub

    Private Function Find_Index_Value_Same_As_Array_Value(ByVal _Array() As Integer) As Integer



        Find_Index_Value_Same_As_Array_Value = -1




        '--- Find The Max Number For The Array Index

        Dim _Max As Integer = UBound(_Array)

        Dim _My_Counter As Integer = 0

        For _My_Counter = 0 To _Max
            If _My_Counter = _Array(_My_Counter) Then
                Find_Index_Value_Same_As_Array_Value = _Array(_My_Counter)
                Exit Function
            End If
        Next

        

    End Function


int[] numbers = { -3, -1, 1, 3, 5 };

int[] results = numbers.Where((nbr, i) => i == nbr).ToArray();
string message = results.Length == 0
? "Nothing found"
: string.Format("Found {0}. First is at index {1}", results.Length, results[0]);
          Console.WriteLine(message);


这篇关于面试问题:整数指数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆