BM17 二分查找-I
import java.util.*;
public class Solution {
/
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @param target int整型
* @return int整型
*/
public int search (int[] nums, int target) {
// write code here
if (nums.length == 0) return -1;
int right = nums.length - 1;
int left = 0;
while (left <= right) {
int mid = (left + right) >> 1;
if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid - 1;
} else {
return mid;
}
}
return -1;
}
}
BM18 二维数组中的查找
public class Solution {
public boolean Find(int target, int [][] array) {
int row = array.length;//行
int col = array[0].length;
for(int i = 0; i < row; i++) {
int[] arr = array[i];
int left = 0;
int right = col - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(arr[mid] > target){
right = mid - 1;
}else if(arr[mid] < target){
left = mid + 1;
}else{
return true;
}
}
}
return false;
}
}
public class Solution {
public boolean Find(int target, int [][] array) {
if(array.length == 0) return false;
int rol = array.length;
int col = array[0].length;
int i = rol - 1;
int j = 0;
while(i >= 0 && j < col){
int tmp = array[i][j];
if(tmp > target){
i--;
}else if(tmp < target){
j++;
}else{
return true;
}
}
return false;
}
}
寻找峰值_牛客题霸_牛客网 (nowcoder.com)
import java.util.*;
public class Solution {
/
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
public int findPeakElement (int[] nums) {
// write code here
for(int i = 1; i < nums.length; i++) {
if(nums[i] >= nums[i - 1]){
while(i < nums.length && nums[i] >= nums[i - 1]) {
i++;
}
return i - 1;
}
}
return 0;
}
}
import java.util.*;
public class Solution {
/
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
public int findPeakElement (int[] nums) {
// write code here
int left = 0;
int right = nums.length - 1;
while(left < right){//后续会left和right重合在一起, 所以这里不能有=
int mid = left + (right - left) / 2;
if(nums[mid] > nums[mid + 1]) {
right = mid;
}else if(nums[mid] <= nums[mid +1]) {
left = mid + 1;
}
}
return left;
}
}
数组中的逆序对_牛客题霸_牛客网 (nowcoder.com)
public class Solution {
int count = 0;
public int InversePairs(int [] array) {
if (array.length < 2) {
return 0;
}
mergeSort(array, 0, array.length - 1);
return count;
}
public void mergeSort(int[]array, int left, int right) {
int mid = left + (right - left) / 2;
if (left < right) {
mergeSort(array, left, mid);
mergeSort(array, mid+1, right);
merge(array, left, mid, right);
}
}
public void merge(int [] array, int left, int mid, int right){
int s1 = left;
int s2 = mid + 1;
int e1 = mid;
int e2 = right;
int i = 0;
int[] arr = new int[right - left + 1];
while(s1 <= e1 && s2 <= e2){
if(array[s1] <= array[s2]){
arr[i++] = array[s1++];
}else{//存在逆序
arr[i++] = array[s2++];
count += ((e1 - s1 + 1));
count %= 1000000007;
}
}
while(s1 <= e1){
arr[i++] = array[s1++];
}
while(s2 <= e2){
arr[i++] = array[s2++];
}
for(int j = 0; j < arr.length; j++){
array[j+left] = arr[j];
}
}
}
旋转数组的最小数字_牛客题霸_牛客网 (nowcoder.com)
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
int left = 0;
int right = array.length - 1;
while(left < right){
int mid = left + (right - left) / 2;
if(array[mid] > array[right]){
left = mid + 1;
}else if(array[mid] < array[right]){
right = mid;
}else{
right--;
}
}
return array[right];
}
}
比较版本号_牛客题霸_牛客网 (nowcoder.com)
import java.util.*;
public class Solution {
/
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 比较版本号
* @param version1 string字符串
* @param version2 string字符串
* @return int整型
*/
public int compare (String version1, String version2) {
if (version1 == version2) return 0;
if (version1 == null || version1 == "") {
return -1;
}
if (version2 == null || version2 == "") {
return 1;
}
// write code here
String[] arr1 = version1.split("\\.");
String[] arr2 = version2.split("\\.");
int len = arr1.length > arr2.length ? arr2.length : arr1.length;
for (int i = 0; i < len; i++) {
if (toInt(arr1[i]) > toInt(arr2[i])) {
return 1;
} else if (toInt(arr1[i]) < toInt(arr2[i])) {
return -1;
}
}
if (arr1.length > arr2.length) {
for(int j = len; j < arr1.length; j++) {
if(toInt(arr1[j]) != 0) return 1;
}
} else if (arr1.length < arr2.length) {
for(int j = len; j < arr2.length; j++) {
if(toInt(arr2[j]) != 0) return -1;
}
}
return 0;
}
public int toInt(String str) {
return Integer.parseInt(str);
}
}