
左神数据结构
深入理解算法
飞翔的代码人
这个作者很懒,什么都没留下…
展开
-
左神_中级班_07
2.题目二给定一个整数数组A,长度为n,有 1 <= A[i] <= n,且对于[1,n]的整数,其中部分整数会重复出现而部分不会出现。实现算法找到[1,n]中所有未出现在A中的整数。提示:尝试实现O(n)的时间复杂度和O(1)的空间复杂度(返回值不计入空间复杂度)。输入描述:一行数字,全部为整数,空格分隔A0 A1 A2 A3…输出描述:一行数字,全部为整数,空格分隔R0 R1 R2 R3…示例1:输入1 3 4 3输出2 // 请保证arr[0..N-1]上的原创 2021-06-17 10:30:21 · 354 阅读 · 0 评论 -
左神_中级班_06
1.给你一个字符串类型的数组arr,譬如:String[] arr = { “b\cst”, “d\”, “a\d\e”, “a\b\c” };你把这些路径中蕴含的目录结构给画出来,子目录直接列在父目录下面,并比父目录向右进两格,就像这样:abcdebcstd同一级的需要按字母顺序排列,不能乱。public class Problem01_GetFolderTree { public static class Node { public String name; pub原创 2021-05-31 10:01:09 · 223 阅读 · 0 评论 -
左神_中级班_04
2.实现一个特殊栈,除了基本功能,还能返回栈中最小元素解法:两个栈:stackData,stackMin public static class MyStack1 { private Stack<Integer> stackData; private Stack<Integer> stackMin; public MyStack1() { this.stackData = new Stack<Integer>(); this.stackMin原创 2021-05-05 10:40:30 · 150 阅读 · 0 评论 -
左神_中级班_03
1.洗衣机问题:每个位置的瓶颈的最大值求出每个位置左边需求,与右边需求(1)若左<0,右<0: 至少|左|+|右|(2)若其他: 至少max(|左|+|右|) public static int MinOps(int[] arr) { if(arr==null||arr.length==0){ return 0; } int sum=0; for (int i : arr) { sum+=i; } int size=arr.length; if(s原创 2021-04-27 10:13:12 · 382 阅读 · 0 评论 -
左神_中级班_02
1.给定一个数组arr,求差值为k的去重数组对 public static List<List<Integer>> allPair(int[] arr, int k) { HashSet<Integer> hashSet=new HashSet<>(); for (int i = 0; i < arr.length; i++) { hashSet.add(arr[i]); } List<List<Integer>原创 2021-04-23 11:32:26 · 172 阅读 · 0 评论 -
左神_中级班_01
1.数轴上从左到右有n各点a[0], a[1], ……,a[n -1],给定一根长度为L的绳子,求绳子最多能覆盖其中的几个点(1)右侧端点来到每一个点能覆盖多少 // 长度为L的绳子最多覆盖几个点,请保证arr有序 public static int maxPoint(int[] arr, int L) { if(arr==null||arr.length==0){ return 0; } int numMax=Integer.MIN_VALUE; for(int right=0原创 2021-04-20 11:38:52 · 313 阅读 · 0 评论 -
左神_基础提升班_08_暴力递归到动态规划02
1.排成一条线的纸牌博弈问题(1)暴力解法 public static int win1(int[] arr) { if(arr==null||arr.length==0){ return 0; } return Math.max(f(arr,0,arr.length-1),s(arr,0,arr.length-1)); } //先手函数 public static int f(int[] arr, int i, int j) { if(i==j){ return ar原创 2021-04-13 08:48:44 · 185 阅读 · 0 评论 -
左神_基础提升班_07_暴力递归到动态规划01
1.机器人到达指定位置方法数(1)暴力递归 public static int ways1(int N, int M, int K, int P) { return walk(N,M,K,P); } // N : 位置为1 ~ N,固定参数 // cur : 当前在cur位置,可变参数 // rest : 还剩res步没有走,可变参数 // P : 最终目标位置是P,固定参数 // 该函数的含义:只能在1~N这些位置上移动,当前在cur位置,走完rest步之后,停在P位置的方法数作为返回原创 2021-04-10 11:44:55 · 219 阅读 · 0 评论 -
左神_基础提升班_06_大数据以及位运算
1.给定两个有符号32位整数a和b,返回a和b中较大的a * returnA+b*returnB ---------returnA与returnB互斥,可用加法表达if else返回a的条件:(1)if(a和b符号不相同):a>0 返回a(2)if(a和b符号相同):a-b>=0 返回a //请保证参数n,不是1就是0的情况下 //1-->0 //0-->1 public static int flip(int n) { return n ^ 1;//原创 2021-04-09 10:08:00 · 162 阅读 · 1 评论 -
左神_基础提升班_05_Morris遍历和树形dp
1.Morris遍历morris:开始时,cur来到头结点位置(1)如果cur没有左孩子,cur向右移动(cur=cur.right);(2)如果cur有左孩子,找到左子树上最右的节点mostRight:a. 若mostRight的右指针指向空,让其指向cur,然后cur向左移动(cur=cur.left);b. 若mostRight的右指针指向cur,让其指向null,然后cur向右移动(cur=cur.right);(3)cur为空时遍历停止; public static class N原创 2021-04-04 11:53:11 · 166 阅读 · 0 评论 -
左神_基础提升班_04_滑动窗口以及单调栈
1.滑动窗口(1)通用形式 public static class WindowMax{ public int L; public int R; public int[] arr; public LinkedList<Integer> list; public WindowMax(int [] a){ this.arr=a; this.L=-1; this.R=0; this.list=new LinkedList<>(); }原创 2021-04-03 10:32:00 · 285 阅读 · 0 评论 -
左神_基础提升班_03_kmp和manacher算法
1.kmp算法 public static int getIndexOf(String s, String m) { if(s==null||m==null||m.length()<1||m.length()>s.length()){ return -1; } char[] cs = s.toCharArray(); char[] cm = m.toCharArray(); int i=0; int j=0; int[] next=getNextArray(c原创 2021-03-31 09:18:07 · 212 阅读 · 0 评论 -
左神_基础提升班_01_哈希函数与哈希表等
1.哈希函数与哈希表//遍历键的集合for(String key: hashMap.keySet()){ System.out.println(key);}//遍历值的集合for(String value:hashMap.values()){ System.out.println(value);}//遍历hashMap集合Set<Character> set=hashMap.keySet()for(Character ch : set){ System.ou原创 2021-03-29 09:10:43 · 160 阅读 · 0 评论 -
第八课.暴力递归
N皇后问题:法一:正规解法public static int num1(int n) {//n个皇后 if (n < 1) { return 0; } int[] record = new int[n];//record[i] -->i行的皇后,放在了第几列 return process1(0, record, n); } //潜台词:record[0..i-1]的皇后,任何两个皇后一定不共行、不共列、不共斜线 //目前来到第i行 //record[0..i-1原创 2021-03-25 11:44:04 · 191 阅读 · 0 评论 -
第四课:二叉树
1.二叉树递归遍历前序: public static void preOrderRecur(Node head) { if(head==null){ return; } System.out.println(head.value); preOrderRecur(head.left); preOrderRecur(head.right); }中序: public static void inOrderRecur(Node head) { if(head==null){原创 2021-03-22 10:33:56 · 152 阅读 · 0 评论 -
第三课:链表
1.反转单向链表 public static class Node { public int value; public Node next; public Node(int data) { this.value = data; } } public static Node reverseList(Node head) { Node pre=null; Node next=null; while(head!=null){ next=head.next;原创 2021-03-17 10:09:47 · 134 阅读 · 1 评论 -
第二课:复杂排序算法
1.归并排序 public static void mergeSort(int[] arr) { if(arr==null||arr.length<2){ return; } mergeSort(arr,0,arr.length-1); } public static void mergeSort(int[] arr, int l, int r) { if (l==r){ return; } int mid=l+(r-l)/2; mergeSort(arr原创 2021-03-10 18:55:06 · 249 阅读 · 0 评论 -
第一课:简单排序算法
选择排序:public static void selectionSort(int[] arr) { if(arr==null||arr.length<2){ return; } for (int i=0;i<arr.length-1;i++){ int minIndex=i; for (int j=i+1;j<arr.length;j++){ if (arr[j]<=arr[minIndex]){ minIndex=j; }原创 2021-03-08 15:02:57 · 158 阅读 · 0 评论