题目描述
public class Solution {
public boolean Find(int target, int [][] array) {
int flag = 0;
int row = array.length-1;
int col = array[0].length;
int j = 0;
//从左下角开始遍历,寻找大于target的下标
//遍历列方向
while(j < col){
int i = row;
if(array[i][j] == target) return true;
else if(array[i][j] > target) //如果array最后一行元素大于target,遍历这一列
{
while( i >= 0){
if(array[i][j] == target ) return true;
i--;
}
}
j++;
}
return false;
}
}
总结:自己真是菜,这么简单,写了20分钟,太久没摸这些题了,慢慢来。 2018/10/29
====================================
题目描述
public class Solution {
public String replaceSpace(StringBuffer str) {
String str1 = str.toString();
String str2 = str1.replace(" ","%20");
return str2;
}
}
======================================
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
思路,遍历传入的头节点存入temp,然后反取
/** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */ import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { //返回一个ArrayList ArrayList<Integer> result = new ArrayList<Integer>(); ArrayList<Integer> temp = new ArrayList<Integer>(); ListNode tempNode = listNode; while(tempNode != null){ temp.add(tempNode.val); tempNode = tempNode.next; } for(int i = temp.size() - 1 ; i>=0 ; i--){ result.add(temp.get(i)); } return result; } }
===========================================