import java.util.ArrayList;
import java.util.Stack;
public class OfferTest {
public static void main(String[] args) {
int [][] test = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
System.out.println(Find(9,test));
StringBuffer stringBuffer = new StringBuffer("hello world");
System.out.println(replaceSpace(stringBuffer));
System.out.println(printListFromTailToHead(new ListNode(5555)).toString());
push(1);
push(2);
push(3);
System.out.println(pop()+"---"+pop());
push(4);
System.out.println(pop());
push(5);
System.out.println(pop()+"---"+pop());
System.out.println(Fibonacci(7));
System.out.println(JumpFloor(4));
System.out.println(JumpFloorII(3));
}
/**
* 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,
* 每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
* @param target
* @param array
* @return
*/
public static boolean Find(int target, int [][] array) {
/*二维数组的行数和列数*/
int rowCount = array.length;
int colCount = array[0].length;
int i, j;//i指定行的变化,j指定列的变化
//循环
for (i = rowCount - 1, j = 0; i >= 0 && j<colCount;)
{
if (target == array[i][j]){
return true;
}
else if (target<array[i][j])
{
i--;
continue;
}
else if(target>array[i][j])
{
j++;
continue;
}
}
return false;
}
/**
* 请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
* @param stringBuffer
* @return
*/
public static String replaceSpace(StringBuffer stringBuffer){
String str = stringBuffer.toString();
char [] chars = str.toCharArray();
int length = chars.length;
int count = 0;
for (int i=0;i<length;i++){
if(chars[i]==' '){
count++;
}
}
char [] newChar = new char[length+count*2];
int pos = 0;
for (int i=0;i<length;i++){
if(chars[i]==' '){
newChar[pos]='%';
newChar[pos+1] = '2';
newChar[pos+2] = '0';
pos = pos+3;
continue;
}
newChar[pos] = chars[i];
pos++;
}
return new String(newChar);
}
/**
* 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
*/
public static class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
private static ArrayList<Integer> re = new ArrayList<Integer>();
public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if (listNode == null)
return re;
printListFromTailToHead(listNode.next);
re.add(listNode.val);
return re;
}
/**
* 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
* 例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
* @param pre
* @param in
* @return
*/
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
return root;
}
//前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
if(startPre>endPre||startIn>endIn)
return null;
TreeNode root=new TreeNode(pre[startPre]);
for(int i=startIn;i<=endIn;i++)
if(in[i]==pre[startPre]){
root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
break;
}
return root;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
/**
* 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
*/
static Stack<Integer> stack1 = new Stack<Integer>();
static Stack<Integer> stack2 = new Stack<Integer>();
public static void push(int node) {
stack1.push(node);
}
public static int pop() {
if (stack1.empty() && stack2.empty()) {
throw new RuntimeException("Queue is empty!");
}
if (stack2.empty()) {
while (!stack1.empty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
/**
* 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。
* 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
*/
public int minNumberInRotateArray(int [] array) {
if(array.length==1){
return array[0];
}
for(int i=0;i<array.length-1;i++){
if(array[i]>array[i+1]){
return array[i+1];
}else{
if(i==array.length-2){
return array[0];
}
}
}
return 0;
}
/**
* 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。n<=39
* @param n
* @return
*/
public static int Fibonacci(int n) {
if(n == 0 ){
return 0;
}
if(n == 1 || n==2){
return 1;
}
int now = 1;
int last = 1;
int temp;
for (int i= 2;i<n;i++){
temp = now;
now = now+last;
last = temp;
}
return now;
}
/**
* 一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
* @param target
* @return
*/
public static int JumpFloor(int target) {
if (target <= 0) {
return -1;
} else if (target == 1) {
return 1;
} else if (target ==2) {
return 2;
} else {
return JumpFloor(target-1)+JumpFloor(target-2);
}
}
/**
* 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
* @param target
* @return
*/
public static int JumpFloorII(int target) {
int result = 1;
if (target <= 0) {
return -1;
} else if (target == 1) {
return 1;
} else if (target ==2) {
return 2;
} else {
for (int i = 1;i<target;i++){
result += JumpFloorII(target-i);
}
return result;
}
}
}
复制代码
转载于:https://juejin.im/post/5baf36cdf265da0a8f35d284