Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Your algorithm’s runtime complexity must be in the order of O(log n).
输入格式:
3 lines.
The first line means the number of the keys.
The second line consists several integers separated by commas.
The third line is the target value to search
输出格式:
1 line
If found, output the index of the target value, otherwise output -1.
intmain() { int n; scanf("%d,", &n); if (n == 0) { printf("-1"); return0; } int arr[n]; for (int i = 0; i < n - 1; i++) { scanf("%d,", &arr[i]); } scanf("%d", &arr[n - 1]); int keyval; scanf("%d", &keyval); int keyidx = -1; //Search if (n == 1) { if (arr[0] == keyval) { keyidx = 0; } else { printf("-1"); return0; } } int left = 0, right = n - 1;
while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == keyval) { keyidx = mid; break; } elseif (arr[left] <= arr[mid]) { if (arr[left] <= keyval && keyval < arr[mid]) { right = mid - 1; } else { left = mid + 1; } } else { if (arr[mid] < keyval && keyval <= arr[right]) { left = mid + 1; } else { right = mid - 1; } } } printf("%d", keyidx); return0; }
7-2 Search a 2D Matrix
Using an efficient algorithm (e.g., binary search) to search for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
输入格式:
The first line contains two integers m, n which are the numbers of the rows and columns, respectivley.
Then follows m lines which is an m x n matrix. This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
The last line is the target value to search.
输出格式:
1 line
If found, output true, otherwise output false.
// Perform a binary search on the given array to find the target value intbinarySearch(int* nums, int len, int target) { int low = 0, high = len - 1; while (low <= high) { int mid = low + (high - low) / 2; if (nums[mid] == target) return mid; if (nums[mid] > target) high = mid - 1; else low = mid + 1; } return-1; }
// Search for the target value in the matrix using binary search intsearchMatrix(int** matrix, int rows, int cols, int target) { if (rows == 0 || cols == 0) return-1;
// Use binary search to find the row in which the target value is likely to be located int low = 0, high = rows - 1; while (low <= high) { int mid = low + (high - low) / 2; if (matrix[mid][0] <= target && matrix[mid][cols - 1] >= target) { // The target value is likely to be in the current row, so perform binary search on this row return binarySearch(matrix[mid], cols, target); } if (matrix[mid][0] > target) high = mid - 1; else low = mid + 1; } return-1; }
intmain() { int rows, cols; scanf("%d,%d\n", &rows, &cols);
// Create the matrix using dynamic memory allocation int** matrix = (int**) malloc(rows * sizeof(int*)); for (int i = 0; i < rows; i++) { matrix[i] = (int*) malloc(cols * sizeof(int)); }
// Read the matrix from input for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { scanf("%d,", &matrix[i][j]); } } int target; scanf("%d", &target);
// Search for the target value in the matrix int result = searchMatrix(matrix, rows, cols, target); if (result == -1) printf("false\n"); elseprintf("true\n");
// Free the memory allocated for the matrix for (int i = 0; i < rows; i++) { free(matrix[i]); } free(matrix);