给定一个按升序排列的 n 个不同整数的数组,编写一个函数,返回数组中的一个定点,如果数组中存在任何定点,否则返回 -1。数组中的不动点是一个索引 i,使得 arr[i] 等于 i。请注意,数组中的整数可以为负数。
例子:
Input: arr[] = {-10, -5, 0, 3, 7}
Output: 3 // arr[3] == 3
Input: arr[] = {0, 2, 5, 8, 17}
Output: 0 // arr[0] == 0
Input: arr[] = {-10, -5, 3, 4, 7, 9}
Output: -1 // No Fixed Point
方法一(线性搜索)
线性搜索索引 i 使得 arr[i] == i。返回找到的第一个此类索引。感谢 pm 提出这个解决方案。
C++
// C++ program to check fixed point
// in an array using linear search
#include
using namespace std;
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;
}
/* Driver code */
int main()
{
int arr[] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Fixed Point is " << linearSearch(arr, n);
return 0;
}
// This is code is contributed by rathbhupendra
C
// C program to check fixed point
// in an array using linear search
#include
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;
}
/* Driver program to check above functions */
int main()
{
int arr[] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Fixed Point is %d", linearSearch(arr, n));
getchar();
return 0;
}
Java
// Java program to check fixed point
// in an array using linear search
class Main
{
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
public static void main(String args[])
{
int arr[] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
int n = arr.length;
System.out.println("Fixed Point is "
+ linearSearch(arr, n));
}
}
Python
# Python program to check fixed point
# in an array using linear search
def linearSearch(arr, n):
for i in range(n):
if arr[i] is i:
return i
# If no fixed point present then return -1
return -1
# Driver program to check above functions
arr = [-10, -1, 0, 3, 10, 11, 30, 50, 100]
n = len(arr)
print("Fixed Point is " + str(linearSearch(arr,n)))
# This code is contributed by Pratik Chhajer
C#
// C# program to check fixed point
// in an array using linear search
using System;
class GFG
{
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;
}
// Driver code
public static void Main()
{
int []arr = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
int n = arr.Length;
Console.Write("Fixed Point is "+ linearSearch(arr, n));
}
}
// This code is contributed by Sam007
PHP
Javascript
C++
// C++ program to check fixed point
// in an array using binary search
#include
using namespace std;
int binarySearch(int arr[], int low, int high)
{
if(high >= low)
{
int mid = (low + high)/2; /*low + (high - low)/2;*/
if(mid == arr[mid])
return mid;
if(mid > arr[mid])
return binarySearch(arr, (mid + 1), high);
else
return binarySearch(arr, low, (mid -1));
}
/* Return -1 if there is no Fixed Point */
return -1;
}
/* Driver code */
int main()
{
int arr[10] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
int n = sizeof(arr)/sizeof(arr[0]);
cout<<"Fixed Point is "<< binarySearch(arr, 0, n-1);
return 0;
}
// This code is contributed by rathbhupendra
C
// C program to check fixed point
// in an array using binary search
#include
int binarySearch(int arr[], int low, int high)
{
if(high >= low)
{
int mid = (low + high)/2; /*low + (high - low)/2;*/
if(mid == arr[mid])
return mid;
if(mid > arr[mid])
return binarySearch(arr, (mid + 1), high);
else
return binarySearch(arr, low, (mid -1));
}
/* Return -1 if there is no Fixed Point */
return -1;
}
/* Driver program to check above functions */
int main()
{
int arr[10] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Fixed Point is %d", binarySearch(arr, 0, n-1));
getchar();
return 0;
}
Java
// Java program to check fixed point
// in an array using binary search
class Main
{
static int binarySearch(int arr[], int low, int high)
{
if(high >= low)
{
/* low + (high - low)/2; */
int mid = (low + high)/2;
if(mid == arr[mid])
return mid;
if(mid > arr[mid])
return binarySearch(arr, (mid + 1), high);
else
return binarySearch(arr, low, (mid -1));
}
/* Return -1 if there is
no Fixed Point */
return -1;
}
//main function
public static void main(String args[])
{
int arr[] = {-10, -1, 0, 3 , 10, 11, 30, 50, 100};
int n = arr.length;
System.out.println("Fixed Point is "
+ binarySearch(arr,0, n-1));
}
}
Python
# Python program to check fixed point
# in an array using binary search
def binarySearch(arr, low, high):
if high >= low:
mid = (low + high)//2
if mid is arr[mid]:
return mid
if mid > arr[mid]:
return binarySearch(arr, (mid + 1), high)
else:
return binarySearch(arr, low, (mid -1))
# Return -1 if there is no Fixed Point
return -1
# Driver program to check above functions */
arr = [-10, -1, 0, 3, 10, 11, 30, 50, 100]
n = len(arr)
print("Fixed Point is " + str(binarySearch(arr, 0, n-1)))
# This code is contributed by Pratik Chhajer
C#
// C# program to check fixed point
// in an array using binary search
using System;
class GFG
{
static int binarySearch(int []arr, int low, int high)
{
if(high >= low)
{
// low + (high - low)/2;
int mid = (low + high)/2;
if(mid == arr[mid])
return mid;
if(mid > arr[mid])
return binarySearch(arr, (mid + 1), high);
else
return binarySearch(arr, low, (mid -1));
}
/* Return -1 if there is
no Fixed Point */
return -1;
}
// Driver code
public static void Main()
{
int []arr = {-10, -1, 0, 3 , 10, 11, 30, 50, 100};
int n = arr.Length;
Console.Write("Fixed Point is "+ binarySearch(arr,0, n-1));
}
}
// This code is contributed by Sam007
PHP
= $low)
{
/*low + (high - low)/2;*/
$mid = (int)(($low + $high) / 2);
if($mid == $arr[$mid])
return $mid;
if($mid > $arr[$mid])
return binarySearch($arr, ($mid + 1), $high);
else
return binarySearch($arr, $low, ($mid - 1));
}
/* Return -1 if there is
no Fixed Po*/
return -1;
}
// Driver Code
$arr = array(-10, -1, 0, 3, 10,
11, 30, 50, 100);
$n = count($arr);
echo "Fixed Point is: "
. binarySearch($arr, 0, $n - 1);
// This code is contributed by Anuj_67
?>
Javascript
输出:
Fixed Point is 3
时间复杂度:O(n)
方法二(二分查找)
首先检查中间元素是否为Fixed Point。如果是,则返回;否则检查中间元素的索引是否大于索引处的值。如果 index 更大,则 Fixed Point(s) 位于中间点的右侧(显然只有在有 Fixed Point 时)。否则,固定点位于左侧。
C++
// C++ program to check fixed point
// in an array using binary search
#include
using namespace std;
int binarySearch(int arr[], int low, int high)
{
if(high >= low)
{
int mid = (low + high)/2; /*low + (high - low)/2;*/
if(mid == arr[mid])
return mid;
if(mid > arr[mid])
return binarySearch(arr, (mid + 1), high);
else
return binarySearch(arr, low, (mid -1));
}
/* Return -1 if there is no Fixed Point */
return -1;
}
/* Driver code */
int main()
{
int arr[10] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
int n = sizeof(arr)/sizeof(arr[0]);
cout<<"Fixed Point is "<< binarySearch(arr, 0, n-1);
return 0;
}
// This code is contributed by rathbhupendra
C
// C program to check fixed point
// in an array using binary search
#include
int binarySearch(int arr[], int low, int high)
{
if(high >= low)
{
int mid = (low + high)/2; /*low + (high - low)/2;*/
if(mid == arr[mid])
return mid;
if(mid > arr[mid])
return binarySearch(arr, (mid + 1), high);
else
return binarySearch(arr, low, (mid -1));
}
/* Return -1 if there is no Fixed Point */
return -1;
}
/* Driver program to check above functions */
int main()
{
int arr[10] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Fixed Point is %d", binarySearch(arr, 0, n-1));
getchar();
return 0;
}
Java
// Java program to check fixed point
// in an array using binary search
class Main
{
static int binarySearch(int arr[], int low, int high)
{
if(high >= low)
{
/* low + (high - low)/2; */
int mid = (low + high)/2;
if(mid == arr[mid])
return mid;
if(mid > arr[mid])
return binarySearch(arr, (mid + 1), high);
else
return binarySearch(arr, low, (mid -1));
}
/* Return -1 if there is
no Fixed Point */
return -1;
}
//main function
public static void main(String args[])
{
int arr[] = {-10, -1, 0, 3 , 10, 11, 30, 50, 100};
int n = arr.length;
System.out.println("Fixed Point is "
+ binarySearch(arr,0, n-1));
}
}
Python
# Python program to check fixed point
# in an array using binary search
def binarySearch(arr, low, high):
if high >= low:
mid = (low + high)//2
if mid is arr[mid]:
return mid
if mid > arr[mid]:
return binarySearch(arr, (mid + 1), high)
else:
return binarySearch(arr, low, (mid -1))
# Return -1 if there is no Fixed Point
return -1
# Driver program to check above functions */
arr = [-10, -1, 0, 3, 10, 11, 30, 50, 100]
n = len(arr)
print("Fixed Point is " + str(binarySearch(arr, 0, n-1)))
# This code is contributed by Pratik Chhajer
C#
// C# program to check fixed point
// in an array using binary search
using System;
class GFG
{
static int binarySearch(int []arr, int low, int high)
{
if(high >= low)
{
// low + (high - low)/2;
int mid = (low + high)/2;
if(mid == arr[mid])
return mid;
if(mid > arr[mid])
return binarySearch(arr, (mid + 1), high);
else
return binarySearch(arr, low, (mid -1));
}
/* Return -1 if there is
no Fixed Point */
return -1;
}
// Driver code
public static void Main()
{
int []arr = {-10, -1, 0, 3 , 10, 11, 30, 50, 100};
int n = arr.Length;
Console.Write("Fixed Point is "+ binarySearch(arr,0, n-1));
}
}
// This code is contributed by Sam007
PHP
= $low)
{
/*low + (high - low)/2;*/
$mid = (int)(($low + $high) / 2);
if($mid == $arr[$mid])
return $mid;
if($mid > $arr[$mid])
return binarySearch($arr, ($mid + 1), $high);
else
return binarySearch($arr, $low, ($mid - 1));
}
/* Return -1 if there is
no Fixed Po*/
return -1;
}
// Driver Code
$arr = array(-10, -1, 0, 3, 10,
11, 30, 50, 100);
$n = count($arr);
echo "Fixed Point is: "
. binarySearch($arr, 0, $n - 1);
// This code is contributed by Anuj_67
?>
Javascript
输出:
Fixed Point is 3
算法范式:分而治之
时间复杂度:O(Logn)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。