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

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

I plan 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 pseudocode, I have implemented all of them for obsessive-compulsive disorder. The warehouse is here.

  • Linear table

2.2.3, 10

image.png

  • This real question is basically the same as question 8, we can directly copy it and change the parameters
  • The first is to solve violently, open a new array, and copy it in separately (it is the same to change the structure to an array)
  • This time complexity is O(n), and the space complexity is also O(n)
  • In order to save space, you can also create an array of size p to temporarily store the previous array [0, p-1], move the original array to the left as a whole and then put it back in sequence, the space complexity will be reduced to O(p)
void change(SqList &list, int p, int n) {
  // 1.左右两个数组分别是[0, p-1], [p, n-1]
  SqList copied = list;
  int k = -1;
  // 2.分别复制进去
  for (int i = p; i < n; i++) {
    copied.data[++k] = list.data[i];
  }
  for (int i = 0; i < p; i++) {
    copied.data[++k] = list.data[i];
  }
  // 3.新换旧
  list = copied;
}
复制代码
  • Four or two dials a thousand pounds, the whole is reversed, and then reversed separately
  • For example, [1, 2, 3, 4] is reversed into [4, 3, 2, 1], then one of the arrays [4, 3] is reversed into [3, 4], and the other [2, 1] is reversed Set to [1, 2], the final result is [3, 4, 1, 2]
  • Time complexity O(n), space complexity O(1)
void reverse(SqList &list, int l, int r) {
  if (l > r || r > list.length) return;

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

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

2.2.3, 11

image.png

  • Find the median after merging two ordered sequences, then the violent solution will be merged directly.
  • You can copy question 7 directly, and then return to the number on the (A.length + B.length)/2position
  • Then you will find that you don't need to merge all, and you don't need an auxiliary table to store data, you just need to loop to this position.
  • Pay attention to the requirements in the title, mid should be equal to(A.length + B.length - 1) / 2
  • Time complexity O(n), space complexity O(1)
int merge(SqList A, SqList B) {
  int i = 0, j = 0, k = -1, mid = (A.length + B.length - 1) / 2;
  
  // 条件也不需要,因为我们找到中间值就会直接return
  while (1) {
    ++k;
    if (A.data[i] <= B.data[j]) {
      if (k == mid) return A.data[i];
      i++;
    } else {
      if (k == mid) return B.data[j];
      j++;
    }
  }
}
复制代码
  • You can also loop directly to mid, you don't need to judge every time==mid
int merge2(SqList A, SqList B) {
  int i = 0, j = 0, mid = (A.length + B.length - 1) / 2 ;
  while (i+j < mid) {
    if (A.data[i] <= B.data[j]) i++;
    else j++;
  }
  return A.data[i] < B.data[j] ? A.data[i] : B.data[j];
}
复制代码
  • The optimal solution is not recommended for the exam, the time cost is too high (except for the big guys), after all, the violent solution seems to have a gap of only 5 points at most, it is unnecessary
  • I won't write it here, you can look at Wangdao's answer

Guess you like

Origin juejin.im/post/7085255116026019847