├── README.md └── src └── BAT ├── BinSearch ├── TheLeft.java ├── getLessIndex.java └── minNumberInRotateArray.java ├── BinTree ├── basic │ ├── TreeNode.java │ ├── getHeight.java │ ├── isBanlance.java │ ├── levelprint.java │ └── print.java ├── 中序遍历的序列.java ├── 判断完全二叉树.java ├── 判断平衡二叉树.java ├── 判断搜索二叉树.java ├── 判断满二叉树.java ├── 后序遍历序列.java ├── 序列化二叉树.java ├── 按行打印二叉树.java ├── 有父节点的树找后继节点.java ├── 非递归中序.java ├── 非递归先序.java └── 非递归后序.java ├── Bit ├── BrownFilter.java ├── Compare.java ├── OddAppearance.java ├── Swap.java ├── Test.java ├── TwoOddAppearance.java └── cipher.java ├── DP ├── 把字符串1修改成字符串2的最大开销.java ├── 把钱分成多种面值的硬币.java ├── 最长公共子序列.java ├── 最长递增子序列.java ├── 矩阵中走的最短路径.java ├── 背包01.java └── 跳台阶的方法数.java ├── LinkedList ├── basic │ ├── Node.java │ ├── forList.java │ └── 逆序.java ├── 判断链表是否回文结构.java ├── 单链表每k个元素逆序.java ├── 复杂链表的复制.java ├── 打印两个链表的公共值练题.java ├── 有序链表的公共部分.java ├── 环形链表插值.java ├── 相交及有环问题 │ ├── 任意单链表的相交.java │ ├── 无环链表相交.java │ ├── 有环链表相交.java │ └── 链表有环.java └── 链表的荷兰旗问题.java ├── QueueAndStack ├── BFS.java ├── DFS.java ├── 两个栈实现一个队列.java ├── 单变量排序栈元素.java ├── 栈的原地逆序.java ├── 滑动窗口的最大值.java └── 获取栈中的最小值.java ├── Sort ├── basic │ ├── HeapSort.java │ ├── Mergesort.java │ ├── bubblesort.java │ ├── dividesort.java │ ├── insertsort.java │ ├── quicksort.java │ ├── quicksort2.java │ └── selectsort.java ├── 小范围排序.java ├── 是否有数重复出现.java ├── 有序数组合并.java ├── 有序矩阵查找.java ├── 相邻两数最大差值.java ├── 荷兰国旗.java └── 需要排序的最短子序列.java ├── String ├── Basic │ ├── KMP.java │ └── 字符串逆序.java ├── 判断二叉树的子结构.java ├── 判断有效括号字符串.java ├── 变形词.java ├── 字符串的最长无重复子序列.java ├── 旋转字符串.java ├── 旋转词.java ├── 替换字符串中的空格.java ├── 最小字典序字符串数组.java └── 英文句子逆序.java └── 大数据 ├── AgeSort.java ├── MR.java ├── WordCount.java └── ipSort.java /README.md: -------------------------------------------------------------------------------- 1 | # Nowcoder 2 | 牛客算法 与 2018年算法笔试面试题 3 | 4 | ## 二分查找 5 | 6 | ## 二叉树 7 | 8 | ## 位运算 9 | 10 | ## 动态规划 11 | 12 | ## 链表 13 | 14 | ## 队列和栈 15 | 16 | ## 字符串 17 | 18 | ## 排序 19 | 20 | ## 大数据 21 | 22 | ## 概率 23 | 24 | ## 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/BAT/BinSearch/TheLeft.java: -------------------------------------------------------------------------------- 1 | package BAT.BinSearch; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/16. 5 | * 有序数组中元素最左出现的位置。 6 | */ 7 | public class TheLeft { 8 | 9 | public int leftNum(int []arr, int x) { 10 | int res = -1; 11 | int size = arr.length; 12 | if(size == 0) return -1; 13 | if(size == 1 ){ 14 | if(x == arr[0]){ 15 | return 0; 16 | }else return -1; 17 | } 18 | 19 | int l=0,r=size-1; 20 | while(l<=r) { 21 | int mid = l + (r-l)/2; 22 | if(arr[mid]>x){ 23 | r = mid -1; 24 | } 25 | else if(arr[mid]arr[size-1]){ 13 | return size - 1; 14 | } 15 | if(arr[0]arr[mid+1]){ 23 | l = mid+1; 24 | } 25 | else if(arr[mid]>arr[mid-1]){ 26 | r = mid -1; 27 | } 28 | else return mid; 29 | } 30 | return -1; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/BAT/BinSearch/minNumberInRotateArray.java: -------------------------------------------------------------------------------- 1 | package BAT.BinSearch; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/16. 5 | * 有序循环数组的最小值 6 | */ 7 | public class minNumberInRotateArray { 8 | public int minNumberInRotateArray(int [] array) { 9 | 10 | int size = array.length; 11 | if(size ==0 )return 0; 12 | if(size ==1 )return array[0]; 13 | if(array[0]array[r]) 22 | { 23 | l=mid+1; 24 | } 25 | else if(array[mid] right ? left : right) + 1; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/BAT/BinTree/basic/isBanlance.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree.basic; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/27. 5 | */ 6 | public class isBanlance { 7 | public static void main(String[] args) { 8 | 9 | } 10 | boolean isBanlance(TreeNode t) { 11 | 12 | if (t == null) return true; 13 | int left = getHeight.getHeight(t.left); 14 | int right = getHeight.getHeight(t.right); 15 | if(Math.abs(left - right) > 1) { 16 | return false; 17 | } 18 | return isBanlance(t.left) && isBanlance(t.right); 19 | 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/BAT/BinTree/basic/levelprint.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree.basic; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class levelprint { 7 | public static void initTree(TreeNode root){ 8 | root.val=0; 9 | TreeNode treeNode1=new TreeNode(1); 10 | TreeNode treeNode2=new TreeNode(3); 11 | TreeNode treeNode3=new TreeNode(2); 12 | TreeNode treeNode4=new TreeNode(4); 13 | root.left=treeNode1; 14 | treeNode2.left=treeNode3; 15 | treeNode2.right=treeNode4; 16 | root.right=treeNode2; 17 | 18 | } 19 | public static void levelvisit(TreeNode root) { 20 | Queue queue = new LinkedList(); 21 | if(root!=null){ 22 | queue.offer(root); 23 | 24 | } 25 | while(!queue.isEmpty()){ 26 | TreeNode p=queue.poll(); 27 | System.out.println(p.val); 28 | if(p.left!=null){ 29 | queue.offer(p.left); 30 | } 31 | if(p.right!=null){ 32 | queue.offer(p.right); 33 | } 34 | 35 | } 36 | } 37 | public static void main(String args[]){ 38 | TreeNode T=new TreeNode(); 39 | initTree(T); 40 | levelvisit(T); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/BAT/BinTree/basic/print.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree.basic; 2 | 3 | /** 4 | * Created by 周杰伦 on 2017/3/10. 5 | */ 6 | public class print { 7 | public static void preprint(TreeNode root){ 8 | if(root!=null){ 9 | System.out.print(root.val+" "); 10 | preprint(root.left); 11 | preprint(root.right); 12 | } 13 | 14 | } 15 | 16 | public static void inprint(TreeNode root){ 17 | 18 | if(root!=null){ 19 | inprint(root.left); 20 | System.out.print(root.val+" "); 21 | inprint(root.right); 22 | } 23 | 24 | 25 | } 26 | 27 | public static void postprint(TreeNode root){ 28 | 29 | 30 | if(root!=null){ 31 | postprint(root.left); 32 | postprint(root.right); 33 | System.out.print(root.val+" "); 34 | 35 | } 36 | } 37 | 38 | public static void main(String args[]){ 39 | TreeNode t=new TreeNode(); 40 | levelprint.initTree(t); 41 | postprint(t); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/BAT/BinTree/中序遍历的序列.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | import BAT.BinTree.basic.TreeNode; 3 | 4 | import java.util.ArrayList; 5 | 6 | /** 7 | * Created by 周杰伦 on 2018/3/27. 8 | * * 0 9 | * 1 2 10 | 3 4 11 | */ 12 | public class 中序遍历的序列 { 13 | public static void main(String[] args) { 14 | TreeNode root = new TreeNode(1); 15 | TreeNode treeNode1=new TreeNode(0); 16 | TreeNode treeNode2=new TreeNode(3); 17 | TreeNode treeNode3=new TreeNode(2); 18 | TreeNode treeNode4=new TreeNode(4); 19 | root.left=treeNode1; 20 | treeNode2.left=treeNode3; 21 | treeNode2.right=treeNode4; 22 | root.right=treeNode2; 23 | 24 | ArrayList list = inOrderSeq(root); 25 | for (TreeNode p : list) { 26 | System.out.println(p.val); 27 | } 28 | } 29 | static ArrayList list = new ArrayList<>(); 30 | public static ArrayList inOrderSeq(TreeNode TreeNode) { 31 | if (TreeNode == null) return null; 32 | inOrderSeq(TreeNode.left); 33 | list.add(TreeNode); 34 | inOrderSeq(TreeNode.right); 35 | return list; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/BAT/BinTree/判断完全二叉树.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | import java.util.LinkedList; 6 | import java.util.Queue; 7 | 8 | /** 9 | * Created by 周杰伦 on 2018/3/27. 10 | * * 0 11 | * 1 2 12 | 3 4 13 | */ 14 | public class 判断完全二叉树 { 15 | public static void main(String[] args) { 16 | TreeNode root = new TreeNode(0); 17 | TreeNode treeNode1=new TreeNode(1); 18 | TreeNode treeNode2=new TreeNode(2); 19 | TreeNode treeNode3=new TreeNode(3); 20 | TreeNode treeNode4=new TreeNode(4); 21 | root.left=treeNode1; 22 | treeNode1.left=treeNode3; 23 | treeNode1.right=treeNode4; 24 | root.right=treeNode2; 25 | System.out.println(isTotally(root)); 26 | } 27 | static boolean isTotally(TreeNode t) { 28 | if (t == null) return true; 29 | Queue queue = new LinkedList<>(); 30 | queue.offer(t); 31 | int flag = 0; 32 | while (!queue.isEmpty()) { 33 | TreeNode node = queue.poll(); 34 | if (flag == 1) { 35 | if (node.left != null || node.right != null) { 36 | return false; 37 | } 38 | } 39 | if (node.left == null && node.right != null) { 40 | return false; 41 | } 42 | else if (node.left == null || node.right == null) { 43 | flag = 1; 44 | } 45 | if (node.left != null) { 46 | queue.offer(node.left); 47 | } 48 | if (node.right != null) { 49 | queue.offer(node.right); 50 | } 51 | } 52 | return true; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/BAT/BinTree/判断平衡二叉树.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | import BAT.BinTree.basic.getHeight; 5 | 6 | /** 7 | * Created by 周杰伦 on 2018/3/27. 8 | */ 9 | public class 判断平衡二叉树 { 10 | boolean isBanlance(TreeNode t) { 11 | 12 | if (t == null) return true; 13 | int left = getHeight.getHeight(t.left); 14 | int right = getHeight.getHeight(t.right); 15 | if(Math.abs(left - right) > 1) { 16 | return false; 17 | } 18 | return isBanlance(t.left) && isBanlance(t.right); 19 | 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/BAT/BinTree/判断搜索二叉树.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | import java.util.Stack; 6 | 7 | /** 8 | * Created by 周杰伦 on 2018/3/27. 9 | *根据中序遍历的非递归来写,前一个节点要比后一个节点小 10 | * 或者直接中序遍历,序列有序即可 11 | * * 1 12 | * 0 3 13 | 2 4 14 | */ 15 | public class 判断搜索二叉树 { 16 | public static void main(String[] args) { 17 | TreeNode root = new TreeNode(1); 18 | TreeNode treeNode1=new TreeNode(0); 19 | TreeNode treeNode2=new TreeNode(3); 20 | TreeNode treeNode3=new TreeNode(2); 21 | TreeNode treeNode4=new TreeNode(4); 22 | root.left=treeNode1; 23 | treeNode2.left=treeNode3; 24 | treeNode2.right=treeNode4; 25 | root.right=treeNode2; 26 | System.out.println(isSearch(root)); 27 | } 28 | 29 | public static boolean isSearch(TreeNode t) { 30 | if (t == null) return true; 31 | Stack stack = new Stack<>(); 32 | TreeNode cur = t; 33 | TreeNode pre = null; 34 | while (!stack.isEmpty() || cur != null) { 35 | while (cur != null) { 36 | stack.push(cur); 37 | cur = cur.left; 38 | } 39 | 40 | TreeNode node = stack.pop(); 41 | if (pre != null && pre.val >= node.val) return false; 42 | pre = node; 43 | 44 | if (node.right != null) { 45 | cur = node.right; 46 | } 47 | } 48 | return true; 49 | } 50 | 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/BAT/BinTree/判断满二叉树.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | import BAT.BinTree.basic.getHeight; 5 | 6 | /** 7 | * Created by 周杰伦 on 2018/3/27. 8 | */ 9 | public class 判断满二叉树 { 10 | public static void main(String[] args) { 11 | 12 | } 13 | public boolean isFull(TreeNode t) { 14 | if (t == null) return true; 15 | int l = getHeight.getHeight(t); 16 | int count = count(t); 17 | if (Math.pow(2,l)-1 == count) { 18 | return true; 19 | } 20 | else return false; 21 | } 22 | int count = 0; 23 | public int count(TreeNode t) { 24 | if (t == null)return 0; 25 | if (t != null)count ++; 26 | count(t.left); 27 | count(t.right); 28 | return count; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/BAT/BinTree/后序遍历序列.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/27. 5 | */ 6 | public class 后序遍历序列 { 7 | public static void main(String[] args) { 8 | 9 | } 10 | public boolean VerifySquenceOfBST(int [] sequence) { 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/BAT/BinTree/序列化二叉树.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/16. 7 | */ 8 | public class 序列化二叉树 { 9 | static StringBuffer sb = new StringBuffer(); 10 | static String Serialize(TreeNode root) { 11 | if(root == null) { 12 | sb.append("#!") ; 13 | } 14 | else { 15 | sb.append(root.val + "!"); 16 | Serialize(root.left); 17 | Serialize(root.right); 18 | } 19 | 20 | return sb.toString(); 21 | } 22 | 23 | public static int index = -1; 24 | static TreeNode Deserialize(String str) { 25 | index ++; 26 | int len = str.length(); 27 | if(index >= len) { 28 | return null; 29 | } 30 | String[] strr = str.split("!"); 31 | TreeNode node = null; 32 | if(!strr[index].equals("#")) { 33 | node = new TreeNode(Integer.valueOf(strr[index])); 34 | node.left = Deserialize(str); 35 | node.right = Deserialize(str); 36 | } 37 | return node; 38 | 39 | } 40 | public static void main(String[] args) { 41 | TreeNode t = TreeNode.getNode(); 42 | System.out.println(Serialize(t)); 43 | 按行打印二叉树.printByRow(Deserialize(Serialize(t))); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/BAT/BinTree/按行打印二叉树.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | import java.util.*; 6 | 7 | /** 8 | * Created by 周杰伦 on 2018/3/14. 9 | */ 10 | public class 按行打印二叉树 { 11 | ArrayList> Print(TreeNode pRoot) { 12 | Queue queue = new LinkedList<>(); 13 | TreeNode root = pRoot; 14 | if(root == null) { 15 | return new ArrayList<>(); 16 | } 17 | TreeNode last = root; 18 | TreeNode nlast = root; 19 | queue.offer(root); 20 | ArrayList list = new ArrayList<>(); 21 | list.add(root); 22 | list.add(null); 23 | while (!queue.isEmpty()){ 24 | 25 | TreeNode t = queue.poll(); 26 | 27 | if(t.left != null) { 28 | queue.offer(t.left); 29 | list.add(t.left); 30 | nlast = t.left; 31 | } 32 | if(t.right != null) { 33 | queue.offer(t.right); 34 | list.add(t.right); 35 | nlast = t.right; 36 | } 37 | if(t == last) { 38 | if(!queue.isEmpty()) { 39 | list.add(null); 40 | last = nlast; 41 | } 42 | } 43 | } 44 | 45 | ArrayList> arr = new ArrayList<>(); 46 | ArrayList list1 = new ArrayList<>(); 47 | for(TreeNode t :list) { 48 | if( t != null) { 49 | list1.add(t.val); 50 | } 51 | else { 52 | 53 | ArrayList temp = new ArrayList<>(); 54 | temp.addAll(list1); 55 | arr.add(temp); 56 | list1.clear(); 57 | } 58 | } 59 | 60 | return arr; 61 | } 62 | 63 | 64 | public static void main(String[] args) { 65 | 66 | TreeNode t1 = new TreeNode(); 67 | t1.val=1; 68 | TreeNode t2 = new TreeNode(); 69 | t2.val=2; 70 | TreeNode t3 = new TreeNode(); 71 | t3.val=3; 72 | TreeNode t4 = new TreeNode(); 73 | t4.val=4; 74 | TreeNode t5 = new TreeNode(); 75 | t5.val=5; 76 | TreeNode t6 = new TreeNode(); 77 | t6.val=6; 78 | TreeNode t7 = new TreeNode(); 79 | t7.val=7; 80 | TreeNode t8 = new TreeNode(); 81 | t8.val=8; 82 | 83 | t5.left = t7; 84 | t5.right = t8; 85 | t3.left = t5; 86 | t3.right = t6; 87 | t2.left = t4; 88 | t1.left = t2; 89 | t1.right = t3; 90 | printByRow(t1); 91 | for(ArrayList list : printByRow(t1)) { 92 | for(Integer i : list) { 93 | System.out.print(i+ " "); 94 | } 95 | System.out.println(); 96 | } 97 | } 98 | 99 | public static ArrayList> printByRow(TreeNode root) { 100 | Queue queue = new LinkedList<>(); 101 | if(root == null) { 102 | return null; 103 | } 104 | TreeNode last = root; 105 | TreeNode nlast = root; 106 | queue.offer(root); 107 | ArrayList list = new ArrayList<>(); 108 | list.add(root); 109 | list.add(null); 110 | while (!queue.isEmpty()){ 111 | TreeNode t = queue.poll(); 112 | if(t.left != null) { 113 | queue.offer(t.left); 114 | list.add(t.left); 115 | nlast = t.left; 116 | } 117 | if(t.right != null) { 118 | queue.offer(t.right); 119 | list.add(t.right); 120 | nlast = t.right; 121 | } 122 | if(t == last) { 123 | if(!queue.isEmpty()) { 124 | list.add(null); 125 | last = nlast; 126 | } 127 | } 128 | } 129 | for(TreeNode t: list) { 130 | if(t != null) { 131 | System.out.print(t.val + " "); 132 | }else { 133 | System.out.println(); 134 | } 135 | } 136 | ArrayList> arr = new ArrayList<>(); 137 | ArrayList list1 = new ArrayList<>(); 138 | for(TreeNode t :list) { 139 | 140 | if( t != null) { 141 | list1.add(t.val); 142 | } 143 | else { 144 | arr.add(list1); 145 | list1.clear(); 146 | } 147 | } 148 | return arr; 149 | } 150 | //其余二叉树算法 先序中序后序 层次 151 | public void prior(TreeNode root){ 152 | System.out.println(root); 153 | prior(root.left); 154 | prior(root.right); 155 | } 156 | public void mid(TreeNode root){ 157 | prior(root.left); 158 | System.out.println(root); 159 | prior(root.right); 160 | 161 | } 162 | public void rear(TreeNode root){ 163 | prior(root.left); 164 | prior(root.right); 165 | System.out.println(root); 166 | } 167 | public void BFS(TreeNode root){ 168 | Queue queue = new LinkedList<>(); 169 | queue.offer(root); 170 | while (!queue.isEmpty()){ 171 | TreeNode t = queue.poll(); 172 | System.out.println(t.val); 173 | if(t.left != null) { 174 | queue.offer(t.left); 175 | } 176 | if(t.right != null) { 177 | queue.offer(t.right); 178 | } 179 | } 180 | } 181 | 182 | public void DFS(TreeNode root){ 183 | TreeNode t = root; 184 | if (t != null) { 185 | System.out.println(t.val); 186 | if (t.left != null) { 187 | DFS(t.left); 188 | } 189 | if(t.right != null) { 190 | DFS(t.right); 191 | } 192 | } 193 | 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /src/BAT/BinTree/有父节点的树找后继节点.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | import java.util.ArrayList; 6 | 7 | import static BAT.BinTree.中序遍历的序列.inOrderSeq; 8 | 9 | /** 10 | * Created by 周杰伦 on 2018/3/27. 11 | */ 12 | public class 有父节点的树找后继节点 { 13 | 14 | public static void main(String[] args) { 15 | 16 | } 17 | public TreeNode findNextNode(PNode anynode) { 18 | if (anynode == null) return null; 19 | PNode p = anynode; 20 | while (p.parent != null) { 21 | p = p.parent; 22 | } 23 | ArrayList list = inOrderSeq(p); 24 | for (int i = 0;i < list.size();i ++) { 25 | if (list.get(i) == anynode) { 26 | if (i + 1 < list.size()) { 27 | return list.get(i + 1); 28 | } 29 | else return null; 30 | } 31 | } 32 | return null; 33 | 34 | } 35 | } 36 | class PNode extends TreeNode{ 37 | public int val; 38 | public PNode left; 39 | public PNode right; 40 | public PNode parent; 41 | 42 | public PNode(int val){ 43 | this.val = val; 44 | } 45 | public PNode(){} 46 | } 47 | -------------------------------------------------------------------------------- /src/BAT/BinTree/非递归中序.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | import java.util.Stack; 6 | 7 | /** 8 | * Created by 周杰伦 on 2018/3/27. 9 | * * 0 10 | * 1 2 11 | 3 4 12 | */ 13 | public class 非递归中序 { 14 | public static void main(String[] args) { 15 | TreeNode root = new TreeNode(0); 16 | TreeNode treeNode1=new TreeNode(1); 17 | TreeNode treeNode2=new TreeNode(2); 18 | TreeNode treeNode3=new TreeNode(3); 19 | TreeNode treeNode4=new TreeNode(4); 20 | root.left=treeNode1; 21 | treeNode2.left=treeNode3; 22 | treeNode2.right=treeNode4; 23 | root.right=treeNode2; 24 | inOrder(root); 25 | } 26 | public static void inOrder(TreeNode t) { 27 | if (t == null) return; 28 | Stack stack = new Stack<>(); 29 | TreeNode cur = t; 30 | while (!stack.isEmpty() || cur != null) { 31 | while (cur != null) { 32 | stack.push(cur); 33 | cur = cur.left; 34 | } 35 | 36 | TreeNode node = stack.pop(); 37 | System.out.println(node.val); 38 | if (node.right != null) { 39 | cur = node.right; 40 | } 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/BAT/BinTree/非递归先序.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | import java.util.Stack; 6 | 7 | /** 8 | * Created by 周杰伦 on 2018/3/27. 9 | * 0 10 | * 1 2 11 | 3 4 12 | */ 13 | public class 非递归先序 { 14 | public static void main(String[] args) { 15 | TreeNode root = new TreeNode(0); 16 | TreeNode treeNode1=new TreeNode(1); 17 | TreeNode treeNode2=new TreeNode(2); 18 | TreeNode treeNode3=new TreeNode(3); 19 | TreeNode treeNode4=new TreeNode(4); 20 | root.left=treeNode1; 21 | treeNode2.left=treeNode3; 22 | treeNode2.right=treeNode4; 23 | root.right=treeNode2; 24 | preOrder(root); 25 | 26 | } 27 | static Stack stack = new Stack<>(); 28 | public static void preOrder(TreeNode t) { 29 | if (t == null) { 30 | return; 31 | } 32 | stack.push(t); 33 | while (!stack.isEmpty()) { 34 | TreeNode next = stack.pop(); 35 | System.out.println(next.val); 36 | if (next.right != null) { 37 | stack.push(next.right); 38 | } 39 | if (next.left != null) { 40 | stack.push(next.left); 41 | } 42 | } 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/BAT/BinTree/非递归后序.java: -------------------------------------------------------------------------------- 1 | package BAT.BinTree; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | import java.util.Stack; 6 | 7 | /** 8 | * Created by 周杰伦 on 2018/3/27. 9 | * 0 10 | * 1 2 11 | 3 4 12 | */ 13 | public class 非递归后序 { 14 | public static void main(String[] args) { 15 | TreeNode root = new TreeNode(0); 16 | TreeNode treeNode1=new TreeNode(1); 17 | TreeNode treeNode2=new TreeNode(2); 18 | TreeNode treeNode3=new TreeNode(3); 19 | TreeNode treeNode4=new TreeNode(4); 20 | root.left=treeNode1; 21 | treeNode2.left=treeNode3; 22 | treeNode2.right=treeNode4; 23 | root.right=treeNode2; 24 | postOrder(root); 25 | postOrder2(root); 26 | } 27 | public static void postOrder(TreeNode t) { 28 | if (t == null) return; 29 | Stack stack = new Stack<>(); 30 | Stack res = new Stack<>(); 31 | stack.push(t); 32 | while (!stack.isEmpty()) { 33 | TreeNode n = stack.pop(); 34 | res.push(n); 35 | if (n.left != null) { 36 | stack.push(n.left); 37 | } 38 | if (n.right != null) { 39 | stack.push(n.right); 40 | } 41 | } 42 | while (!res.isEmpty()) { 43 | System.out.println(res.pop().val); 44 | } 45 | } 46 | public static void postOrder2(TreeNode t) { 47 | if (t == null) return; 48 | Stack stack = new Stack<>(); 49 | TreeNode h = t; 50 | TreeNode c = null; 51 | stack.push(t); 52 | while (!stack.isEmpty()) { 53 | c = stack.peek(); 54 | if (c.left != null && c.left != h && c.right != h) { 55 | stack.push(c.left); 56 | } 57 | else if (c.right != null && c.right != h) { 58 | stack.push(c.right); 59 | } 60 | else { 61 | h = stack.pop(); 62 | System.out.println(h.val); 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/BAT/Bit/BrownFilter.java: -------------------------------------------------------------------------------- 1 | package BAT.Bit; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/19. 5 | */ 6 | public class BrownFilter { 7 | } 8 | -------------------------------------------------------------------------------- /src/BAT/Bit/Compare.java: -------------------------------------------------------------------------------- 1 | package BAT.Bit; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/19. 5 | */ 6 | public class Compare { 7 | public static int getBiggerWithoutCompare(int a,int b) { 8 | int c = a - b; 9 | int sc = sign(c); 10 | int sa = sc; 11 | int sb = flip(sa); 12 | return sa*a + sb*b; 13 | } 14 | //用于反转0和1 15 | static int flip(int x){ 16 | return x^1; 17 | } 18 | //0代表负数 19 | //1代表正数 20 | static int sign(int x){ 21 | return flip((x>>31)&1); 22 | } 23 | 24 | public static void main(String[] args) { 25 | int a =-4;int b =-8; 26 | System.out.println(getBiggerWithoutCompare(a,b)); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/BAT/Bit/OddAppearance.java: -------------------------------------------------------------------------------- 1 | package BAT.Bit; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/19. 5 | */ 6 | public class OddAppearance { 7 | static int getOddNum(String str) { 8 | int e0 = 0; 9 | char[] arr = str.toCharArray(); 10 | for(int i = 0;i < arr.length; i ++) { 11 | e0 = e0^arr[i]; 12 | } 13 | return e0; 14 | } 15 | 16 | public static void main(String[] args) { 17 | String s = "abbdeedgccaff"; 18 | System.out.println((char)getOddNum(s)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/BAT/Bit/Swap.java: -------------------------------------------------------------------------------- 1 | package BAT.Bit; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/19. 5 | */ 6 | public class Swap { 7 | public static void swap(int a, int b){ 8 | System.out.println(a); 9 | System.out.println(b); 10 | a = a^b; 11 | b = a^b; 12 | a = a^b; 13 | System.out.println(a); 14 | System.out.println(b); 15 | } 16 | 17 | public static void main(String[] args) { 18 | int a =1; 19 | int b =2; 20 | swap(a,b); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/BAT/Bit/Test.java: -------------------------------------------------------------------------------- 1 | package BAT.Bit; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/19. 5 | */ 6 | public class Test { 7 | public static void main(String[] args) { 8 | int a = 2; 9 | System.out.println(Integer.toBinaryString(2>>31)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/BAT/Bit/TwoOddAppearance.java: -------------------------------------------------------------------------------- 1 | package BAT.Bit; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/19. 5 | */ 6 | public class TwoOddAppearance { 7 | static int[] getTwoOddNum(String str) { 8 | int e0 = 0; 9 | char[] arr = str.toCharArray(); 10 | for (int i = 0;i < arr.length; i ++) { 11 | e0 = e0^arr[i]; 12 | } 13 | //e0为两个不同数异或的结果且不为0。 14 | //所以e0的32位中至少一位不为0。设为k。 15 | //说明a和b在第k位一个是0,一个是1 16 | int a = 0; 17 | int b = 0; 18 | int e1 = 0; 19 | int k = 0; 20 | int temp = e0; 21 | while ( (temp & 1) != 1) { 22 | temp >>= 1; 23 | k ++; 24 | } 25 | double help = Math.pow(2.0, k); 26 | for (int i = 0;i < arr.length;i ++) { 27 | if (1 == ((arr[i]>>k) & 1)){ 28 | b = b^arr[i]; 29 | } 30 | } 31 | a = e0 ^ b; 32 | int []res = new int[2]; 33 | res[0] = a; 34 | res[1] = b; 35 | return res; 36 | } 37 | 38 | 39 | public static void main(String[] args) { 40 | String s = "abbdeedgccaffs"; 41 | int a[] = getTwoOddNum(s); 42 | for(int i : a) { 43 | System.out.println((char)i); 44 | } 45 | 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/BAT/Bit/cipher.java: -------------------------------------------------------------------------------- 1 | package BAT.Bit; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/19. 5 | */ 6 | public class cipher { 7 | int cipher(int text, int passwd) { 8 | return text^passwd; 9 | } 10 | int getText(int cipher, int passwd) { 11 | return cipher ^ passwd; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/BAT/DP/把字符串1修改成字符串2的最大开销.java: -------------------------------------------------------------------------------- 1 | package BAT.DP; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/18. 5 | */ 6 | public class 把字符串1修改成字符串2的最大开销 { 7 | public static int minCost(String s1, String s2, int ic, int rc, int dc) { 8 | int n = s1.length(); 9 | int m = s2.length(); 10 | int [][]dp = new int[n][m]; 11 | for(int i = 0;i= coins.length) return 0; 26 | if(index == coins.length - 1){ 27 | res = (aim%coins[index] == 0) ? 1:0; 28 | return res; 29 | } 30 | for(int i = 0;coins[index]*i <= aim;i ++) { 31 | res += coinsLope(coins, index + 1, aim - coins[index] * i); 32 | } 33 | return res; 34 | } 35 | 36 | //记忆化搜索,防止重复计算 37 | public int coinsMap(int []coins, int index, int aim) { 38 | 39 | int [][]map = new int[index + 1][aim + 1]; 40 | Arrays.fill(map, -1); 41 | if(index >= coins.length) return 0; 42 | if(index == coins.length - 1){ 43 | res = (aim%coins[index] == 0) ? 1:0; 44 | return res; 45 | } 46 | for(int i = 0;coins[index]*i <= aim;i ++) { 47 | int temp = 0; 48 | if (map[index][aim] == -1) { 49 | temp = coinsLope(coins, index + 1, aim - coins[index] * i); 50 | map[index][aim] = temp; 51 | }else temp = map[index][aim]; 52 | res += temp; 53 | } 54 | return res; 55 | } 56 | 57 | public int coinsDP(int []coins,int n, int aim) { 58 | int [][]map = new int[n][aim + 1]; 59 | Arrays.fill(map, -1); 60 | //填充第一列 61 | for(int i = 0; i < n; i ++) { 62 | map[i][0] = 1; 63 | } 64 | //填充第一行 65 | for(int i=0;i= 0) { 77 | map[i][j] = map[i][j - coins[i]] + map[i-1][j]; 78 | } 79 | else map[i][j] = map[i-1][j]; 80 | } 81 | } 82 | return map[n-1][aim]; 83 | 84 | } 85 | 86 | public static void main(String[] args) { 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/BAT/DP/最长公共子序列.java: -------------------------------------------------------------------------------- 1 | package BAT.DP; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/18. 7 | */ 8 | public class 最长公共子序列 { 9 | public static int findLCS(String s1, String s2) { 10 | int n = s1.length(); 11 | int m = s2.length(); 12 | int [][]dp = new int[n][m]; 13 | for(int i = 0;i=max){ 25 | max=dp[j]; 26 | } 27 | } 28 | dp[i] = max + 1; 29 | 30 | } 31 | return dp[str.length() - 1]; 32 | 33 | } 34 | 35 | public static void main(String[] args) { 36 | String s = "215364897"; 37 | System.out.println(longestIncreasingSubsequence(s)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/BAT/DP/矩阵中走的最短路径.java: -------------------------------------------------------------------------------- 1 | package BAT.DP; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/18. 7 | */ 8 | public class 矩阵中走的最短路径 { 9 | public int DP(int [][]map ) { 10 | int n = map.length -1; 11 | int m = map[0].length -1; 12 | int [][]dp = new int[n + 1][m + 1]; 13 | Arrays.fill(dp, 0); 14 | dp[0][0] = map[0][0]; 15 | 16 | for(int i = 1;i <=n;i++) { 17 | dp[0][i] = map[0][i] + dp[0][i-1]; 18 | } 19 | for(int i = 1;i <= m;i++) { 20 | dp[i][0] = map[i][0] + dp[i-1][0]; 21 | } 22 | for(int i = 1,j=1;i<=n && j<=m;){ 23 | dp[i][j] = map[i][j] + Math.min(dp[i - 1][j],dp[i][j-1]); 24 | } 25 | return dp[n][m]; 26 | } 27 | 28 | public static void main(String[] args) { 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/BAT/DP/背包01.java: -------------------------------------------------------------------------------- 1 | package BAT.DP; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/18. 7 | */ 8 | public class 背包01 { 9 | public static int maxValue(int v[], int w[], int n,int W) { 10 | int [][]dp = new int[n][W+1]; 11 | for(int i=0;iw[0]) { 19 | dp[0][i] = v[0]; 20 | } 21 | else dp[0][i] = 0; 22 | } 23 | for(int i = 1;i linkedList = new LinkedList<>(); 13 | linkedList.add(1); 14 | linkedList.add(2); 15 | linkedList.add(3); 16 | linkedList.add(2); 17 | linkedList.add(1); 18 | linkedList.add(1); 19 | System.out.println(isHuiwen(linkedList)); 20 | 21 | 22 | } 23 | public static boolean isHuiwen(LinkedList list) { 24 | if (list.size() <= 1)return true; 25 | Stack stack = new Stack<>(); 26 | for (int i = 0;i < list.size();i ++) { 27 | stack.push(list.get(i)); 28 | } 29 | for (int i = 0;i < list.size();i ++) { 30 | int top = stack.pop(); 31 | if (top != list.get(i)) { 32 | return false; 33 | } 34 | } 35 | return true; 36 | 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/BAT/LinkedList/单链表每k个元素逆序.java: -------------------------------------------------------------------------------- 1 | package BAT.LinkedList; 2 | 3 | import java.util.Arrays; 4 | import java.util.LinkedList; 5 | import java.util.Stack; 6 | 7 | /** 8 | * Created by 周杰伦 on 2018/3/26. 9 | */ 10 | public class 单链表每k个元素逆序 { 11 | public static void main(String[] args) { 12 | LinkedList list = new LinkedList<>(); 13 | int i = 0; 14 | while (i < 10) { 15 | list.add(i); 16 | i ++; 17 | } 18 | reverseK(list, 3); 19 | System.out.println(Arrays.toString(list.toArray())); 20 | } 21 | static void reverseK(LinkedList list, int k) { 22 | if (k < 2)return; 23 | if (list.size() < k) { 24 | return; 25 | } 26 | Stack stack = new Stack<>(); 27 | int count = 0; 28 | LinkedList res = new LinkedList<>(); 29 | while (!list.isEmpty() && list.size() >= k) { 30 | while (count < k) { 31 | int i = list.remove(); 32 | stack.push(i); 33 | count++; 34 | } 35 | while (!stack.isEmpty()) { 36 | int i = stack.pop(); 37 | res.addLast(i); 38 | } 39 | count = 0; 40 | } 41 | while (!list.isEmpty()) { 42 | res.addLast(list.remove()); 43 | } 44 | list.addAll(res); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/BAT/LinkedList/复杂链表的复制.java: -------------------------------------------------------------------------------- 1 | package BAT.LinkedList; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/26. 5 | * 6 | */ 7 | public class 复杂链表的复制 { 8 | 9 | class RandomListNode { 10 | int label; 11 | RandomListNode next = null; 12 | RandomListNode random = null; 13 | 14 | RandomListNode(int label) { 15 | this.label = label; 16 | } 17 | } 18 | 19 | public RandomListNode Clone(RandomListNode pHead) 20 | { 21 | RandomListNode w = new RandomListNode(0); 22 | RandomListNode p = pHead; 23 | while (p!= null) { 24 | RandomListNode copy = new RandomListNode(p.label); 25 | copy.next = p.next; 26 | p.next = copy; 27 | w.next = p; 28 | p = copy.next; 29 | } 30 | RandomListNode pre = pHead; 31 | RandomListNode q = pHead.next; 32 | RandomListNode res = q; 33 | 34 | //串接节点 35 | RandomListNode temp = new RandomListNode(0); 36 | RandomListNode head = temp; 37 | 38 | while (q.next != null) { 39 | if (pre.random != null) { 40 | q.random = pre.random.next; 41 | }else q.random = null; 42 | pre = q.next; 43 | q.next = q.next.next; 44 | 45 | temp.next = q; 46 | temp = temp.next; 47 | 48 | q = q.next; 49 | if (q.next == null) { 50 | if (pre.random != null) { 51 | q.random = pre.random.next; 52 | }else q.random = null; 53 | q.next = null; 54 | break; 55 | } 56 | } 57 | return head; 58 | } 59 | public static void main(String[] args) { 60 | Node node = new Node(); 61 | Node node2 = new Node(2); 62 | Node node3 = new Node(3); 63 | node3.rand = node2; 64 | node2.next = node3; 65 | node2.rand = node; 66 | node.next = node2; 67 | node.rand = node3; 68 | 69 | 70 | Node res = copy(node); 71 | while (res!=null) { 72 | System.out.println(res.val); 73 | res = res.next; 74 | } 75 | } 76 | public static Node copy(Node n) { 77 | Node p = n; 78 | while (p!= null) { 79 | Node copy = new Node(p.val,null,null); 80 | copy.next = p.next; 81 | p.next = copy; 82 | p = copy.next; 83 | } 84 | Node pre =n; 85 | Node q = n.next; 86 | Node res = q; 87 | 88 | while (q.next != null) { 89 | q.rand = pre.rand.next; 90 | pre = q.next; 91 | q.next = q.next.next; 92 | q = q.next; 93 | if (q.next == null) { 94 | q.rand = pre.rand.next; 95 | q.next = null; 96 | break; 97 | } 98 | } 99 | return res; 100 | } 101 | 102 | static class Node{ 103 | int val; 104 | Node next; 105 | Node rand; 106 | Node(int val, Node next, Node rand) { 107 | this.val = val; 108 | this.next = next; 109 | this.rand = rand; 110 | } 111 | Node() { 112 | val = 1; 113 | next = null; 114 | rand = null; 115 | } 116 | Node(int val) { 117 | this.val = val; 118 | next = null; 119 | rand = null; 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/BAT/LinkedList/打印两个链表的公共值练题.java: -------------------------------------------------------------------------------- 1 | package BAT.LinkedList; 2 | 3 | import java.util.LinkedList; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/26. 7 | */ 8 | public class 打印两个链表的公共值练题 { 9 | public static void main(String[] args) { 10 | 11 | } 12 | public void printCommon (LinkedList l1, LinkedList l2) { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/BAT/LinkedList/有序链表的公共部分.java: -------------------------------------------------------------------------------- 1 | package BAT.LinkedList; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/26.略 5 | * 太简单 6 | */ 7 | public class 有序链表的公共部分 { 8 | } 9 | -------------------------------------------------------------------------------- /src/BAT/LinkedList/环形链表插值.java: -------------------------------------------------------------------------------- 1 | package BAT.LinkedList; 2 | 3 | import BAT.LinkedList.basic.Node; 4 | import BAT.LinkedList.basic.forList; 5 | 6 | import java.util.LinkedList; 7 | 8 | /** 9 | * Created by 周杰伦 on 2018/3/26. 10 | * 错误示范 11 | * 12 | */ 13 | public class 环形链表插值 { 14 | public static void main(String[] args) { 15 | Node node = new Node(1); 16 | forList list = new forList(node); 17 | list.add(new Node(2)); 18 | list.add(new Node(4)); 19 | list.add(new Node(3)); 20 | list.add(new Node(0)); 21 | list.print(); 22 | } 23 | public void insert(forList list, int val) { 24 | Node head = list.p; 25 | Node pre = head; 26 | head = head.next; 27 | 28 | if (val < pre.val) { 29 | Node n = list.p; 30 | while (n.next != head) { 31 | n = n.next; 32 | } 33 | n.next = new Node(val, head); 34 | list.p = n.next; 35 | } 36 | while (val > head.val) { 37 | head = head.next; 38 | } 39 | if (head.next == list.p) { 40 | head.next = new Node(val, list.p); 41 | } 42 | else { 43 | Node temp = head.next; 44 | head.next = new Node(val, temp); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/BAT/LinkedList/相交及有环问题/任意单链表的相交.java: -------------------------------------------------------------------------------- 1 | package BAT.LinkedList.相交及有环问题; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/26. 5 | */ 6 | public class 任意单链表的相交 { 7 | public static void main(String[] args) { 8 | //1先找到入环节点 9 | //2如果有一个链表有环另一个却没有,则不能相交。 10 | //若都无环,则算出长度让锻炼表先走长度差。 11 | //若都有环,先看入环节点是否相同,再分别处理 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/BAT/LinkedList/相交及有环问题/无环链表相交.java: -------------------------------------------------------------------------------- 1 | package BAT.LinkedList.相交及有环问题; 2 | 3 | import BAT.LinkedList.basic.Node; 4 | import BAT.LinkedList.basic.逆序; 5 | 6 | import static BAT.LinkedList.basic.逆序.reverse; 7 | 8 | /** 9 | * Created by 周杰伦 on 2018/3/26. 10 | */ 11 | public class 无环链表相交 { 12 | public static void main(String[] args) { 13 | 14 | } 15 | public Node wuhuanxiangjiao(Node a, Node b) { 16 | a = reverse(a); 17 | b= reverse(b); 18 | while (a != null && b != null) { 19 | if (a == b)return a; 20 | a = a.next; 21 | b = b.next; 22 | } 23 | return null; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/BAT/LinkedList/相交及有环问题/有环链表相交.java: -------------------------------------------------------------------------------- 1 | package BAT.LinkedList.相交及有环问题; 2 | 3 | import BAT.LinkedList.basic.Node; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/26. 7 | * 环节点相同时 8 | * 分成两种情况。一种是在入环前相交,一种是在入环处相交 9 | * 环节点不相同时,又分为两种情况 10 | */ 11 | public class 有环链表相交 { 12 | public static void main(String[] args) { 13 | 14 | } 15 | public int length (Node a, Node meet) { 16 | int len = 0; 17 | while (a != meet) { 18 | len ++; 19 | a = a.next; 20 | } 21 | return len; 22 | } 23 | public Node xiangjiao(Node a,Node b) { 24 | Node h1 = 链表有环.youhuan(a); 25 | Node h2 = 链表有环.youhuan(b); 26 | //人环节点不同 27 | if(h1 != h2){ 28 | Node p1 = h1; 29 | Node p2 = h2; 30 | while (p1.next != p1) { 31 | p1 = p1.next; 32 | if (p1 == p2) return p1; 33 | } 34 | return null; 35 | } 36 | // 入环节点相同 37 | else { 38 | while (a != h1 && b != h2) { 39 | if (a == b) return a; 40 | else { 41 | int l1 = length(a, h1); 42 | int l2 = length(b, h2); 43 | int len = l1 > l2 ?(l1 - l2):(l2 - l1); 44 | if (l1 < l2) { 45 | while (len > 0) { 46 | a = a.next; 47 | len --; 48 | } 49 | } 50 | if (l1 > l2) { 51 | while (len > 0) { 52 | b = b.next; 53 | len --; 54 | } 55 | } 56 | break; 57 | 58 | } 59 | } 60 | while (a != h1 && b != h2) { 61 | if (a == b) { 62 | return a; 63 | }else { 64 | a = a.next; 65 | b = b.next; 66 | } 67 | } 68 | return h1; 69 | } 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/BAT/LinkedList/相交及有环问题/链表有环.java: -------------------------------------------------------------------------------- 1 | package BAT.LinkedList.相交及有环问题; 2 | 3 | import BAT.LinkedList.basic.Node; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/26. 7 | */ 8 | public class 链表有环 { 9 | public static void main(String[] args) { 10 | 11 | } 12 | public static Node youhuan(Node node) { 13 | Node slow = node; 14 | Node fast = node; 15 | while(fast != null && fast.next != null) { 16 | slow = slow.next; 17 | fast = fast.next.next; 18 | if (slow == fast) { 19 | fast = node; 20 | while (slow != fast) { 21 | slow = slow.next; 22 | fast = fast.next; 23 | } 24 | return fast; 25 | } 26 | } 27 | return null; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/BAT/LinkedList/链表的荷兰旗问题.java: -------------------------------------------------------------------------------- 1 | package BAT.LinkedList; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | import java.util.LinkedList; 6 | 7 | /** 8 | * Created by 周杰伦 on 2018/3/26. 9 | */ 10 | public class 链表的荷兰旗问题 { 11 | public static void main(String[] args) { 12 | LinkedList linkedList = new LinkedList<>(); 13 | linkedList.offer(1); 14 | linkedList.offer(3); 15 | linkedList.offer(7); 16 | linkedList.offer(4); 17 | linkedList.offer(8); 18 | linkedList.offer(6); 19 | linkedList.offer(10); 20 | sort(linkedList, 6); 21 | 22 | 23 | } 24 | public static void sort(LinkedList l, int val) { 25 | LinkedList l1 = new LinkedList<>(); 26 | LinkedList l2 = new LinkedList<>(); 27 | LinkedList l3 = new LinkedList<>(); 28 | while (!l.isEmpty()) { 29 | int num = l.remove(); 30 | if (num < val) l1.offer(num); 31 | else if (num > val)l3.offer(num); 32 | else l2.offer(num); 33 | } 34 | System.out.println(Arrays.toString(l1.toArray())); 35 | System.out.println(Arrays.toString(l2.toArray())); 36 | System.out.println(Arrays.toString(l3.toArray())); 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/BAT/QueueAndStack/BFS.java: -------------------------------------------------------------------------------- 1 | package BAT.QueueAndStack; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | import java.util.LinkedList; 6 | import java.util.Queue; 7 | 8 | /** 9 | * Created by 周杰伦 on 2018/3/25. 10 | */ 11 | public class BFS { 12 | public static void main(String[] args) { 13 | TreeNode t = TreeNode.getNode(); 14 | bfs(t); 15 | } 16 | public static void bfs(TreeNode t) { 17 | if (t == null) { 18 | return; 19 | } 20 | Queue queue = new LinkedList<>(); 21 | queue.offer(t); 22 | while (!queue.isEmpty()) { 23 | TreeNode p = queue.poll(); 24 | System.out.println(p.val); 25 | if (p.left != null) { 26 | queue.offer(p.left); 27 | } 28 | if (p.right != null) { 29 | queue.offer(p.right); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/BAT/QueueAndStack/DFS.java: -------------------------------------------------------------------------------- 1 | package BAT.QueueAndStack; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | import java.util.HashMap; 6 | import java.util.Stack; 7 | 8 | /** 9 | * Created by 周杰伦 on 2018/3/25. 10 | */ 11 | public class DFS { 12 | public static void main(String[] args) { 13 | TreeNode root = new TreeNode(10); 14 | TreeNode treeNode1=new TreeNode(5); 15 | TreeNode treeNode2=new TreeNode(12); 16 | TreeNode treeNode3=new TreeNode(4); 17 | TreeNode treeNode4=new TreeNode(7); 18 | treeNode1.left=treeNode3; 19 | treeNode1.right=treeNode4; 20 | root.left=treeNode1; 21 | root.right=treeNode2; 22 | TreeNode treeNode = TreeNode.getNode(); 23 | dfs(treeNode); 24 | } 25 | static Stack stack = new Stack<>(); 26 | public static void dfs(TreeNode t) { 27 | if (t == null)return; 28 | HashMap map = new HashMap<>(); 29 | stack.push(t.val); 30 | if (!stack.isEmpty()) { 31 | int top = stack.peek(); 32 | if (map.getOrDefault(top, 0) == 0) { 33 | System.out.println(top); 34 | map.put(top, 1); 35 | } 36 | } 37 | dfs(t.left); 38 | dfs(t.right); 39 | stack.pop(); 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/BAT/QueueAndStack/两个栈实现一个队列.java: -------------------------------------------------------------------------------- 1 | package BAT.QueueAndStack; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/25. 7 | */ 8 | public class 两个栈实现一个队列 { 9 | 10 | public static void main(String[] args) { 11 | Queue.add(3); 12 | System.out.println(Queue.get());; 13 | Queue.add(2); 14 | System.out.println(Queue.get());; 15 | Queue.add(1); 16 | System.out.println(Queue.remove());; 17 | System.out.println(Queue.get());; 18 | } 19 | static class Queue { 20 | private static Stack stackPop = new Stack<>(); 21 | private static Stack stackPush = new Stack<>(); 22 | static void add(int val) { 23 | stackPush.push(val); 24 | } 25 | static int remove() { 26 | if (!stackPop.isEmpty()) { 27 | return stackPop.pop(); 28 | } 29 | else { 30 | while (!stackPush.isEmpty()) { 31 | int i = stackPush.pop(); 32 | stackPop.push(i); 33 | } 34 | } 35 | return stackPop.pop(); 36 | } 37 | static int get() { 38 | if (!stackPop.isEmpty()) { 39 | return stackPop.peek(); 40 | } 41 | else { 42 | while (!stackPush.isEmpty()) { 43 | int i = stackPush.pop(); 44 | stackPop.push(i); 45 | } 46 | } 47 | return stackPop.peek(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/BAT/QueueAndStack/单变量排序栈元素.java: -------------------------------------------------------------------------------- 1 | package BAT.QueueAndStack; 2 | 3 | import java.util.Collections; 4 | import java.util.Stack; 5 | 6 | /** 7 | * Created by 周杰伦 on 2018/3/25. 8 | */ 9 | public class 单变量排序栈元素 { 10 | public static void main(String[] args) { 11 | Stack stack = new Stack<>(); 12 | for (int i = 10; i > 0; i --) { 13 | stack.push(i); 14 | } 15 | 16 | sortStack(stack); 17 | while (!stack.isEmpty()) { 18 | System.out.print(stack.pop()); 19 | } 20 | } 21 | public static void sortStack(Stack stack) { 22 | if(stack.isEmpty() || stack.size() == 1)return; 23 | Stack stackHelp = new Stack<>(); 24 | int i = stack.pop(); 25 | stackHelp.push(i); 26 | while (!stack.isEmpty()) { 27 | int temp = stack.pop(); 28 | if (temp > stackHelp.firstElement()) { 29 | int count = 0; 30 | while (!stackHelp.isEmpty()) { 31 | stack.push(stackHelp.pop()); 32 | count ++; 33 | } 34 | stackHelp.push(temp); 35 | while (count > 0) { 36 | stackHelp.push(stack.pop()); 37 | count --; 38 | } 39 | } 40 | else { 41 | stackHelp.push(temp); 42 | } 43 | } 44 | while (!stackHelp.isEmpty()) { 45 | stack.push(stackHelp.pop()); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/BAT/QueueAndStack/栈的原地逆序.java: -------------------------------------------------------------------------------- 1 | package BAT.QueueAndStack; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/25. 7 | */ 8 | public class 栈的原地逆序 { 9 | public static void main(String[] args) { 10 | Stack stack = new Stack<>(); 11 | stack.push(1); 12 | stack.push(2); 13 | 14 | System.out.println(get(stack)); 15 | System.out.println(get(stack)); 16 | 17 | Stack stack2 = new Stack<>(); 18 | for (int i = 1;i < 10;i ++) { 19 | stack2.push(i); 20 | } 21 | reverse(stack2); 22 | while (!stack2.isEmpty()) 23 | System.out.print(stack2.pop()); 24 | } 25 | static void reverse(Stack stack) { 26 | if (stack.isEmpty())return; 27 | else { 28 | int i = get(stack); 29 | reverse(stack); 30 | stack.push(i); 31 | } 32 | } 33 | 34 | static int get(Stack stack) { 35 | if (stack.isEmpty()){ 36 | System.exit(-1); 37 | } 38 | int result = stack.pop(); 39 | if (stack.isEmpty()) { 40 | return result; 41 | } 42 | else { 43 | int res2 = get(stack); 44 | stack.push(result); 45 | return res2; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/BAT/QueueAndStack/滑动窗口的最大值.java: -------------------------------------------------------------------------------- 1 | package BAT.QueueAndStack; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/25. 7 | */ 8 | public class 滑动窗口的最大值 { 9 | public static void main(String[] args) { 10 | int []a = {4,3,5,4,3,3,6,7}; 11 | System.out.println(Arrays.toString(maxValueOfSlideWindow(a, 3))); 12 | } 13 | static int[] maxValueOfSlideWindow (int []a, int w) { 14 | int []res = new int[a.length - w + 1]; 15 | int end = w; 16 | int start = 0; 17 | while (end <= a.length) { 18 | int max = a[start]; 19 | for (int i = start; i < end; i++) { 20 | if (a[i] > max) { 21 | max = a[i]; 22 | } 23 | } 24 | res[start] = max; 25 | start ++; 26 | end ++; 27 | } 28 | return res; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/BAT/QueueAndStack/获取栈中的最小值.java: -------------------------------------------------------------------------------- 1 | package BAT.QueueAndStack; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/25. 7 | */ 8 | public class 获取栈中的最小值 { 9 | public static void main(String[] args) { 10 | MinStack minStack = new MinStack(); 11 | minStack.push(1); 12 | minStack.push(2); 13 | minStack.push(4); 14 | minStack.push(3); 15 | System.out.println(minStack.getmin()); 16 | } 17 | 18 | static class MinStack extends Stack{ 19 | int min = Integer.MAX_VALUE; 20 | Stack minStack = new Stack<>(); 21 | public MinStack() { 22 | minStack.push(min); 23 | } 24 | 25 | @Override 26 | public Integer push(Integer item) { 27 | if (item < minStack.peek()) { 28 | minStack.push(item); 29 | } 30 | super.push(item); 31 | return item; 32 | } 33 | 34 | @Override 35 | public synchronized Integer pop() { 36 | if (minStack.peek() == min) { 37 | minStack.pop(); 38 | } 39 | return super.pop(); 40 | } 41 | 42 | public Integer getmin() { 43 | if (minStack.isEmpty()) { 44 | return null; 45 | } 46 | return minStack.peek(); 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/BAT/Sort/basic/HeapSort.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort.basic; 2 | 3 | import java.util.Arrays; 4 | 5 | public class HeapSort { 6 | /** 7 | * 构建大顶堆 8 | */ 9 | public static void adjustHeap(int[] a, int i, int len) { 10 | int temp, j; 11 | temp = a[i]; 12 | for (j = 2 * i; j < len; j *= 2) {// 沿关键字较大的孩子结点向下筛选 13 | if (j < len && a[j] < a[j + 1]) 14 | ++j; // j为关键字中较大记录的下标 15 | if (temp >= a[j]) 16 | break; 17 | a[i] = a[j]; 18 | i = j; 19 | } 20 | a[i] = temp; 21 | } 22 | 23 | public static void heapSort(int[] a) { 24 | int i; 25 | for (i = a.length / 2 - 1; i >= 0; i--) {// 构建一个大顶堆 26 | adjustHeap(a, i, a.length - 1); 27 | } 28 | for (i = a.length - 1; i >= 0; i--) {// 将堆顶记录和当前未经排序子序列的最后一个记录交换 29 | int temp = a[0]; 30 | a[0] = a[i]; 31 | a[i] = temp; 32 | adjustHeap(a, 0, i - 1);// 将a中前i-1个记录重新调整为大顶堆 33 | } 34 | } 35 | 36 | public static void main(String[] args) { 37 | int a[] = { 51, 46, 20, 18, 65, 97, 82, 30, 77, 50 }; 38 | heapSort(a); 39 | System.out.println(Arrays.toString(a)); 40 | } 41 | } -------------------------------------------------------------------------------- /src/BAT/Sort/basic/Mergesort.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort.basic; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Mergesort { 6 | /** 7 | * 归并排序 8 | * 简介:将两个(或两个以上)有序表合并成一个新的有序表 即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列 9 | * 时间复杂度为O(nlogn) 10 | * 稳定排序方式 11 | * @param nums 待排序数组 12 | * @return 输出有序数组 13 | */ 14 | public static int[] sort(int[] nums, int low, int high) { 15 | int mid = (low + high) / 2; 16 | if (low < high) { 17 | // 左边 18 | sort(nums, low, mid); 19 | // 右边 20 | sort(nums, mid + 1, high); 21 | // 左右归并 22 | merge(nums, low, mid, high); 23 | } 24 | return nums; 25 | } 26 | 27 | public static void merge(int[] nums, int low, int mid, int high) { 28 | int[] temp = new int[high - low + 1]; 29 | int i = low;// 左指针 30 | int j = mid + 1;// 右指针 31 | int k = 0; 32 | 33 | // 把较小的数先移到新数组中 34 | while (i <= mid && j <= high) { 35 | if (nums[i] < nums[j]) { 36 | temp[k++] = nums[i++]; 37 | } else { 38 | temp[k++] = nums[j++]; 39 | } 40 | } 41 | 42 | // 把左边剩余的数移入数组 43 | while (i <= mid) { 44 | temp[k++] = nums[i++]; 45 | } 46 | 47 | // 把右边边剩余的数移入数组 48 | while (j <= high) { 49 | temp[k++] = nums[j++]; 50 | } 51 | 52 | // 把新数组中的数覆盖nums数组 53 | for (int k2 = 0; k2 < temp.length; k2++) { 54 | nums[k2 + low] = temp[k2]; 55 | } 56 | } 57 | 58 | 59 | // 归并排序的实现 60 | public static void main(String[] args) { 61 | 62 | int[] nums = { 2, 7, 8, 3, 1, 6, 9, 0, 5, 4 }; 63 | 64 | sort(nums, 0, nums.length-1); 65 | System.out.println(Arrays.toString(nums)); 66 | } 67 | } -------------------------------------------------------------------------------- /src/BAT/Sort/basic/bubblesort.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort.basic; 2 | 3 | /** 4 | * Created by 周杰伦 on 2017/2/28. 5 | */ 6 | public class bubblesort { 7 | public static void bubblesort(int num[]){ 8 | int i; 9 | boolean flag=false; 10 | int temp; 11 | for(int j=num.length;j>1;j--) 12 | { 13 | for(i=1;ix){ 13 | right=mid-1; 14 | } 15 | else if(num[mid]0 && temp temp) --j; 17 | if (i < j) { 18 | num[i] = num[j]; 19 | ++i; 20 | } 21 | 22 | while (i < j && num[i] < temp) ++i; 23 | if (i < j) { 24 | num[j] = num[i]; 25 | --j; 26 | } 27 | 28 | } 29 | num[i] = temp; 30 | quicksort1(num, left, i - 1); 31 | quicksort1(num, i + 1, right); 32 | 33 | } 34 | } 35 | 36 | public static void main(String args[]) { 37 | int []num={13,33,22,1,23,14,12}; 38 | quicksort1(num,0,6); 39 | for (int x = 0; x < num.length; x++) 40 | System.out.println(num[x]); 41 | 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /src/BAT/Sort/basic/quicksort2.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort.basic; 2 | 3 | /** 4 | * Created by 周杰伦 on 2017/2/28. 5 | */ 6 | public class quicksort2 { 7 | 8 | public static int partition(int []array,int lo,int hi){ 9 | //固定的切分方式 10 | int key=array[lo]; 11 | while(lo=key && hi>lo){//从后半部分向前扫描 13 | hi--; 14 | } 15 | array[lo]=array[hi]; 16 | while(array[lo]<=key && hi>lo){ 17 | lo++; 18 | } 19 | array[hi]=array[lo]; 20 | } 21 | array[hi]=key; 22 | return hi; 23 | } 24 | 25 | public static void sort(int[] array,int lo ,int hi){ 26 | if(lo>=hi){ 27 | return ; 28 | } 29 | int index=partition(array,lo,hi); 30 | sort(array,lo,index-1); 31 | sort(array,index+1,hi); 32 | } 33 | 34 | public static void main(String args[]){ 35 | int []num={1,3,22,121,123,21}; 36 | sort(num,0,5); 37 | for(int x=0;x<5;x++) 38 | System.out.println(num[x]); 39 | 40 | } 41 | } -------------------------------------------------------------------------------- /src/BAT/Sort/basic/selectsort.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort.basic; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 周杰伦 on 2017/3/9. 7 | */ 8 | public class selectsort { 9 | public static void selectSort(int []num) { 10 | 11 | int i,j=0,k=0,temp; 12 | for (i = 0; i < num.length; i++) { 13 | int min = num[i]; 14 | k=i; 15 | while (k< num.length) { 16 | if (min > num[k]) { 17 | min = num[k]; 18 | j = k; 19 | k++; 20 | }else k++; 21 | } 22 | if(i list = new ArrayList<>(); 28 | int i = start; 29 | int len = k; 30 | for (i = k / 2 - 1; i >= 0; i--) {// 构建一个小顶堆 31 | adjustSmallHeap(a, i, k - 1); 32 | } 33 | 34 | list.add(a[a.length - k]); 35 | for (int j = a.length-k;j >0;j --) { 36 | a[a.length - k] = a[j]; 37 | heapSort(a); 38 | list.add(a[a.length - k]); 39 | } 40 | int s = 0; 41 | for (int w : list) { 42 | a[s++] = w; 43 | } 44 | } 45 | public static void adjustSmallHeap(int[] a, int i, int len) { 46 | int temp, j; 47 | temp = a[i]; 48 | for (j = 2 * i; j < len; j *= 2) {// 沿关键字较大的孩子结点向下筛选 49 | if (j < len && a[j] > a[j + 1]) 50 | ++j; // j为关键字中较大记录的下标 51 | if (temp <= a[j]) 52 | break; 53 | a[i] = a[j]; 54 | i = j; 55 | } 56 | a[i] = temp; 57 | } 58 | 59 | public static void heapSort(int[] a) { 60 | int i; 61 | for (i = a.length / 2 - 1; i >= 0; i--) {// 构建一个小顶堆 62 | adjustSmallHeap(a, i, a.length - 1); 63 | } 64 | for (i = a.length - 1; i >= 0; i--) {// 将堆顶记录和当前未经排序子序列的最后一个记录交换 65 | int temp = a[0]; 66 | a[0] = a[i]; 67 | a[i] = temp; 68 | adjustSmallHeap(a, 0, i - 1);// 将a中前i-1个记录重新调整为大顶堆 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/BAT/Sort/是否有数重复出现.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/23. 7 | */ 8 | public class 是否有数重复出现 { 9 | public static void main(String[] args) { 10 | int []a = {2,4,1,7,45,86,3,1,6,17,1}; 11 | System.out.println(duplicate(a)); 12 | } 13 | public static boolean duplicate(int []a) { 14 | Arrays.sort(a); 15 | for (int i = 1;i < a.length;i ++) { 16 | if (a[i] == a[i - 1]){ 17 | return true; 18 | } 19 | } 20 | return false; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/BAT/Sort/有序数组合并.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/23. 7 | */ 8 | public class 有序数组合并 { 9 | //1 3 5 10 | //2 4 6 11 | //从后往前,两两比较,大的放最后,小的放次后,以此类推。 12 | public static void combineSortArrays (int []a, int []b,int n,int m) { 13 | int len = a.length - 1; 14 | int i = n - 1; 15 | int j = m - 1; 16 | while (i >= 0 && j >= 0 && len >= 0) { 17 | if (a[i] > b[j]) { 18 | a[len --] = a[i --]; 19 | } 20 | else { 21 | a[len --] = b[j --]; 22 | } 23 | } 24 | while (j >= 0) { 25 | a[j] = b[j]; 26 | j --; 27 | } 28 | } 29 | public static void main(String[] args) { 30 | int []a = new int[6]; 31 | int []b = new int[4]; 32 | b[0] = 2; 33 | b[1] = 4; 34 | b[2] = 6; 35 | b[3] = 8; 36 | a[0] = 1; 37 | a[1] = 3; 38 | 39 | combineSortArrays(a, b, 2, 4); 40 | System.out.println(Arrays.toString(a)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/BAT/Sort/有序矩阵查找.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/23. 5 | */ 6 | public class 有序矩阵查找 { 7 | public boolean Find(int target, int[][] array) { 8 | 9 | 10 | int row = 0; 11 | int column = array[0].length - 1; 12 | 13 | 14 | //二维矩阵,可化成正方形,如果这个数比左右边的数小,则列数减少。 15 | //如果这个数比右边的数大,则行数增加。 16 | while (column >=0 && row < array.length) { 17 | 18 | 19 | 20 | if (target == array[row][column]) 21 | return true; 22 | 23 | 24 | else if (target < array[row][column]) 25 | column--; 26 | 27 | else row++; 28 | 29 | 30 | } 31 | 32 | return false; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/BAT/Sort/相邻两数最大差值.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/23. 7 | */ 8 | public class 相邻两数最大差值 { 9 | public static void main(String[] args) { 10 | int []a = {1,4,2,3,6,8,7}; 11 | System.out.println(maxGap(a)); 12 | } 13 | public static int maxGap (int []a){ 14 | if (a.length <=1) return 0; 15 | if (a.length == 2) return Math.abs(a[1] - a[0]); 16 | Arrays.sort(a); 17 | int max = 0; 18 | for (int i = 1;i < a.length;i ++) { 19 | if (a[i] - a[i-1] > max) max = a[i] - a[i-1]; 20 | } 21 | return max; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/BAT/Sort/荷兰国旗.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/23. 7 | */ 8 | public class 荷兰国旗 { 9 | public static void hollan (int []arr) { 10 | int l,r,f; 11 | l = 0; 12 | r = arr.length - 1; 13 | f = 0; 14 | int temp = 0; 15 | 16 | for (;f <= r;f ++) { 17 | if (arr[f] == 0) { 18 | temp = arr[f]; 19 | arr[f] = arr[l]; 20 | arr[l] = temp; 21 | l ++; 22 | } 23 | else if (arr[f] == 2) { 24 | temp = arr[f]; 25 | arr[f] = arr[r]; 26 | arr[r] = temp; 27 | r --; 28 | //因为是从左到右遍历。所以从右边换过来的值还没判断,需要判断 29 | f --; 30 | } 31 | 32 | } 33 | } 34 | 35 | public static void main(String[] args) { 36 | int []a = {0,2,1,2,1,1,1,1,0,0,0,0,1,2,1}; 37 | hollan(a); 38 | System.out.println(Arrays.toString(a)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/BAT/Sort/需要排序的最短子序列.java: -------------------------------------------------------------------------------- 1 | package BAT.Sort; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/23. 5 | * 从左边起,不断更新最大值,找到A【i】值比最大值大时替换最大值,比最大值小时更新下标记录为r。 6 | * 从右边起,不断更新最小值,找到A【i】值比最小值小时替换最小值,比最小值大时更新下标记录为r。 7 | */ 8 | public class 需要排序的最短子序列 { 9 | public static void main(String[] args) { 10 | int []a = {1,5,3}; 11 | System.out.println(shortestLength(a)); 12 | } 13 | public static int shortestLength (int []a) { 14 | if (a.length <= 1)return 0; 15 | if (a.length == 2)return a[0] > a[1] ? 2:0; 16 | 17 | int min = a[a.length - 1]; 18 | int max = a[0]; 19 | int l = 0,r = 0; 20 | int i = 1; 21 | int j = a.length - 2; 22 | for(;i < a.length;i ++) { 23 | if (max < a[i]) { 24 | max = a[i]; 25 | } 26 | else r = i; 27 | } 28 | for (;j > 0;j --) { 29 | if (min > a[j]) { 30 | min = a[j]; 31 | } 32 | else l = j; 33 | } 34 | 35 | return r - l; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/BAT/String/Basic/KMP.java: -------------------------------------------------------------------------------- 1 | package BAT.String.Basic; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/24. 5 | */ 6 | public class KMP { 7 | public static int kmp(String str, String dest,int[] next){//str文本串 dest 模式串 8 | for(int i = 0, j = 0; i < str.length(); i++){ 9 | while(j > 0 && str.charAt(i) != dest.charAt(j)){ 10 | j = next[j - 1]; 11 | } 12 | if(str.charAt(i) == dest.charAt(j)){ 13 | j++; 14 | } 15 | if(j == dest.length()){ 16 | return i-j+1; 17 | } 18 | } 19 | return 0; 20 | } 21 | public static int[] kmpnext(String dest){ 22 | int[] next = new int[dest.length()]; 23 | next[0] = 0; 24 | for(int i = 1,j = 0; i < dest.length(); i++){ 25 | while(j > 0 && dest.charAt(j) != dest.charAt(i)){ 26 | j = next[j - 1]; 27 | } 28 | if(dest.charAt(i) == dest.charAt(j)){ 29 | j++; 30 | } 31 | next[i] = j; 32 | } 33 | return next; 34 | } 35 | public static void main(String[] args){ 36 | String a = "ababa"; 37 | String b = "ssdfgasdbababa"; 38 | int[] next = kmpnext(a); 39 | int res = kmp(b, a,next); 40 | System.out.println(res); 41 | for(int i = 0; i < next.length; i++){ 42 | System.out.println(next[i]); 43 | } 44 | System.out.println(next.length); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/BAT/String/Basic/字符串逆序.java: -------------------------------------------------------------------------------- 1 | package BAT.String.Basic; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/24. 5 | */ 6 | public class 字符串逆序 { 7 | public static void reverse(char[] str,int start, int end) { 8 | int l = start; 9 | int r = end; 10 | while (l < r) { 11 | char temp = str[l]; 12 | str[l] = str[r]; 13 | str[r] = temp; 14 | l ++; 15 | r --; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/BAT/String/判断二叉树的子结构.java: -------------------------------------------------------------------------------- 1 | package BAT.String; 2 | 3 | import BAT.BinTree.basic.TreeNode; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/24. 7 | */ 8 | public class 判断二叉树的子结构 { 9 | //这种方法是错的,因为前序遍历不能确定一个二叉树 10 | public static void main(String[] args) { 11 | System.out.println(("8!8#").contains("!8")); 12 | System.out.println(1 + "!#"); 13 | } 14 | public boolean HasSubtree(TreeNode root1,TreeNode root2) { 15 | if (root2 == null) return false; 16 | if (root1 == null) return false; 17 | StringBuffer sb = new StringBuffer(); 18 | StringBuffer sb1 = new StringBuffer(); 19 | StringBuffer sb2 = new StringBuffer(); 20 | StringBuffer sb3 = new StringBuffer(); 21 | Serialize(root1, sb); 22 | midSerialize(root1, sb1); 23 | String str1 = sb.toString(); 24 | String str11 = sb1.toString(); 25 | Serialize(root2 ,sb2); 26 | midSerialize(root1, sb3); 27 | String str2 = sb2.toString(); 28 | String str22 = sb3.toString(); 29 | return str1.contains(str2) && str11.contains(str22); 30 | } 31 | 32 | void Serialize(TreeNode root, StringBuffer sb) { 33 | if(root == null) { 34 | sb.append("#!") ; 35 | return; 36 | } 37 | 38 | sb.append(root.val + "!"); 39 | Serialize(root.left, sb); 40 | Serialize(root.right, sb); 41 | } 42 | 43 | void midSerialize(TreeNode root, StringBuffer sb) { 44 | 45 | Serialize(root.left, sb); 46 | if(root == null) { 47 | sb.append("#!") ; 48 | return; 49 | } 50 | sb.append(root.val + "!"); 51 | Serialize(root.right, sb); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/BAT/String/判断有效括号字符串.java: -------------------------------------------------------------------------------- 1 | package BAT.String; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/24. 7 | */ 8 | public class 判断有效括号字符串 { 9 | public static void main(String[] args) { 10 | String s = "((asdas)))"; 11 | System.out.println(legalStr(s)); 12 | } 13 | public static boolean legalStr(String s) { 14 | int count = 0; 15 | for (int i = 0;i < s.length(); i ++) { 16 | if (s.charAt(i) == '(') 17 | count ++; 18 | else if (s.charAt(i) == ')') { 19 | count --; 20 | } 21 | } 22 | return count == 0; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/BAT/String/变形词.java: -------------------------------------------------------------------------------- 1 | package BAT.String; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Set; 6 | 7 | /** 8 | * Created by 周杰伦 on 2018/3/24. 9 | */ 10 | public class 变形词 { 11 | public static void main(String[] args) { 12 | String s1 = "123"; 13 | String s2 = "2331"; 14 | System.out.println(StrToStr(s1, s2)); 15 | } 16 | public static boolean StrToStr(String s1, String s2) { 17 | HashMap map1 = new HashMap<>(); 18 | HashMap map2 = new HashMap<>(); 19 | for (int i = 0;i < s1.length(); i ++) { 20 | if (map1.containsKey(s1.charAt(i))) { 21 | map1.put(s1.charAt(i), map1.get(s1.charAt(i)) + 1); 22 | } 23 | else { 24 | map1.put(s1.charAt(i), 1); 25 | } 26 | } 27 | for (int i = 0;i < s2.length(); i ++) { 28 | if (map2.containsKey(s2.charAt(i))) { 29 | map2.put(s2.charAt(i), map2.get(s2.charAt(i)) + 1); 30 | } 31 | else { 32 | map2.put(s2.charAt(i), 1); 33 | } 34 | } 35 | 36 | if (map1.equals(map2)) return true; 37 | else return false; 38 | 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/BAT/String/字符串的最长无重复子序列.java: -------------------------------------------------------------------------------- 1 | package BAT.String; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/24. 5 | */ 6 | public class 字符串的最长无重复子序列 { 7 | public static void main(String[] args) { 8 | 9 | } 10 | 11 | public String longestsubstr (String s) { 12 | 13 | return ""; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/BAT/String/旋转字符串.java: -------------------------------------------------------------------------------- 1 | package BAT.String; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/24. 5 | */ 6 | public class 旋转字符串 { 7 | public static void main(String[] args) { 8 | String a = "abcde"; 9 | System.out.println(rotateString(a, 2)); 10 | } 11 | public static String rotateString(String str, int i) { 12 | char []a = str.toCharArray(); 13 | reverse(a, 0 , i); 14 | reverse(a, i + 1, a.length - 1); 15 | reverse(a, 0, a.length - 1); 16 | return String.valueOf(a); 17 | } 18 | public static void reverse(char[] str,int start, int end) { 19 | int l = start; 20 | int r = end; 21 | while (l < r) { 22 | char temp = str[l]; 23 | str[l] = str[r]; 24 | str[r] = temp; 25 | l ++; 26 | r --; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/BAT/String/旋转词.java: -------------------------------------------------------------------------------- 1 | package BAT.String; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/24. 5 | */ 6 | public class 旋转词 { 7 | public static void main(String[] args) { 8 | 9 | } 10 | public boolean rotateWord(String s1, String s2) { 11 | if (s1.length() != s2.length()) { 12 | return false; 13 | } 14 | return (s1 + s1).contains(s2); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/BAT/String/替换字符串中的空格.java: -------------------------------------------------------------------------------- 1 | package BAT.String; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/24. 5 | */ 6 | public class 替换字符串中的空格 { 7 | public static String replaceSpace(StringBuffer str) { 8 | int len = str.length(); 9 | int newlen = 0; 10 | for(int i = 0; i < str.length(); i++) { 11 | if(str.charAt(i) == ' ') { 12 | newlen = newlen + 3; 13 | } 14 | else { 15 | newlen ++; 16 | } 17 | } 18 | char []newstr = new char[newlen]; 19 | int j = 0; 20 | for(int i = 0 ; i < str.length(); i++) { 21 | if (str.charAt(i) == ' ') { 22 | newstr[j++] = '%'; 23 | newstr[j++] = '2'; 24 | newstr[j++] = '0'; 25 | }else { 26 | newstr[j++] = str.charAt(i); 27 | } 28 | } 29 | 30 | return String.valueOf(newstr); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/BAT/String/最小字典序字符串数组.java: -------------------------------------------------------------------------------- 1 | package BAT.String; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | 6 | /** 7 | * Created by 周杰伦 on 2018/3/24. 8 | */ 9 | public class 最小字典序字符串数组 { 10 | public static void main(String[] args) { 11 | String[] arr = {"abc", "de", "a", "c"}; 12 | minSortStrArray(arr); 13 | System.out.println(Arrays.toString(arr)); 14 | } 15 | public static String[] minSortStrArray(String []arr) { 16 | Arrays.sort(arr, new Comparator() { 17 | @Override 18 | public int compare(String o1, String o2) { 19 | return ((o1 + o2).compareTo(o2 + o1)); 20 | } 21 | }); 22 | return arr; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/BAT/String/英文句子逆序.java: -------------------------------------------------------------------------------- 1 | package BAT.String; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * Created by 周杰伦 on 2018/3/24. 7 | * 先将整句逆序,再将单词逆序 8 | */ 9 | public class 英文句子逆序 { 10 | public static void main(String[] args) { 11 | String s = "hello mike"; 12 | System.out.println(reverseSentence(s)); 13 | } 14 | public static String reverseSentence(String str) { 15 | char []a = str.toCharArray(); 16 | reverse(a, 0, a.length - 1); 17 | int i = 0; 18 | int begin = 0; 19 | while (i <= a.length - 1) { 20 | if (i > 1 && a[i] == ' ') { 21 | reverse(a, begin, i - 1); 22 | begin = i + 1; 23 | }else if (i == a.length - 1) { 24 | reverse(a, begin, i); 25 | } 26 | 27 | i ++; 28 | } 29 | 30 | return String.valueOf(a); 31 | } 32 | public static void reverse(char[] str,int start, int end) { 33 | int l = start; 34 | int r = end; 35 | while (l < r) { 36 | char temp = str[l]; 37 | str[l] = str[r]; 38 | str[r] = temp; 39 | l ++; 40 | r --; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/BAT/大数据/AgeSort.java: -------------------------------------------------------------------------------- 1 | package BAT.大数据; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/19. 5 | * 10亿个年龄排序 6 | * 使用计数排序,因为年龄分布在0到200,复杂度为o(n+k) 7 | * 堆排序,复杂度为o(nlogk) k为200个年龄。排n次。 8 | */ 9 | public class AgeSort { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/BAT/大数据/MR.java: -------------------------------------------------------------------------------- 1 | package BAT.大数据; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/19. 5 | */ 6 | public class MR { 7 | } 8 | -------------------------------------------------------------------------------- /src/BAT/大数据/WordCount.java: -------------------------------------------------------------------------------- 1 | package BAT.大数据; 2 | 3 | import java.util.concurrent.CopyOnWriteArrayList; 4 | import java.util.concurrent.locks.AbstractQueuedSynchronizer; 5 | import java.util.concurrent.locks.Condition; 6 | import java.util.concurrent.locks.Lock; 7 | import java.util.concurrent.locks.ReentrantLock; 8 | 9 | /** 10 | * Created by 周杰伦 on 2018/3/19. 11 | * map将单词映射成key -value形式 12 | * reduce任务合并key-value。变成key valuelist,进而变成key sum; 13 | * 得到每个单词的词频。返回结果。 14 | */ 15 | public class WordCount { 16 | public static void main(String[] args) { 17 | CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList(); 18 | Thread thread = new Thread(); 19 | Lock lock = new ReentrantLock(); 20 | Condition condition= lock.newCondition(); 21 | lock.tryLock(); 22 | AbstractQueuedSynchronizer AQS; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/BAT/大数据/ipSort.java: -------------------------------------------------------------------------------- 1 | package BAT.大数据; 2 | 3 | /** 4 | * Created by 周杰伦 on 2018/3/19. 5 | * 10亿个ip排序,32位无符号数。若转化为整数再排序。需要4g内存 6 | * 如果用Bitmap,只需一个长度为2^32的bit数组可以表示所有的32位二进制数。 7 | * 此时。所有ip由于不重复,只需保存在对应数的位置。 8 | * 保存后,遍历bitmap,取出来的ip就是有序的了, 9 | * 10 | */ 11 | public class ipSort { 12 | 13 | } 14 | --------------------------------------------------------------------------------