Find–二分查找实现
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数
public class Solution {
public boolean Find(int target, int [][] array) {
for (int i = 0; i < array.length; i++) {
if(twoFind(target,array[i])) return true;
}
return false;
}
public boolean twoFind(int target,int [] array){
int high = array.length-1;
int low = 0;
while(high >= low){
int mid = (high+low)/2;
if(array[mid] == target) return true;
else if (array[mid] > target) high = mid-1;
else low = mid + 1;
}
return false;
}
}
PrintListFromTailToHead–链表从尾到头的顺序返回一个ArrayList
package offer;
import java.util.ArrayList;
import java.util.Collections;
/*输入一个链表,按链表从尾到头的顺序返回一个ArrayList。*/
public class PrintListFromTailToHead {
public static void main(String[] args) {
ListNode listNode =new ListNode(1);
listNode.next = new ListNode(2);
System.out.println(new PrintListFromTailToHead().printListFromTailToHead(null));
}
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> integers = new ArrayList<>();
if (listNode == null){
return integers;
}
while(listNode.next != null){
integers.add(listNode.val);
listNode = listNode.next;
}
integers.add(listNode.val);
Collections.reverse(integers);
return integers;
}
}
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
ReConstructBinaryTree–前序中序重建树
/**
* @Description TODO
* @ClassName ReConstructBinaryTree.java
* @Author yangkai
* @CreateTime 2020年03月16日 22:45:00
* @Version 1.0
* @History
**/
package offer;
import java.util.HashMap;
import java.util.Map;
/*输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如输入前序遍历序列{1,2,4,7,3,5,6,8}和
中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
*/
public class ReConstructBinaryTree {
// 前序遍历序列{1,2,4,7,3,5,6,8}
// 中序遍历序列{4,7,2,1,5,3,8,6}
public static void main(String[] args) {
int pre[] = {1,2,4,7,3,5,6,8};
int in[] = {4,7,2,1,5,3,8,6};
TreeNode treeNode = new ReConstructBinaryTree().reConstructBinaryTree(pre,in);
System.out.println(1);
}
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if(pre==null||in==null){
return null;
}
Map<Integer,Integer> map= new HashMap<Integer, Integer>();
for(int i=0;i<in.length;i++){
map.put(in[i],i);
}
return preIn(pre,0,pre.length-1,in,0,in.length-1,map);
}
public TreeNode preIn(int[] p,int pi,int pj,int[] n,int ni,int nj,Map<Integer,Integer> map){
if(pi>pj){
return null;
}
TreeNode head=new TreeNode(p[pi]);
int index=map.get(p[pi]);
head.left=preIn(p,pi+1,pi+index-ni,n,ni,index-1,map);
head.right=preIn(p,pi+index-ni+1,pj,n,index+1,nj,map);
return head;
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
StackPushAndPop–双栈实现队列
/**
* @Description TODO
* @ClassName StackPushAndPop.java
* @Author yangkai
* @CreateTime 2020年03月17日 21:01:00
* @Version 1.0
* @History
**/
package offer;
import java.util.Stack;
//用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
public class StackPushAndPop {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
while(stack1.size() > 0){
Integer pop = stack1.pop();
stack2.push(pop);
}
stack1.push(node);
while(stack2.size() > 0){
Integer pop = stack2.pop();
stack1.push(pop);
}
}
public int pop() {
return stack1.pop();
}
public static void main(String[] args) {
StackPushAndPop stackPushAndPop = new StackPushAndPop();
stackPushAndPop.push(1);
stackPushAndPop.push(2);
stackPushAndPop.push(3);
stackPushAndPop.push(4);
Integer pop = null;
while((pop = stackPushAndPop.pop())!=null){
System.out.println(pop);
}
}
}
Tuzi
一对兔子,三个月后,每个月生一对,问第N个月有多少只兔子
import java.util.HashMap;
import java.util.Map;
/**
* @Description TODO
* @ClassName TuZi.java
* @Author yangkai
* @CreateTime 2020年03月18日 17:01:00
* @Version 1.0
* @History
**/
public class TuZi {
public static void main(String[] args) {
Map<Integer,Integer> map = new HashMap<>();
map.put(1,0);
map.put(2,0);
map.put(3,1);
for (int i = 4; i <= 10; i++) {
Integer a1 = map.get(1);
Integer a2 = map.get(2);
Integer a3 = map.get(3);
map.put(3,a3+a2);
map.put(2,a1);
map.put(1,a3);
}
System.out.println(2*(map.get(1)+map.get(2)+map.get(3)));
}
}