[英]Using only if-else statements to find specific element in an array with length n and elements from 0 to n-1
我一直在解决面试问题。 目标是从一个长度未知(不能用.length)的数组中找到一个特定的元素并返回步数,但是对于长度为n的数组,元素保证是从0到n-1,没有重复。 例如,如果数组的长度为 5,则元素为 {0, 1, 2, 3, 4} 但顺序可能不同。 额外的要求是没有循环,没有静态/全局变量,也没有辅助函数,传入的参数只有数组 int[] arr 和目标值 int x,不允许额外的参数,数组在所有之后保持不变操作已经完成。
//So you can only write in the body of the following method, no outside variables nor methods could be used.
private int findElement (int[] arr, int x) {
}
到目前为止我得到的是,由于元素保证为 0 到 n-1 ,我可以使用目标数字作为索引并将 go 返回数组以查看数字arr[x]是否等于数字x I想。 如果不是,我将那个数字arr[x] 设为我的新索引,重复直到找到目标值。
int[] arr = {4, 1, 0, 2, 3}
int target = 3;
arr[3] = 2; //take target as the initial index
arr[2] = 0;
arr[0] = 4;
arr[4] = 3; //we got the number we want
//steps total is 3 since the question says the first time doesn't count.
问题:我试图通过递归来解决这个问题,但由于我总是将以下值与初始参数值进行比较,在上述情况下我总是想找到 3。那么如何在没有 static 变量或额外参数的情况下存储该信息是我的大问题。 有没有其他方法可以存储初始参数值并在整个过程中传递它?
private int findElement(int [] arr, int x) {
int actualN = arr[x];
if (actualN == **???**) { //can't be x cuz x is changing but I always want 3
return 0;
} else {
return findElement(arr, arr[x]) + 1;
}
}
最好使用 Java任何提示或帮助将不胜感激。
你几乎在那里
// t is the target number, o is teh array offset
static int find(int [] arr, int t, int o) {
if (arr[o] == t)
return o;
return find(arr, t, o + 1);
}
和
static void Main(string[] args) {
int[] arr = { 4, 1, 0, 2, 3 };
int target = 3;
int x = find(arr, 3, 0);
}
如果只允许 2 个 args - 我错过了
在 c
static int* find(int* arr, int t) {
if (*arr == t)
return arr;
return find(arr + 1, t);
}
int main() {
int arr[] = {4, 1, 0, 2, 3};
int target = 2;
int x = find(arr, target) - arr;
}
在 c#
static unsafe int* find(int * arr, int t) {
if (*arr == t)
return arr;
return find(arr + 1,t);
}
static void Main(string[] args) {
int[] arr = { 4, 1, 0, 2, 3 };
int target = 3;
unsafe {
fixed (int * p = &arr[0]) {
int x = (int)(find(p, target) - p);
}
}
}
可能这应该有效:
private int findElement(int [] arr, int x) {
int currValue = arr[x], returnValue;
if(arr[x]>0)
arr[x] = 0;//setting the actual element of the array to 0
else
arr[x]--;// decrementing the search index so it goes from 0-> -1 -> -2 -> -3...
if(Math.abs(arr[-arr[x]]) == x)//We check if the number is at our search index...
returnValue = 0;
else
returnValue = findElement(arr, x)+1;
arr[x] = currValue;//We take the value of the index from when the function was called and then reassign it to the same index after our work with it is done.
return returnValue;
}
由于数组只需要在执行后相同,并且在执行期间它是 state 并不重要,这可能会起作用。 注意:我还没有对此进行详细的测试,所以请在提交之前有时测试代码
我假设arr
可以修改,前提是在获得答案后它没有改变。
因为答案最好是 Java(而且我不懂 Java),所以我将在 Ruby 中提供一个解决方案。凭借其伪代码外观和添加的评论,不熟悉 Ruby 的读者应该能够理解计算。
具体来说,我 append 给定数组的一个元素,它等于要检查的数组的当前元素的索引(最初为零)。 如果该元素等于目标值,我们返回递归链,最初返回零,然后在链的每个后续点加一。 在doit
返回所需的计数之前,删除数组的最后一个元素以将数组恢复为初始值。
如果由数组的最后一个元素(当前索引)索引的数组值不等于目标值,则数组的最后一个元素递增 1 并递归调用该方法。
def doit(arr,target)
arr << 0 # append the index 0 to arr
n = recurse(arr, target)
arr.pop # remove the last element of arr
n
end
def recurse(arr, target)
return 0 if arr[arr[-1]] == target
arr[-1] += 1 # increment last value of arr by 1
1 + recurse(arr, target)
end
arr = [4, 1, 0, 2, 3]
doit(arr, 4) #=> 0
doit(arr, 1) #=> 1
doit(arr, 0) #=> 2
doit(arr, 2) #=> 3
doit(arr, 3) #=> 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.