以下代码都是我提交过的正确的代码
- 二维数组中的查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
public static boolean Find(int target, int[][] array) {
int height = array.length;
int width = array[0].length;
int i = height - 1;
int j = 0;
while(i >= 0 && j < width){
if(array[i][j] == target)
return true;
else if(array[i][j] > target){
i--;
}
else
j++;
}
return false;
}
- 替换空格
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
//Java版
static String replaceSpace(StringBuffer str) {
if(str == null)
return null;
StringBuilder newStr = new StringBuilder();
for(int i = 0; i < str.length();i++){
if(str.charAt(i) == ' '){
newStr.append("%20");
}
else
newStr.append(str.charAt(i));
}
return newStr.toString();
}
//js版
function replaceSpace(str){
if(str == null)
return null;
var reg = /\s/g;//全局替换空格
var result = str.replace(reg,"%20");
return result;
}
- 从尾到头打印链表
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
//用栈存链表的节点
public ArrayList<Integer> printListFromTailToHead(ListNode listNode){
Stack<Integer> stack = new Stack<>();
while(listNode != null){
stack.push(listNode.val);
listNode = listNode.next;
}
ArrayList<Integer> arrayList = new ArrayList<>();
while (!stack.isEmpty()){
arrayList.add(stack.pop());
}
return arrayList;
}
- 用两个栈实现队列
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
int first = stack1.pop();
while(!stack1.isEmpty()){
stack2.push(first);
first = stack1.pop();
}
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
return first;
}
}
- 旋转数组的最小数字
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
/*非递减数组:数组中每个元素大于等于上一个元素,所以非递减数组的旋转数组以最小的数字为界&#x