408 King's Road Data Structure After-Class Code Exercises (4)

Get into the habit of writing together! This is the 8th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

It is planned to update the implementation of all after-school code exercises in the 23 King's Road data structure. Although the exams are generally written in pseudo-code, I have implemented them all because of obsessive-compulsive disorder. The warehouse is here.

  • Linear table

2.2.3, 08

image.png

  • Violent method, open another array, and then loop!
  • One cycle won't work, twice!
  • Parameters m and n are the lengths of the two linear tables
  • Time complexity O(m+n), space complexity O(m+n)
void change(SqList &list, int m, int n) {
  // 前一个线性表[0, m-1], 后一个[m, m+n-1]
  SqList copied = list;
  int k = -1;
  for (int i = m; i < m+n; i++) {
    copied.data[++k] = list.data[i];
  }
  for (int i = 0; i < m; i++) {
    copied.data[++k] = list.data[i];
  }
  list = copied;
}
复制代码
  • Of course, there is also a way to reverse the whole array first, and then reverse the two linear tables respectively.
  • For example, [1, 2, 3, 4] is reversed into [4, 3, 2, 1], then one of the linear tables [4, 3] is reversed into [3, 4], and the other [2, 1] Reverse it to [1, 2], the final result is [3, 4, 1, 2]
  • Time complexity O(m+n), space complexity O(1)
// 逆置,跟第二题类似, l=left, r=right
void reverse(SqList &list, int l, int r) {
  if (l > r || r > list.length) return;

  int mid = (l + r) / 2;
  // 注意边界
  for (int i = 0; i <= mid - l; i++) {
    swap(list.data[l+i], list.data[r-i]);
  }
}

void change2(SqList &list, int m, int n) {
  // 注意参数
  reverse(list, 0, m+n-1);
  reverse(list, 0, n-1);
  reverse(list, n, m+n-1);
}
复制代码

2.2.3, 09

image.png

  • Incremental order, violent cycle
  • There are many boundary conditions to consider, which is prone to errors
  • Time complexity O(n), space complexity O(1)
void find_x2(SqList &list, int x) {
  // 1.二分找x
  int low = 0, high = list.length - 1, mid;
  while (low <= high) {
    mid = (low + high) / 2;
    if (list.data[mid] == x) break;
    else if (list.data[mid] < x) low = mid + 1;
    else high = mid - 1;
  }

  // 2.找到了
  if (list.data[mid] == x && mid != list.length - 1) {
    swap(list.data[mid], list.data[mid + 1]);
    return;
  }
  
  // 3.没找到, 此时low>high
  list.length++;
  int i = list.length - 2;
  while (i > high) {
    list.data[i + 1] = list.data[i]; 
    i--;
  }
  list.data[i + 1] = x;
}
复制代码
  • Optimization, using binary search, no need to record the value of i
  • When the lookup fails, high will record the last element less than x
  • Time complexity O(logn), space complexity O(1)
void find_x2(SqList &list, int x) {
  int low = 0, high = list.length - 1, mid;
  while (low <= high) {
    mid = (low + high) / 2;
    if (list.data[mid] == x) break;
    else if (list.data[mid] < x) low = mid + 1;
    else high = mid - 1;
  }

  // 找到了并且不是最后一个元素
  if (list.data[mid] == x && mid != list.length - 1) {
    swap(list.data[mid], list.data[mid + 1]);
    return;
  }

  // 没找到
  list.length++;
  int i = list.length - 2;
  while (i > high) {
    list.data[i + 1] = list.data[i]; 
    i--;
  }
  list.data[i + 1] = x;
}
复制代码

Guess you like

Origin juejin.im/post/7084781212794880037