From 49f2850c5eeead43e65b31d68b694c2d3b81f684 Mon Sep 17 00:00:00 2001 From: DuHouAn <18351926682@163.com> Date: 2019年7月25日 23:46:18 +0800 Subject: [PATCH 1/2] Java --- ...345円215円260円351円223円276円350円241円250円.java" | 32 +++++++ ...347円232円204円346円225円260円345円255円227円.java" | 38 +++++++++ ...345円276円205円345円244円204円347円220円206円.java" | 37 +++++++++ ...347円247円257円346円225円260円347円273円204円.java" | 36 ++++++++ ...347円232円204円350円267円257円345円276円204円.java" | 83 +++++++++++++++++++ 5 files changed, 226 insertions(+) create mode 100644 "docs/AimForOffer/344円273円216円345円260円276円345円210円260円345円244円264円346円211円223円345円215円260円351円223円276円350円241円250円.java" create mode 100644 "docs/AimForOffer/346円225円260円347円273円204円344円270円255円351円207円215円345円244円215円347円232円204円346円225円260円345円255円227円.java" create mode 100644 "docs/AimForOffer/346円234円272円345円231円250円344円272円272円347円232円204円350円277円220円345円212円250円350円214円203円345円233円264円_345円276円205円345円244円204円347円220円206円.java" create mode 100644 "docs/AimForOffer/346円236円204円345円273円272円344円271円230円347円247円257円346円225円260円347円273円204円.java" create mode 100644 "docs/AimForOffer/347円237円251円351円230円265円344円270円255円347円232円204円350円267円257円345円276円204円.java" diff --git "a/docs/AimForOffer/344円273円216円345円260円276円345円210円260円345円244円264円346円211円223円345円215円260円351円223円276円350円241円250円.java" "b/docs/AimForOffer/344円273円216円345円260円276円345円210円260円345円244円264円346円211円223円345円215円260円351円223円276円350円241円250円.java" new file mode 100644 index 0000000..c73507b --- /dev/null +++ "b/docs/AimForOffer/344円273円216円345円260円276円345円210円260円345円244円264円346円211円223円345円215円260円351円223円276円350円241円250円.java" @@ -0,0 +1,32 @@ +import java.util.ArrayList; + +/** + * Created by DHA on 2019年7月22日. + */ +public class 从尾到头打印链表 { + //从尾到头打印链表 + public ArrayList printListFromTailToHead(ListNode listNode) { + ArrayList res = new ArrayList(); + + //逆置该链表 + ListNode head = reverse(listNode); + while(head!=null){ + res.add(head.val); + head = head.next; + } + return res; + } + + private ListNode reverse(ListNode listNode){ + ListNode pre = null; + ListNode cur = listNode; + while(cur!=null){ + ListNode next = cur.next; + cur.next = pre; + + pre = cur; + cur = next; + } + return pre; + } +} diff --git "a/docs/AimForOffer/346円225円260円347円273円204円344円270円255円351円207円215円345円244円215円347円232円204円346円225円260円345円255円227円.java" "b/docs/AimForOffer/346円225円260円347円273円204円344円270円255円351円207円215円345円244円215円347円232円204円346円225円260円345円255円227円.java" new file mode 100644 index 0000000..3dd4684 --- /dev/null +++ "b/docs/AimForOffer/346円225円260円347円273円204円344円270円255円351円207円215円345円244円215円347円232円204円346円225円260円345円255円227円.java" @@ -0,0 +1,38 @@ +import org.junit.Test; + +/** + * Created by DHA on 2019年7月22日. + */ +public class 数组中重复的数字 { + //数组中重复的数字 + public boolean duplicate(int numbers[],int length,int [] duplication) { + for(int i=0;i0){ + sum += num %10; + num/=10; + } + return sum; + } + + + + public int movingCount(int threshold, int rows, int cols) { + + } + + @Test + public void test(){ + int num = 37; + System.out.println(getNum(num)); + } +} diff --git "a/docs/AimForOffer/346円236円204円345円273円272円344円271円230円347円247円257円346円225円260円347円273円204円.java" "b/docs/AimForOffer/346円236円204円345円273円272円344円271円230円347円247円257円346円225円260円347円273円204円.java" new file mode 100644 index 0000000..bb5acd5 --- /dev/null +++ "b/docs/AimForOffer/346円236円204円345円273円272円344円271円230円347円247円257円346円225円260円347円273円204円.java" @@ -0,0 +1,36 @@ +import org.junit.Test; + +/** + * Created by DHA on 2019年7月22日. + */ +public class 构建乘积数组 { + //构建乘积数组 + //思路: + //B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。 + //B[i] 可拆分成 (A[0]*A[1]*...*A[i-1])*(A[i+1]*...*A[n-1])两部分 + public int[] multiply(int[] A) { + int n = A.length; + int[] B = new int[n]; + + //B[i] = A[0]*A[1]*...*A[i-1] + int product =1; + for(int i=0;i=0;product*=A[i],i--){ + B[i]*= product; + } + return B; + } + + @Test + public void test(){ + int[] A={1,2,3,4,5}; + int[] B=multiply(A); + for(int num:B){ + System.out.println(num); + } + } +} diff --git "a/docs/AimForOffer/347円237円251円351円230円265円344円270円255円347円232円204円350円267円257円345円276円204円.java" "b/docs/AimForOffer/347円237円251円351円230円265円344円270円255円347円232円204円350円267円257円345円276円204円.java" new file mode 100644 index 0000000..37c982c --- /dev/null +++ "b/docs/AimForOffer/347円237円251円351円230円265円344円270円255円347円232円204円350円267円257円345円276円204円.java" @@ -0,0 +1,83 @@ +import org.junit.Test; + +/** + * Created by DHA on 2019年7月25日. + */ +public class 矩阵中的路径 { + private int m,n; //矩阵的长度、宽度 + private boolean[][] visted; //标记是否访问 + + private int[][] d={ + {0,1}, //向右 + {-1,0},//向上 + {0,-1},//向左 + {1,0} //向下 + }; + + private boolean inArea(int x,int y){ + return (x>=0 && x=0 && y Date: 2019年8月14日 20:29:41 +0800 Subject: [PATCH 2/2] Java --- docs/AimForOffer/MyQueue.java | 56 +++++++++++++ docs/AimForOffer/ThreadSafeLRUCache.java | 49 ++++++++++++ docs/AimForOffer/TreeLinkNode.java | 13 +++ docs/AimForOffer/TreeNode.java | 9 +++ ...52347円273円223円347円202円271円_Important.java" | 31 ++++++++ ...63345円217円260円351円230円266円_Important.java" | 32 ++++++++ ...344円272円214円345円217円211円346円240円221円.java" | 30 +++++++ ...347円232円204円346円225円260円345円255円227円.java" | 13 +-- ...17346円225円260円345円255円227円_Important.java" | 58 ++++++++++++++ ...42347円251円272円346円240円274円_Important.java" | 43 ++++++++++ ...347円216円260円351円230円237円345円210円227円.java" | 35 ++++++++ ...42350円246円206円347円233円226円_Important.java" | 37 +++++++++ ...347円232円204円350円267円257円345円276円204円.java" | 79 +------------------ ...350円267円263円345円217円260円351円230円266円.java" | 29 +++++++ 14 files changed, 424 insertions(+), 90 deletions(-) create mode 100644 docs/AimForOffer/MyQueue.java create mode 100644 docs/AimForOffer/ThreadSafeLRUCache.java create mode 100644 docs/AimForOffer/TreeLinkNode.java create mode 100644 docs/AimForOffer/TreeNode.java create mode 100644 "docs/AimForOffer/344円272円214円345円217円211円346円240円221円347円232円204円344円270円213円344円270円200円344円270円252円347円273円223円347円202円271円_Important.java" create mode 100644 "docs/AimForOffer/345円217円230円346円200円201円350円267円263円345円217円260円351円230円266円_Important.java" create mode 100644 "docs/AimForOffer/345円273円272円344円272円214円345円217円211円346円240円221円.java" create mode 100644 "docs/AimForOffer/346円227円213円350円275円254円346円225円260円347円273円204円347円232円204円346円234円200円345円260円217円346円225円260円345円255円227円_Important.java" create mode 100644 "docs/AimForOffer/346円233円277円346円215円242円347円251円272円346円240円274円_Important.java" create mode 100644 "docs/AimForOffer/347円224円250円344円270円244円344円270円252円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.java" create mode 100644 "docs/AimForOffer/347円237円251円345円275円242円350円246円206円347円233円226円_Important.java" create mode 100644 "docs/AimForOffer/350円267円263円345円217円260円351円230円266円.java" diff --git a/docs/AimForOffer/MyQueue.java b/docs/AimForOffer/MyQueue.java new file mode 100644 index 0000000..59d933c --- /dev/null +++ b/docs/AimForOffer/MyQueue.java @@ -0,0 +1,56 @@ +import java.util.Stack; + +/** + * Created by 18351 on 2019年7月23日. + */ +//使用两个栈实现队列的扩展 +public class MyQueue { + //用两个栈实现队列 + Stack stack1 = new Stack(); + Stack stack2 = new Stack(); + + //存储队列头元素 + private int front; //方便后面的 peek() 操作 + + /** Initialize your data structure here. */ + public MyQueue() { + stack1 = new Stack(); + stack2 = new Stack(); + } + + public void push(int node) { + if(stack1.isEmpty()){ + front = node; + } + stack1.push(node); + } + + public int pop() { + if(stack2.isEmpty()){ + while(!stack1.isEmpty()){ + stack2.push(stack1.pop()); + } + } + return stack2.pop(); + } + + public int peek(){ + if(stack2.isEmpty()){ + return front; + } + return stack2.peek(); + } + + public boolean empty(){ + return (stack1.isEmpty()) && (stack2.isEmpty()); + } + + public static void main(String[] args) { + MyQueue queue = new MyQueue(); + queue.push(1); + queue.push(2); + System.out.println(queue.peek()); // 返回 1 + System.out.println(queue.pop()); // 返回 1 + queue.empty(); // 返回 false + } +} diff --git a/docs/AimForOffer/ThreadSafeLRUCache.java b/docs/AimForOffer/ThreadSafeLRUCache.java new file mode 100644 index 0000000..b725030 --- /dev/null +++ b/docs/AimForOffer/ThreadSafeLRUCache.java @@ -0,0 +1,49 @@ +import org.junit.Test; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Created by 18351 on 2019年7月25日. + */ +public class ThreadSafeLRUCache { + private static final int MAX_CAPACITY = 3; + private LinkedHashMap linkedHashMap; + + public ThreadSafeLRUCache(){ + linkedHashMap = new LinkedHashMap(MAX_CAPACITY,0.75f,false){ + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return size()>MAX_CAPACITY; + } + }; + } + + public synchronized void put(K k,V v){ + linkedHashMap.put(k,v); + } + + public synchronized V get(K k){ + return (V)linkedHashMap.get(k); + } + + @Override + public String toString() { + StringBuilder sb=new StringBuilder(); + for(Map.Entry entry:linkedHashMap.entrySet()) + sb.append(String.format("%s: %s\n", entry.getKey(), entry.getValue())); + return sb.toString(); + } + + @Test + public void test(){ + ThreadSafeLRUCache cache=new ThreadSafeLRUCache(); + cache.put(1, "a"); + cache.put(2, "b"); + cache.put(3, "c"); + cache.get(1); + //LRU 键值1被访问过了,则最近最久未访问的就是2 + cache.put(4, "d"); + System.out.println(cache); + } +} diff --git a/docs/AimForOffer/TreeLinkNode.java b/docs/AimForOffer/TreeLinkNode.java new file mode 100644 index 0000000..c277e9e --- /dev/null +++ b/docs/AimForOffer/TreeLinkNode.java @@ -0,0 +1,13 @@ +/** + * Created by 18351 on 2019年7月23日. + */ +public class TreeLinkNode { + int val; + TreeLinkNode left = null; + TreeLinkNode right = null; + TreeLinkNode next = null; + + TreeLinkNode(int val) { + this.val = val; + } +} diff --git a/docs/AimForOffer/TreeNode.java b/docs/AimForOffer/TreeNode.java new file mode 100644 index 0000000..c9297d8 --- /dev/null +++ b/docs/AimForOffer/TreeNode.java @@ -0,0 +1,9 @@ +/** + * Created by 18351 on 2019年7月23日. + */ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + TreeNode(int x) { val = x; } +} diff --git "a/docs/AimForOffer/344円272円214円345円217円211円346円240円221円347円232円204円344円270円213円344円270円200円344円270円252円347円273円223円347円202円271円_Important.java" "b/docs/AimForOffer/344円272円214円345円217円211円346円240円221円347円232円204円344円270円213円344円270円200円344円270円252円347円273円223円347円202円271円_Important.java" new file mode 100644 index 0000000..7a2963c --- /dev/null +++ "b/docs/AimForOffer/344円272円214円345円217円211円346円240円221円347円232円204円344円270円213円344円270円200円344円270円252円347円273円223円347円202円271円_Important.java" @@ -0,0 +1,31 @@ +import java.util.Scanner; + +/** + * Created by 18351 on 2019年7月23日. + */ +public class 二叉树的下一个结点_Important { + //二叉树的下一个结点 + public TreeLinkNode GetNext(TreeLinkNode pNode){ + if(pNode==null){ + return null; + } + if(pNode.right!=null){ + //先判断 pNode 的右子树不为 null, + //pNode 的下一个结点就是 pNode 右子树的最左结点 + TreeLinkNode node = pNode.right; //pNode 的右子树 + while(node.left!=null){ + node = node.left; + } + return node; + }else{ //否则,向上找第一个左连接指向的树包含该节点的祖先节点 + while (pNode.next!=null){ + TreeLinkNode parent = pNode.next; + if(parent.left==pNode){ + return parent; + } + pNode = pNode.next; + } + } + return null; + } +} diff --git "a/docs/AimForOffer/345円217円230円346円200円201円350円267円263円345円217円260円351円230円266円_Important.java" "b/docs/AimForOffer/345円217円230円346円200円201円350円267円263円345円217円260円351円230円266円_Important.java" new file mode 100644 index 0000000..ceaa998 --- /dev/null +++ "b/docs/AimForOffer/345円217円230円346円200円201円350円267円263円345円217円260円351円230円266円_Important.java" @@ -0,0 +1,32 @@ +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by 18351 on 2019年7月24日. + */ +public class 变态跳台阶_Important { + //动态规划思路: + //dp[i] 表示跳 i 阶台阶共有 dp[i] 中跳法 + public int JumpFloorII(int target) { + int[] dp=new int[target+1]; + //dp 数组数值初始化为 1 + Arrays.fill(dp,1); + + for(int i=2;i<=target;i++){ + for(int j=1;jpreEnd || inStart>inEnd){ + return null; + } + TreeNode root = new TreeNode(pre[preStart]); + + int index = -1; + for(int i=inStart;i<=inend;i++){ + if(in[i]==pre[preStart]){ + index = i; + break; + } + } + root.left = reConstructBinaryTree(pre,in, + preStart+1,preStart+(index-inStart),inStart,index-1); + root.right = reConstructBinaryTree(pre,in, + preStart+(index-inStart)+1,preEnd,index+1,inEnd); + return root; + } +} diff --git "a/docs/AimForOffer/346円225円260円347円273円204円344円270円255円351円207円215円345円244円215円347円232円204円346円225円260円345円255円227円.java" "b/docs/AimForOffer/346円225円260円347円273円204円344円270円255円351円207円215円345円244円215円347円232円204円346円225円260円345円255円227円.java" index 3dd4684..54c4855 100644 --- "a/docs/AimForOffer/346円225円260円347円273円204円344円270円255円351円207円215円345円244円215円347円232円204円346円225円260円345円255円227円.java" +++ "b/docs/AimForOffer/346円225円260円347円273円204円344円270円255円351円207円215円345円244円215円347円232円204円346円225円260円345円255円227円.java" @@ -1,10 +1,7 @@ -import org.junit.Test; - /** - * Created by DHA on 2019/7/22. + * Created by 18351 on 2019/7/23. */ public class 数组中重复的数字 { - //数组中重复的数字 public boolean duplicate(int numbers[],int length,int [] duplication) { for(int i=0;iarray[i]){ + res = array[i]; + break; + } + } + return res; + } + + //二分查找的变形: + //关键在于确定旋转数组在那一部分 + public int minNumberInRotateArray(int [] array) { + if(array==null || array.length==0){ + return 0; + } + if(array.length==1){ + return array[0]; + } + + int l=0; + int h=array.length-1; + while(l=0 && P2>= P1){ + char ch = str.charAt(P1); + if(ch==' '){ + str.setCharAt(P2--,'0'); + str.setCharAt(P2--,'2'); + str.setCharAt(P2--,'%'); + }else{ + str.setCharAt(P2--,ch); + } + P1--; + } + return str.toString(); + } + + @Test + public void test(){ + StringBuffer buffer = new StringBuffer(); + buffer.append("A B C"); + System.out.println(replaceSpace(buffer).toString()); + } +} diff --git "a/docs/AimForOffer/347円224円250円344円270円244円344円270円252円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.java" "b/docs/AimForOffer/347円224円250円344円270円244円344円270円252円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.java" new file mode 100644 index 0000000..50a4694 --- /dev/null +++ "b/docs/AimForOffer/347円224円250円344円270円244円344円270円252円346円240円210円345円256円236円347円216円260円351円230円237円345円210円227円.java" @@ -0,0 +1,35 @@ +import java.util.Stack; + +/** + * Created by 18351 on 2019年7月23日. + */ +public class 用两个栈实现队列 { + //用两个栈实现队列 + Stack stack1 = new Stack(); + Stack stack2 = new Stack(); + int front; //用来记录第一个出栈元素 + + public void push(int node) { + if (stack1.isEmpty()){ + front = node; + } + stack1.push(node); + } + + public int pop() { + if(stack2.isEmpty()){ + while(!stack1.isEmpty()){ + stack2.push(stack1.pop()); + } + } + return stack2.pop(); + } + + public static void main(String[] args) { + 用两个栈实现队列 q = new 用两个栈实现队列(); + q.push(1); + q.push(2); + q.push(3); + q.pop(); + } +} diff --git "a/docs/AimForOffer/347円237円251円345円275円242円350円246円206円347円233円226円_Important.java" "b/docs/AimForOffer/347円237円251円345円275円242円350円246円206円347円233円226円_Important.java" new file mode 100644 index 0000000..45160bf --- /dev/null +++ "b/docs/AimForOffer/347円237円251円345円275円242円350円246円206円347円233円226円_Important.java" @@ -0,0 +1,37 @@ +import org.junit.Test; + +import java.util.WeakHashMap; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Created by 18351 on 2019年7月23日. + */ +public class 矩形覆盖_Important { + //思路: + //n=1 时,1 种 + //n=2 时,2 种 + //n=3 时,3 种 + //n=4 时,5 种 + //f(n) = f(n-1) + f(n-2) + //注意:时间复杂度 + //使用递归的方式时间复杂度较高 + public int RectCover(int target) { + if (target==1){ + return 1; + } + if(target==2){ + return 2; + } + + //TODO:循环方式替代递归方式 + int pre1 = 1; + int pre2 = 2; + int res = 0; + for(int i=3;i<=target;i++){ + res = pre1 + pre2; + pre1 = pre2; + pre2 = res; + } + return res; + } +} diff --git "a/docs/AimForOffer/347円237円251円351円230円265円344円270円255円347円232円204円350円267円257円345円276円204円.java" "b/docs/AimForOffer/347円237円251円351円230円265円344円270円255円347円232円204円350円267円257円345円276円204円.java" index 37c982c..86f084f 100644 --- "a/docs/AimForOffer/347円237円251円351円230円265円344円270円255円347円232円204円350円267円257円345円276円204円.java" +++ "b/docs/AimForOffer/347円237円251円351円230円265円344円270円255円347円232円204円350円267円257円345円276円204円.java" @@ -1,83 +1,6 @@ -import org.junit.Test; - /** - * Created by DHA on 2019/7/25. + * Created by 18351 on 2019/7/25. */ public class 矩阵中的路径 { - private int m,n; //矩阵的长度、宽度 - private boolean[][] visted; //标记是否访问 - - private int[][] d={ - {0,1}, //向右 - {-1,0},//向上 - {0,-1},//向左 - {1,0} //向下 - }; - - private boolean inArea(int x,int y){ - return (x>=0 && x=0 && y

AltStyle によって変換されたページ (->オリジナル) /