码迷,mamicode.com
首页 > 编程语言 > 详细

每天两道算法题(一)

时间:2018-02-18 16:21:53      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:find   ati   blog   nbsp   排序   var   ext   --   one   

题目描述

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

public class Solution {
    public boolean Find(int target, int [][] array) {
	try {
    		for(int i=array.length-1;i>=0;i--) {
    			if (array[i][0]>=target) {
    				if (array[i][0]==target) {
    					return true;
    				}
    				continue;
    			}
    			for(int j = 1;j<array[0].length;j++) {
    				if(array[i][j]==target)
    					return true;
    			}		
    		}
		} catch (Exception e) {
			return false;
		}
    	return false;
    }
}
?
 
1
public class Solution {
2
    public boolean Find(int target, int [][] array) {
3
    try {
4
            for(int i=array.length-1;i>=0;i--) {
5
                if (array[i][0]>=target) {
6
                    if (array[i][0]==target) {
7
                        return true;
8
                    }
9
                    continue;
10
                }
11
                for(int j = 1;j<array[0].length;j++) {
12
                    if(array[i][j]==target)
13
                        return true;
14
                }       
15
            }
16
        } catch (Exception e) {
17
            return false;
18
        }
19
        return false;
20
    }
21
}

题目描述

请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
public class Solution {
    public String replaceSpace(StringBuffer str) {
    	String strs = str.toString();
    	strs  = strs.replace(" ","%20");
    	return strs;
    }
}
x
1
public class Solution {
2
    public String replaceSpace(StringBuffer str) {
3
        String strs = str.toString();
4
        strs  = strs.replace(" ","%20");
5
        return strs;
6
    }
7
}









每天两道算法题(一)

标签:find   ati   blog   nbsp   排序   var   ext   --   one   

原文地址:https://www.cnblogs.com/liaoxianfu/p/6bd612b8cdd63e1bf5c0693e3e72d46b.html

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