You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.3 KiB
2.3 KiB
数组相关
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0) {
return false;
}
int m = matrix.length, n = matrix[0].length;
int row = 0, col = n - 1;
while (row < m && col >= 0) {
if (matrix[row][col] > target) {
col--;
} else if (matrix[row][col] < target) {
row++;
} else {
return true;
}
}
return false;
}
}
class Solution {
public int minArray(int[] numbers) {
int i = 0, j = numbers.length - 1;
while (i < j) {
int m = (i + j) / 2;
if (numbers[m] > numbers[j]) {
i = m + 1;
} else if (numbers[m] < numbers[j]) {
j = m;
} else {
j--;
}
}
return numbers[i];
}
}
class Solution {
public int[] exchange(int[] nums) {
int i = 0, j = nums.length - 1;
while (i < j) {
if (nums[i] % 2 == 1 && nums[j] % 2 == 0) {
i++;
j--;
} else if (nums[i] % 2 == 1 && nums[j] % 2 == 1) {
i++;
} else if (nums[i] % 2 == 0 && nums[j] % 2 == 1) {
int temp = nums[i];
nums[i++] = nums[j];
nums[j--] = temp;
} else {
j--;
}
}
return nums;
}
}
class Solution {
public int[] exchange(int[] nums) {
int i = 0, j = nums.length - 1, temp;
while (i < j) {
while (i < j && (nums[i] & 1) == 1) {
i++;
}
while (i < j && (nums[j] & 1) == 0) {
j--;
}
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
return nums;
}
}