├── src └── com │ ├── algorithdemo │ ├── sort │ │ ├── Sort.java │ │ ├── MergeSort.java │ │ ├── QuickSort.java │ │ └── InsertSort.java │ ├── list │ │ ├── LinkedList.java │ │ └── DoubleLinkedList.java │ ├── stack │ │ └── MyStack.java │ ├── tree │ │ ├── Solution.java │ │ ├── TreeNode.java │ │ ├── TreePrint.java │ │ ├── BalanceBTree.java │ │ ├── CompleteTree.java │ │ └── BinarySearchTree.java │ ├── string │ │ ├── StringDemo.java │ │ └── StringDemo2.java │ ├── search │ │ └── SearchDemo.java │ └── fibonacci │ │ └── Demo.java │ ├── lintcode │ ├── Solution.java │ ├── Solution5.java │ ├── Solution2.java │ ├── Solution1.java │ ├── Solution80.java │ ├── Solution6.java │ ├── Solution49.java │ ├── Solution9.java │ ├── Solution55.java │ ├── Solution3.java │ ├── Solution113.java │ ├── Solution14.java │ ├── MyQueue.java │ ├── Solution362.java │ └── MinStack.java │ └── leetode │ ├── Solution239.java │ └── Solution20.java └── readme.md /src/com/algorithdemo/sort/Sort.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/sort/Sort.java -------------------------------------------------------------------------------- /src/com/algorithdemo/list/LinkedList.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/list/LinkedList.java -------------------------------------------------------------------------------- /src/com/algorithdemo/sort/MergeSort.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/sort/MergeSort.java -------------------------------------------------------------------------------- /src/com/algorithdemo/sort/QuickSort.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/sort/QuickSort.java -------------------------------------------------------------------------------- /src/com/algorithdemo/stack/MyStack.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/stack/MyStack.java -------------------------------------------------------------------------------- /src/com/algorithdemo/tree/Solution.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/tree/Solution.java -------------------------------------------------------------------------------- /src/com/algorithdemo/tree/TreeNode.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/tree/TreeNode.java -------------------------------------------------------------------------------- /src/com/algorithdemo/tree/TreePrint.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/tree/TreePrint.java -------------------------------------------------------------------------------- /src/com/algorithdemo/string/StringDemo.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/string/StringDemo.java -------------------------------------------------------------------------------- /src/com/algorithdemo/tree/BalanceBTree.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/tree/BalanceBTree.java -------------------------------------------------------------------------------- /src/com/algorithdemo/string/StringDemo2.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/string/StringDemo2.java -------------------------------------------------------------------------------- /src/com/algorithdemo/list/DoubleLinkedList.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/5zhu/algorithm-learning/master/src/com/algorithdemo/list/DoubleLinkedList.java -------------------------------------------------------------------------------- /src/com/lintcode/Solution.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | /** 4 | *204. 单例 5 | * 6 | * 单例 是最为最常见的设计模式之一。对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例。例如,对于 class Mouse (不是动物的mouse哦),我们应将其设计为 singleton 模式。 7 | 8 | 你的任务是设计一个 getInstance 方法,对于给定的类,每次调用 getInstance 时,都可得到同一个实例。 9 | */ 10 | class Solution { 11 | 12 | private Solution(){} 13 | 14 | public static Solution instance = new Solution(); 15 | 16 | /** 17 | * @return: The same instance of this class every time 18 | */ 19 | public static Solution getInstance() { 20 | // write your code here 21 | return instance; 22 | } 23 | }; -------------------------------------------------------------------------------- /src/com/lintcode/Solution5.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 5. 第k大元素 在数组中找到第k大的元素 7 | * 8 | * 样例 9 | 给出数组 [9,3,2,4,8],第三大的元素是 4 10 | 11 | 给出数组 [1,2,3,4,5],第一大的元素是 5,第二大的元素是 4,第三大的元素是 3,以此类推 12 | */ 13 | public class Solution5 { 14 | 15 | /* 16 | * @param k : description of k 17 | * @param nums : array of nums 18 | * @return: description of return 19 | */ 20 | public int kthLargestElement(int k, int[] nums) { 21 | // write your code here 22 | // write your code here 23 | Arrays.sort(nums); 24 | return nums[nums.length - k]; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/com/lintcode/Solution2.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | /** 4 | * 2. 尾部的零 5 | * 设计一个算法,计算出n阶乘中尾部零的个数 6 | * 7 | * 样例 8 | 11! = 39916800,因此应该返回 2 9 | * 10 | * O(logN)的时间复杂度 11 | */ 12 | 13 | public class Solution2 { 14 | 15 | /* 16 | * @param n: An integer 17 | * @return: An integer, denote the number of trailing zeros in n! 18 | */ 19 | public long trailingZeros(long n) { 20 | // write your code here, try to do it without arithmetic operators. 21 | long count = 0; 22 | while (n > 0){ 23 | count += n / 5; 24 | n /= 5; 25 | } 26 | return count; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/com/lintcode/Solution1.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | /** 4 | * 1. A + B 问题 5 | * 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符。 6 | * 7 | * 挑战:显然你可以直接 return a + b,但是你是否可以挑战一下不这样做? 8 | */ 9 | 10 | public class Solution1 { 11 | 12 | /** 13 | * @param a: An integer 14 | * @param b: An integer 15 | * @return: The sum of a and b 16 | */ 17 | public int aplusb(int a, int b) { 18 | // write your code here 19 | if (a == 0) { 20 | return b; 21 | } 22 | if (b == 0){ 23 | return a; 24 | } 25 | int c = a ^ b; 26 | int d = (a & b)<<1; 27 | return aplusb(c, d); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/com/lintcode/Solution80.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 给定一个未排序的整数数组,找到其中位数。 7 | 8 | 中位数是排序后数组的中间值,如果数组的个数是偶数个,则返回排序后数组的第N/2个数。 9 | 10 | 样例 11 | 给出数组[4, 5, 1, 2, 3], 返回 3 12 | 13 | 给出数组[7, 9, 4, 5],返回 5 14 | */ 15 | public class Solution80 { 16 | 17 | /* 18 | * @param : A list of integers 19 | * @return: An integer denotes the middle number of the array 20 | */ 21 | public int median(int[] nums) { 22 | // write your code here 23 | // write your code here 24 | Arrays.sort(nums); 25 | int len = nums.length; 26 | int i = len % 2 ==0 ? len/2:len/2+1; 27 | return nums[i-1]; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/com/algorithdemo/sort/InsertSort.java: -------------------------------------------------------------------------------- 1 | package com.algorithdemo.sort; 2 | 3 | public class InsertSort { 4 | 5 | public static void main(String[] args) { 6 | int a[]={12,3,45,23,43,31,64,22,1,4,7}; 7 | insertSort(a); 8 | for(int i=0;i 0; j--) { 15 | if(arr[j] qmax = new LinkedList<>(); 13 | int[] res = new int[nums.length - k + 1]; 14 | int index = 0; 15 | for (int i = 0; i < nums.length; i++) { 16 | while (!qmax.isEmpty() && nums[qmax.peekLast()] <= nums[i]){ 17 | qmax.pollLast(); 18 | } 19 | qmax.addLast(i); 20 | if(qmax.peekFirst() == i -k){ 21 | qmax.pollFirst(); 22 | } 23 | if(i >= k -1){ 24 | res[index++]=nums[qmax.pollFirst()]; 25 | } 26 | } 27 | return res; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/com/leetode/Solution20.java: -------------------------------------------------------------------------------- 1 | package com.leetode; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Stack; 6 | 7 | public class Solution20 { 8 | 9 | public static boolean isValid(String s) { 10 | Stack stack = new Stack(); 11 | Map maps = new HashMap(); 12 | maps.put(')', '('); 13 | maps.put(']', '['); 14 | maps.put('}', '{'); 15 | char[] sarr = s.toCharArray(); 16 | for(int i = 0; i < sarr.length; i++){ 17 | if(!maps.containsKey(sarr[i])){ 18 | stack.push(sarr[i]); 19 | }else if(stack.empty() || maps.get(sarr[i]) != stack.pop()){ 20 | return false; 21 | } 22 | } 23 | return stack.empty(); 24 | } 25 | 26 | public static void main(String[] args) { 27 | String s1 = "(){{}}[(]"; 28 | System.out.println(isValid(s1)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/com/lintcode/Solution49.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | /** 4 | * 49. 字符大小写排序 5 | * 给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。 6 | * 7 | * 样例 8 | 给出"abAcD",一个可能的答案为"acbAD" 9 | */ 10 | public class Solution49 { 11 | 12 | /* 13 | * @param chars: The letter array you should sort by Case 14 | * @return: nothing 15 | */ 16 | public void sortLetters(char[] chars) { 17 | // write your code here 18 | char temp = ' '; 19 | int j = chars.length - 1; 20 | for (int i = 0; i <= j; i++) { 21 | if(Character.isLowerCase(chars[i])){ 22 | continue; 23 | }else{ 24 | while(Character.isUpperCase(chars[j])){ 25 | if(j <= i){ 26 | break; 27 | } 28 | j --; 29 | } 30 | temp=chars[i]; 31 | chars[i]=chars[j]; 32 | chars[j]=temp; 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/com/lintcode/Solution9.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * 9. Fizz Buzz 问题 8 | * 9 | * 给你一个整数n. 从 1 到 n 按照下面的规则打印每个数: 10 | 11 | 如果这个数被3整除,打印fizz. 12 | 如果这个数被5整除,打印buzz. 13 | 如果这个数能同时被3和5整除,打印fizz buzz 14 | */ 15 | public class Solution9 { 16 | 17 | /** 18 | * @param n: An integer 19 | * @return: A list of strings. 20 | */ 21 | public List fizzBuzz(int n) { 22 | // write your code here 23 | List list = new ArrayList(); 24 | for (int i = 1; i <= n; i ++){ 25 | 26 | if(i % 3 == 0 && i % 5 != 0){ 27 | list.add("fizz"); 28 | }else if(i % 3 != 0 && i % 5 == 0){ 29 | list.add("buzz"); 30 | }else if(i % 3 == 0 && i % 5 == 0){ 31 | list.add("fizz buzz"); 32 | }else { 33 | list.add(i + ""); 34 | } 35 | } 36 | return list; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/com/lintcode/Solution55.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | /** 4 | * 比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写 5 | */ 6 | public class Solution55 { 7 | 8 | /* 9 | * @param A: A string 10 | * @param B: A string 11 | * @return: if string A contains all of the characters in B return true else return false 12 | */ 13 | public boolean compareStrings(String A, String B) { 14 | // write your code here 15 | if(A.length() < B.length()){ 16 | return false; 17 | } 18 | int [] achar = new int[26]; 19 | int [] bchar = new int[26]; 20 | for (int i = 0; i < A.length(); i++) { 21 | achar[A.charAt(i) - 'A']++; 22 | } 23 | for (int i = 0; i < B.length(); i++) { 24 | bchar[B.charAt(i) - 'A']++; 25 | } 26 | for(int i = 0; i < 26; ++i) { 27 | if(bchar[i] > achar[i]){ 28 | return false; 29 | } 30 | } 31 | return true; 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/com/algorithdemo/search/SearchDemo.java: -------------------------------------------------------------------------------- 1 | package com.algorithdemo.search; 2 | 3 | 4 | public class SearchDemo { 5 | 6 | /** 7 | * ????????? O(log2N)__??2??? 8 | * @param arr 9 | * @param key 10 | * @return 11 | */ 12 | public static int binarySearch(int[] arr, int key){ 13 | long start = System.currentTimeMillis(); 14 | int low, high, mid; 15 | low = 0; 16 | high = arr.length - 1; 17 | while(low <= high){ 18 | mid = (low + high) >>> 1; 19 | if(arr[mid] < key){ 20 | low = mid + 1; 21 | }else if(arr[mid] > key){ 22 | high = mid - 1; 23 | }else{ 24 | long end = System.currentTimeMillis(); 25 | System.out.println(end - start); 26 | return mid; 27 | } 28 | } 29 | 30 | return -1; 31 | } 32 | 33 | public static void main(String[] args) { 34 | int[] arr = {1,3,5,7,9,12,23,41,53,63}; 35 | int i = SearchDemo.binarySearch(arr, 9); 36 | System.out.println(i); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/com/algorithdemo/tree/CompleteTree.java: -------------------------------------------------------------------------------- 1 | package com.algorithdemo.tree; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | /** 7 | * 判断二叉树是否为完全二叉树 8 | * 9 | * Created by geguofeng on 2018/1/15. 10 | */ 11 | public class CompleteTree { 12 | 13 | public static boolean isCRTree(TreeNode head){ 14 | if(head == null){ 15 | return true; 16 | } 17 | Queue queue = new LinkedList(); 18 | boolean leaf = false; 19 | TreeNode l = null; 20 | TreeNode r = null; 21 | queue.offer(head); 22 | while (!queue.isEmpty()){ 23 | head = queue.poll(); 24 | l = head.getLeft(); 25 | r = head.getRight(); 26 | if ((leaf && (l !=null || r!=null)) ||(l == null && r!= null)){ 27 | return false; 28 | } 29 | if(l != null){ 30 | queue.offer(l); 31 | } 32 | if(r != null){ 33 | queue.offer(r); 34 | }else { 35 | leaf = true; 36 | } 37 | } 38 | return true; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/com/lintcode/Solution3.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | /** 4 | * 3. 统计数字 5 | * 计算数字k在0到n中的出现的次数,k可能是0~9的一个值 6 | * 7 | * 样例 8 | 例如n=12,k=1,在 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],我们发现1出现了5次 (1, 10, 11, 12) 9 | * 10 | */ 11 | 12 | public class Solution3 { 13 | 14 | /* 15 | * @param : An integer 16 | * @param : An integer 17 | * @return: An integer denote the count of digit k in 1..n 18 | */ 19 | public int digitCounts(int k, int n) { 20 | // write your code here 21 | if(n == 0 && k == 0) 22 | return 1; // 特殊情况 23 | int temp = n, cnt = 0, pow = 1;//pow代表当前位的后面低位是多少,1为个位,10为十位,100位千位 24 | while(temp != 0) { 25 | int digit = temp % 10; // 根据当前位置数和k的大小关系,可以算出当前位置出现过k的次数 26 | if(digit < k) 27 | cnt += (temp / 10) * pow; 28 | else if(digit == k) 29 | cnt += (temp / 10) * pow + (n - temp * pow + 1); 30 | else { 31 | if(!(k == 0 && temp / 10 == 0)) // 排除没有更高位时,寻找的数为0的情况 32 | cnt += (temp / 10 + 1) * pow; 33 | } 34 | temp /= 10; 35 | pow *= 10; 36 | } 37 | return cnt; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/algorithdemo/fibonacci/Demo.java: -------------------------------------------------------------------------------- 1 | package com.algorithdemo.fibonacci; 2 | 3 | /** 4 | * 题目:有一座高度是10级台阶的楼梯,从下往上走,每跨一步只能向上1级或者2级台阶。要求用程序来求出一共有多少种走法。 5 | * 6 | * (原理:费氏数列) 7 | * 8 | * Created by Administrator on 2018/1/6. 9 | */ 10 | public class Demo { 11 | 12 | /** 13 | * 递归方法实现 (时间复杂度O(2^n)) 14 | * @param n 台阶数 15 | * @return 16 | */ 17 | public static int getClimbingWays(int n){ 18 | if(n < 1){ 19 | return 0; 20 | } 21 | if(n == 1){ 22 | return 1; 23 | } 24 | if(n == 2){ 25 | return 2; 26 | } 27 | return getClimbingWays(n-1) + getClimbingWays(n-2); 28 | 29 | } 30 | 31 | /*** 32 | * 动态规划算法实现 时间复杂度(O(n)) 33 | * @param n 34 | * @return 35 | */ 36 | public static int getClimbingWays2(int n){ 37 | if(n < 1){ 38 | return 0; 39 | } 40 | if(n == 1){ 41 | return 1; 42 | } 43 | if(n == 2){ 44 | return 2; 45 | } 46 | 47 | int a = 1; 48 | int b = 2; 49 | int temp = 0; 50 | 51 | for (int i = 3; i <= n; i++) { 52 | temp = a + b; 53 | a = b; 54 | b = temp; 55 | } 56 | return temp; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/com/lintcode/Solution113.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | /** 3 | * lintCode 题目113:最长单词 4 | * 5 | * 描述:给一个词典,找出其中所有最长的单词 6 | * 7 | * 样例 {"dog","google","facebook","internationalization","blabla"} 8 | * 9 | * 返回 ["internationalization"] 10 | * 11 | * 挑战:遍历两次的办法很容易想到,如果只遍历一次你有没有什么好办法? 12 | */ 13 | 14 | import java.util.List; 15 | import java.util.Stack; 16 | 17 | /** 18 | * Created by gegf on 2018/2/6. 19 | */ 20 | public class Solution113 { 21 | 22 | /* 23 | * @param dictionary: an array of strings 24 | * @return: an arraylist of strings 25 | */ 26 | public List longestWords(String[] dictionary) { 27 | // write your code here 28 | if(dictionary==null || dictionary.length==0){ 29 | return null; 30 | } 31 | Stack maxLenStack = new Stack(); 32 | maxLenStack.push(dictionary[0]); 33 | int maxLength = dictionary[0].length(); 34 | for (int i = 1; i < dictionary.length; i++) { 35 | if(dictionary[i].length() > maxLength){ 36 | maxLength = dictionary[i].length(); 37 | maxLenStack.clear(); 38 | maxLenStack.push(dictionary[i]); 39 | }else if(dictionary[i].length() == maxLength){ 40 | maxLenStack.push(dictionary[i]); 41 | }else{ 42 | continue; 43 | } 44 | } 45 | 46 | return maxLenStack; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/com/lintcode/Solution14.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | /** 4 | * 14. 二分查找 5 | * 6 | * 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。 7 | */ 8 | public class Solution14 { 9 | 10 | /** 11 | * @param nums: The integer array. 12 | * @param target: Target to find. 13 | * @return: The first position of target. Position starts from 0. 14 | */ 15 | public int binarySearch(int[] nums, int target) { 16 | // write your code here 17 | int length=nums.length; 18 | int pos=length/2; 19 | 20 | //目标值比最小值还要小或者比最大值还要大,则肯定不在数组中 21 | if(nums[0]>target||nums[length-1]target){ 27 | pos=pos/2; 28 | } 29 | 30 | //pos处的值小于或者等于目标值,那么目标值可能的范围就是[pos,pos*2]; 31 | for(int j=pos;j<=pos*2;j++){ 32 | if(nums[j]==target){ 33 | pos=j; 34 | break; 35 | } 36 | //遍历到pos*2位置了,还没找到目标值,那么目标值肯定不在数组中 37 | if((j==pos*2)&&(nums[j]!=target)){ 38 | return -1; 39 | } 40 | } 41 | 42 | //这样只是保证pos处的值是目标值,但不一定是第一次出现的 43 | //因此,比较pos前面一个元素的值与pos处的值是否相等,若相等,则pos往前挪一个 44 | while((pos>=1)&&(nums[pos]==nums[pos-1])){ 45 | pos--; 46 | } 47 | 48 | return pos; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/com/algorithdemo/tree/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | package com.algorithdemo.tree; 2 | 3 | public class BinarySearchTree { 4 | 5 | private Node root; 6 | 7 | public V get(K key){ 8 | return get(root, key); 9 | } 10 | 11 | private V get(Node node, K key){ 12 | return null; 13 | } 14 | 15 | static class Node{ 16 | 17 | private Node left; 18 | 19 | private Node right; 20 | 21 | private K key; 22 | 23 | private V value; 24 | 25 | public Node getLeft() { 26 | return left; 27 | } 28 | 29 | public void setLeft(Node left) { 30 | this.left = left; 31 | } 32 | 33 | public Node getRight() { 34 | return right; 35 | } 36 | 37 | public void setRight(Node right) { 38 | this.right = right; 39 | } 40 | 41 | public K getKey() { 42 | return key; 43 | } 44 | 45 | public void setKey(K key) { 46 | this.key = key; 47 | } 48 | 49 | public V getValue() { 50 | return value; 51 | } 52 | 53 | public void setValue(V value) { 54 | this.value = value; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "Node [left=" + left + ", right=" + right + ", key=" + key 60 | + ", value=" + value + "]"; 61 | } 62 | 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/com/lintcode/MyQueue.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * lintCode 44 用栈实现队列 7 | * 8 | * Created by gegf on 2018/2/11. 9 | */ 10 | public class MyQueue { 11 | 12 | public Stack stackPush; 13 | public Stack stackPop; 14 | 15 | public MyQueue() { 16 | // do intialization if necessary 17 | stackPush = new Stack(); 18 | stackPop = new Stack(); 19 | } 20 | 21 | /* 22 | * @param element: An integer 23 | * @return: nothing 24 | */ 25 | public void push(int element) { 26 | // write your code here 27 | stackPush.push(element); 28 | } 29 | 30 | /* 31 | * @return: An integer 32 | */ 33 | public int pop() { 34 | // write your code here 35 | if(stackPop.isEmpty() && stackPush.isEmpty()){ 36 | throw new RuntimeException("QUEUE IS EMPTY"); 37 | }else if(stackPop.isEmpty()){ 38 | while (!stackPush.isEmpty()){ 39 | stackPop.push(stackPush.pop()); 40 | } 41 | } 42 | return stackPop.pop(); 43 | } 44 | 45 | /* 46 | * @return: An integer 47 | */ 48 | public int top() { 49 | // write your code here 50 | if(stackPop.isEmpty() && stackPush.isEmpty()){ 51 | throw new RuntimeException("QUEUE IS EMPTY"); 52 | }else if(stackPop.isEmpty()){ 53 | while (!stackPush.isEmpty()){ 54 | stackPop.push(stackPush.pop()); 55 | } 56 | } 57 | return stackPop.peek(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/com/lintcode/Solution362.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.LinkedList; 6 | 7 | /** 8 | * 362.滑动窗口的最大值 9 | * 10 | * 给出一个可能包含重复的整数数组,和一个大小为 k 的滑动窗口, 从左到右在数组中滑动这个窗口,找到数组中每个窗口内的最大值。 11 | * 12 | * 样例 13 | 给出数组 [1,2,7,7,8], 滑动窗口大小为 k = 3. 返回 [7,7,8]. 14 | 15 | 最开始,窗口的状态如下: 16 | 17 | [|1, 2 ,7| ,7 , 8], 最大值为 7; 18 | 19 | 然后窗口向右移动一位: 20 | 21 | [1, |2, 7, 7|, 8], 最大值为 7; 22 | 23 | 最后窗口再向右移动一位: 24 | 25 | [1, 2, |7, 7, 8|], 最大值为 8. 26 | */ 27 | public class Solution362 { 28 | 29 | /* 30 | * @param nums: A list of integers 31 | * @param k: An integer 32 | * @return: The maximum number inside the window at each moving 33 | */ 34 | public ArrayList maxSlidingWindow(int[] nums, int k) { 35 | ArrayList result = new ArrayList(); 36 | // write your code here 37 | if (nums == null || k < 1 || nums.length < k){ 38 | return null; 39 | } 40 | LinkedList qmax = new LinkedList(); 41 | int[] res = new int[nums.length - k + 1]; 42 | int index = 0; 43 | for (int i = 0; i < nums.length; i ++){ 44 | while (!qmax.isEmpty() && nums[qmax.peekLast()] <= nums[i]){ 45 | qmax.pollLast(); 46 | } 47 | qmax.addLast(i); 48 | if(qmax.peekFirst() == i - k){ 49 | qmax.pollFirst(); 50 | } 51 | if(i >= k - 1){ 52 | res[index++] = nums[qmax.peekFirst()]; 53 | } 54 | } 55 | Arrays.stream(res).forEach(n -> result.add(n)); 56 | return result; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/com/lintcode/MinStack.java: -------------------------------------------------------------------------------- 1 | package com.lintcode; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | *12. 带最小值操作的栈. 7 | * 8 | * 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。 9 | 10 | 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。 11 | */ 12 | public class MinStack { 13 | 14 | private Stack stackData; 15 | private Stack stackMin; 16 | 17 | public MinStack(){ 18 | // do intialization if necessary 19 | this.stackData = new Stack(); 20 | this.stackMin = new Stack(); 21 | } 22 | 23 | /* 24 | * @param number: An integer 25 | * @return: nothing 26 | */ 27 | public void push(int number) { 28 | // write your code here 29 | if(stackMin.isEmpty()){ 30 | this.stackMin.push(number); 31 | }else if(number <= this.min()){ 32 | this.stackMin.push(number); 33 | } 34 | this.stackData.push(number); 35 | } 36 | 37 | /* 38 | * @return: An integer 39 | */ 40 | public int pop(){ 41 | // write your code here 42 | if(this.stackData.isEmpty()){ 43 | throw new RuntimeException("Your stack is empty."); 44 | } 45 | int value = this.stackData.pop(); 46 | if(value == this.min()){ 47 | this.stackMin.pop(); 48 | } 49 | return value; 50 | } 51 | 52 | /* 53 | * @return: An integer 54 | */ 55 | public int min(){ 56 | // write your code here 57 | if(this.stackMin.isEmpty()){ 58 | throw new RuntimeException("Your stack is empty."); 59 | } 60 | return this.stackMin.peek(); 61 | } 62 | 63 | public static void main(String[] args) { 64 | MinStack s = new MinStack(); 65 | s.push(1); 66 | s.pop(); 67 | s.push(2); 68 | s.push(3); 69 | int i = s.min(); 70 | System.out.println(i); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # algorithm-learning 2 | 算法与数据结构 3 | 4 | ----------------------- 5 | ## 目录 6 | 7 | - [链表](src/com/algorithdemo/list/): 8 | - [二分法查找](src/com/algorithdemo/search/): 9 | - [排序](src/com/algorithdemo/sort/): 10 | - [一个有getMin功能的栈](src/com/algorithdemo/stack): 11 | - [判断两个字符串是否互为变形](src/com/algorithdemo/string) 12 | - [判断二叉树是否是平衡二叉树](src/com/algorithdemo/tree/BalanceBTree.java) 13 | - [判断二叉树是否是完全二叉树](src/com/algorithdemo/tree/CompleteTree.java) 14 | - [从上往下打印出二叉树的每个节点,同层节点从左至右打印](src/com/algorithdemo/tree/Solution.java) 15 | - [二叉树遍历](src/com/algorithdemo/tree/TreePrint.java) 16 | - [动态规范简单应用](src/com/algorithdemo/fibonacci/Demo.java):题目:有一座高度是10级台阶的楼梯,从下往上走,每跨一步只能向上1级或者2级台阶。要求用程序来求出一共有多少种走法。 17 | 18 | ## LintCode 刷题 19 | - [1. A + B 问题](src/com/lintcode/Solution1.java) [LintCode 1](http://www.lintcode.com/zh-cn/problem/a-b-problem/) 20 | - [2. 尾部的零](src/com/lintcode/Solution2.java) [LintCode 2](http://www.lintcode.com/zh-cn/problem/trailing-zeros/) 21 | - [3. 统计数字](src/com/lintcode/Solution3.java) [LintCode 3](http://www.lintcode.com/zh-cn/problem/digit-counts/) 22 | - [5. 第k大元素](src/com/lintcode/Solution5.java) [LintCode 5](http://www.lintcode.com/zh-cn/problem/kth-largest-element/) 23 | - [9. Fizz Buzz 问题](src/com/lintcode/Solution9.java) [LintCode 9](http://www.lintcode.com/zh-cn/problem/fizz-buzz/) 24 | - [12. 带最小值操作的栈](src/com/lintcode/MinStack) [LintCode 12](http://www.lintcode.com/zh-cn/problem/min-stack/) 25 | - [14. 二分查找](src/com/lintcode/Solution14.java)[LintCode 14](http://www.lintcode.com/zh-cn/problem/first-position-of-target/) 26 | - [40. 用栈实现队列](src/com/lintcode/MyQueue.java): [LintCode 40](http://www.lintcode.com/zh-cn/problem/implement-queue-by-two-stacks/) 27 | - [49. 字符大小写排序](src/com/lintcode/Solution49.java) :[LintCode 49](http://www.lintcode.com/zh-cn/problem/sort-letters-by-case/) 28 | - [55.比较字符串](src/com/lintcode/Solution55.java) [LintCode 55](http://www.lintcode.com/zh-cn/problem/compare-strings/) 29 | - [80. 中位数](src/com/lintcode/Solution80.java) [LintCode 80](http://www.lintcode.com/zh-cn/problem/median/) 30 | - [133.最长单词](src/com/lintcode/Solution113.java): [LintCode 133](http://www.lintcode.com/zh-cn/problem/longest-words/) 31 | - [204. 单例](src/com/lintcode/Solution.java) [LintCode 204](http://www.lintcode.com/zh-cn/problem/singleton/) 32 | - [362. 滑动窗口的最大值](src/com/lintcode/Solution362.java): [LintCode 362](http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/) 33 | --------------------------------------------------------------------------------