数组、链表和跳表

289 阅读2分钟

数组、链表和跳表

数组

时间复杂度

类型 复杂度
prepend O(1)
append O(1)
lookup O(1)
insert O(N)
delete O(N)

链表

增加元素

image-20200720000115466

删除节点

image-20200720000158790

双向链表

image-20200720000301180

时间复杂度

类型 复杂度
preappend O(1)
append O(1)
lookup O(N)
insert O(1)
delete O(1)

跳表

链表加速

链表缺陷修复-->

  • 升维
  • 空间换时间

image-20200720001028362

添加索引

image-20200720001055181

二级索引

image-20200720001141652

五级索引

image-20200720001256122

时间复杂度

image-20200720001436864

image-20200720001502793

小结

  • 数组、链表、调表的原理和实现
  • 三者的时间复杂度、空间复杂度
  • 工程运用
  • 跳表:升维思想+空间换时间

练习

步骤:

  • 5-10min:读题和思考
  • 有思路:自己开始做和写代码;不然,马上蓝题解
  • 默写背诵
  • 自己写

两数之和题目: https://leetcode-cn.com/problems/two-sum/

Array 实战题目

  • leetcode-cn.com/problems/co…

    • 解题思路

      题目图

      算法流程:使用双指针i,j,分别位于容器壁两端,根据后面的规则,并且更新最大值area,直到i==j时返回其area

      证明与规则

      • 在每一个状态下,无论长板或短板收窄 1 格,都会导致水槽 底边宽度 -1
        • 若向内移动短板,水槽的短板 min(h[i], h[j])可能变大,因此水槽面积 S(i,j) 可能增大。
        • 若向内移动长板,水槽的短板 min(h[i], h[j])不变或变小,下个水槽的面积一定小于当前水槽面积。

      代码

      public static int maxArea(int[] height){
          if (height == null || height.length < 2) return 0;
          int area = 0;
          int left = 0;
          int right = height.length-1;
          while (left < right){
              area = Math.max(area,(right-left)*Math.min(height[left],height[right]));
              if (height[left] > height[right]) right--;
              else if (height[left] < height[right]) left++;
              else {
                  left++;
                  right--;
              }
          }
          return area;
      }
      
      // leetcode jyd书写的代码
      class Solution {
          public int maxArea(int[] height) {
              int i = 0, j = height.length - 1, res = 0;
              while(i < j){
                  res = height[i] < height[j] ? 
                      Math.max(res, (j - i) * height[i++]): 
                      Math.max(res, (j - i) * height[j--]); 
              }
              return res;
          }
      }
      作者:jyd
      链接:https://leetcode-cn.com/problems/container-with-most-water/solution/container-with-most-water-shuang-zhi-zhen-fa-yi-do/
      来源:力扣(LeetCode)
      著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
      
  • leetcode-cn.com/problems/mo…

  • leetcode.com/problems/cl…

  • leetcode-cn.com/problems/3s…(高频老题)

Linked List 实战题目

课后作业

参考链接