publicclass Solution
public boolean Find(int [][] array, int target)
for (int i=0; i<array.length; i++)
for (int j=0; j<array[i].length; j++)
if (array[i][j] == target)
returntrue;
returnfalse;
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
方法二
时间复杂度 O(n)
publicclass Solution
public boolean Find(int [][] array, int target)
int row =0;
int col = array[0].length-1;
int numRow = array.length;
while (row < numRow && col>=0)
if (array[row][col] > target)
col--;
elseif (array[row][col] < target)
row++;
elsereturntrue; // 相等,返回truereturnfalse; //遍历完,没有找到相等值,返回false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
替换空格
方法一
借用String.replace()
publicclass Solution
public String replaceSpace(StringBuffer str)
String str1 = new String(str);
return str1.replace(" ", "%20");
import java.util.ArrayList;
class ListNode
int val;
ListNode next = null;
ListNode(int val)
this.val = val;
publicclass Solution
public ArrayList<Integer> printListFromTailToHead(ListNode listNode)
ArrayList<Integer> al = new ArrayList<Integer>();
if (listNode == null)
return al;
ListNode p = listNode;
while (p != null)
al.add(p.val);
p = p.next;
int lower = 0;
int higher =al.size()-1;
while (lower < higher)
int temp = al.get(lower);
al.set(lower, al.get(higher));
al.set(higher, temp);
lower++;
higher--;
return al;