03. 数组中重复的数字
class Solution {
public int findRepeatNumber(int[] nums) {
Arrays.sort(nums);
for (int i=0; i<nums.length; i++) if (nums[i] == nums[i+1]) return nums[i];
return 0;
}
}
04. 二维数组中的查找
class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix.length==0 || matrix[0].length==0) return false;
int len = matrix[0].length-1;
for (int i = 0; i < matrix.length; i++) {
if (matrix[i][0]>target) return false;
else if (matrix[i][len]>=target) {
for (int j = 0; j <= len; j++) {
if (matrix[i][j] == target) return true;
}
}
}
return false;
}
}
05. 替换空格
class Solution {
public String replaceSpace(String s) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char data = s.charAt(i);
if (data == ' ') str.append("%20");
else str.append(data);
}
return str.toString();
}
}
06. 从尾到头打印链表
class Solution {
public int[] reversePrint(ListNode head) {
int len=0;
int[] data;
Stack<ListNode> zh = new Stack<>();
ListNode temp = head;
while(temp != null){
len++;
zh.push(temp);
temp = temp.next;
}
data = new int[len];
for (int i = 0; i < len; i++) {
data[i] = zh.pop().val;
}
return data;
}
}