408 After-school code exercises for kingly data structure (twenty-three)

Continue to create, accelerate growth! This is the first day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event

foreword

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.

The code is written in C++, all of which can be compiled and run, including violent solutions and optimal solutions as much as possible (if the exam time is not enough, you can directly attack a violent one, and you can get more than half of the points).

Continuous update, current update progress:

  • Linear Table 14/14
  • Linked List 25/25
  • Stack 3/3
  • queue
  • ...

for reference only! There will be some grammar that the exam is not allowed to write, and there may be some mistakes.

3.1.5, 3

image.png

  • The initial state and final state of the stack clearly written in the question are empty, so the number of times of stacking and popping should be the same
  • The stack is recorded as +1, the pop-out is -1, and the final result is 0, which is legal. If there is a case of <0 in the middle process, it is illegal.
  • Time complexity O(n), space complexity O(1)
bool isValid(string str) {
  int cnt = 0;
  for (int i = 0; i < str.length(); i++) {
    if (str[i] == 'I') cnt++;
    if (str[i] == 'O') cnt--;

    if (cnt < 0) return false;
  }

  if (cnt == 0) return true;
  return false;
}
复制代码

4

image.png

  • Centrosymmetric
  • Traverse, use the stack to store the first half of the elements, and then compare the second half of the elements
  • Only need to pay attention to the determination of odd and even elements
  • Time complexity O(n), space complexity O(n/2)
bool isSymmetry(LinkList L, int n) {
  char stk[n/2];      // 字符栈
  LNode *p = L->next;

  // 1.存入链表前一半的元素
  for (int i = 0; i < n/2; i++) {
    stk[i] = p->data;
    p = p->next;
  }

  // 2.如果链表元素个数为奇数,则中间元素不用比较
  if (n % 2 == 1) p = p->next;

  // 3.对比后半部分
  for (int i = n/2 - 1; i >= 0; i--) {
    if (stk[i] != p->data) return false;
    p = p->next;
  }
  return true;
}
复制代码

5

image.png

  • The key to sharing the sequential stack is the determination of the two stations into and out of the stack. s1 is a stack in the traditional sense. When the stack is pushed, the top pointer of the stack is +1, and s2 is the opposite. When the stack is pushed, the top pointer of the stack is -1. pop the stack
  • Note that "the stack is full, and the stack is empty "
  • The way to judge the stack is full istop2-top1 == 1
#define maxsize 100
typedef struct {
  int stack[maxsize];
  int top1;
  int top2;
} stk;

// 栈的初始化
void initStack(stk &s) {
  s.top1 = -1;
  s.top2 = maxsize;
}

// 判断栈空
bool isEmpty(stk &s, int num) {
  if (num != 1 && num != 2) {
    cout << "栈号输入错误" << endl;
    return false;
  }

  if (num == 1) return s.top1 == -1;
  return s.top2 == maxsize;
}

// 判断栈满
bool isFull(stk &s) {
  if (s.top2 - s.top1 == 1) return true;
  return false;
}

// 入栈
bool push(stk &s, int num, int x) {
  if (num != 1 && num != 2) {
    cout << "栈号输入错误" << endl;
    return false;
  }

  if (isFull(s)) {
    cout << "栈满" << endl;
    return false;
  }

  if (num == 1) s.stack[++s.top1] = x;
  else s.stack[--s.top2] = x;
  return true;
}

// 出栈
bool pop(stk &s, int num, int &x) {
  if (num != 1 && num != 2) {
    cout << "栈号输入错误" << endl;
    return false;
  }

  if (isEmpty(s, num)) {
    cout << "栈空" << endl;
    return false;
  }

  if (num == 1) x = s.stack[s.top1--];
  else x = s.stack[s.top2++];
  return true;
}
复制代码

Guess you like

Origin juejin.im/post/7104274290408685581