查找数据是否存在于二维数组中
2018-01-20 15:52
190 查看
package com.mooc;
public class Basic {
public static void main(String[] args) {
// 在一个二维数组中,每一行都按照从左到右递增的顺序排序
// ,每一列都按照从上到下递增的顺序排序。
// 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
int target = 22;
int target1 = 66;
int[][] a = { { 1, 3, 4 }, { 55, 66, 77 }, { 541, 4445, 6677 } };
System.out.println(contains(a, target));
System.out.println(contains(a, target1));
}
public static boolean contains(int[][] array, int target) {
for (int i = 0, j = array[i].length - 1; j>=0&&i < array.length - 1;) {
if (target > array[i][j]) {
i++;
continue;
}
if (target < array[i][j]) {
j--;
continue;
}
if (target == array[i][j]) {
return true;
}
}
return false;
}
}
public class Basic {
public static void main(String[] args) {
// 在一个二维数组中,每一行都按照从左到右递增的顺序排序
// ,每一列都按照从上到下递增的顺序排序。
// 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
int target = 22;
int target1 = 66;
int[][] a = { { 1, 3, 4 }, { 55, 66, 77 }, { 541, 4445, 6677 } };
System.out.println(contains(a, target));
System.out.println(contains(a, target1));
}
public static boolean contains(int[][] array, int target) {
for (int i = 0, j = array[i].length - 1; j>=0&&i < array.length - 1;) {
if (target > array[i][j]) {
i++;
continue;
}
if (target < array[i][j]) {
j--;
continue;
}
if (target == array[i][j]) {
return true;
}
}
return false;
}
}
相关文章推荐
- 开辟一个二维数组,有10*8个元素,用随机数填充,按照下面的方法用函数实现查找一个数是否存在
- //杨氏矩阵 有一个二维数组. 数组的每行从左到右是递增的,每列从上到下是递增的. 在这样的数组中查找一个数字是否存在。 时间复杂度小于O(N);
- 在一个每一行从左到右递增每一列从上到下递增的二维数组中查找一个整数是否存在――3
- 剑指offer_快速查找递增二维数组中是否存在目标
- 在一个每一行从左到右递增每一列从上到下递增的二维数组中查找一个整数是否存在——3
- 用二维数组保存99乘法表并且输入一个数查找它是否在数组中存在
- 有一个二维数组杨氏矩阵,数组的每行从左到右是递增的,每列从上到下是递增的,在这样的数组中查找一个数字是否存在
- //杨氏矩阵 有一个二维数组. 数组的每行从左到右是递增的,每列从上到下是递增的. 在这样的数组中查找一个数字是否存在。
- 在一个二维有序数组中,查找指定的数据是否存在
- 有一个二维数组. 数组的每行从左到右是递增的,每列从上到下是递增的. 在这样的数组中查找一个数字是否存在。 时间复杂度小于O(N)
- 在二维数组中查找指定的序列是否存在
- 二维数组中查找一个数是否存在
- 有一个二维数组. 数组的每行从左到右是递增的,每列从上到下是递增的. 在这样的数组中查找一个数字是否存在。 时间复杂度小于O(N);
- 二维数组中查找是否存在一个数
- 程序员面试题目总结--数组(五)【数组的后面m个数移动为前面m个数、列的前n项数据、判断整数x是否可以表示成n个连续正整数的和、数组中出现奇数次的元素、二维数组中的查找】
- 二维数组中查找某个数是否存在
- 位图法--解决海量数据问题(查找是否存在,不重复数据)
- 杨氏矩阵 有一个二维数组. 数组的每行从左到右是递增的,每列从上到下是递增的. 在这样的数组中查找一个数字是否存在。
- 开辟一个二维数组,有10*8个元素,用随机数填充,按照下面的方法用函数实现查找一个数是否存在
- 杨氏矩阵:有一个二维数组,数组的每行从左到右都是递增的,每列从上到下都是递增的,从这样的数组中查找一个数字是否存在