码迷,mamicode.com
首页 > 其他好文 > 详细

11.6---矩阵查找元素(CC150)

时间:2016-01-05 01:22:13      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

思路,一旦提到查找就要想到二分查找。

技术分享
    public static int[] findElement(int[][] a, int n, int m, int key) {
        // write code here
        int[] res = new int[2];
        for(int i = 0;i < n;i++){
            int left = 0;
            int right = m - 1;
            while(left <= right){
                int mid = (left + right) / 2;
                if(key < a[i][mid]){
                    right = mid -1;
                }
                else if (key > a[i][mid]){
                    left = mid + 1;
                }
                else{
                    res[0] = i;
                    res[1] = mid;
                    break;
                }
            }
        }

        return res;
    }
View Code

 

11.6---矩阵查找元素(CC150)

标签:

原文地址:http://www.cnblogs.com/yueyebigdata/p/5100666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!