Technology Sharing

July 11 study check-in, data structure stack

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Hello everyone, the purpose of this blog is to record the summer vacation learning check-in, which will be organized into a column later. I mainly plan to finish learning data structure in the summer vacation, so I will post some blogs related to data structure implementation and some practice questions. I will use it for personal study and hope that everyone will support me. Please point out any shortcomings. Thank you.

1. Force buckle 115, minimum stack

. - LeetCode

Simply use an array to simulate a stack, but I am trying to save trouble here so the running speed is not very high, and there is a lot of room for optimization.

  1. class MinStack {
  2. int[] el;
  3. int numsize;
  4. public MinStack() {
  5. el = new int[10000];
  6. int numsize = 0;
  7. }
  8. // private void grow(){
  9. // this.el=Arrays.copyof(el,2*el.lenth);
  10. // }
  11. public void push(int val) {
  12. // if (el.lenth == numsize) {
  13. // grow();
  14. // }
  15. el[numsize] = val;
  16. numsize++;
  17. }
  18. public int pop() {
  19. if (empty())
  20. return -1;
  21. return el[--numsize];
  22. }
  23. public int top() {
  24. return el[numsize - 1];
  25. }
  26. private boolean empty() {
  27. return numsize == 0;
  28. }
  29. public int getMin() {
  30. int num = el[0];
  31. for (int i = 0; i < numsize; i++) {
  32. if (el[i] < num)
  33. num = el[i];
  34. }
  35. return num;
  36. }
  37. }
  38. /**
  39. * Your MinStack object will be instantiated and called as such:
  40. * MinStack obj = new MinStack();
  41. * obj.push(val);
  42. * obj.pop();
  43. * int param_3 = obj.top();
  44. * int param_4 = obj.getMin();
  45. */

2. Pascal's Triangle

. - LeetCode

Analysis: In order to use the sequence table learned before, we use the sequence table to solve this problem. Use the sequence table to simulate a two-dimensional array. NoteThe two-dimensional array simulated by the sequence list cannot simply access elements by subscript

  1. class Solution {
  2. public List<List<Integer>> generate(int numRows) {
  3. List<List<Integer>> ret=new ArrayList<>();
  4. List<Integer> list=new ArrayList<>();
  5. list.add(1);
  6. ret.add(list);
  7. for(int i=1;i<numRows;i++){
  8. List<Integer> row=new ArrayList<>();
  9. row.add(1);
  10. List<Integer> a= ret.get(i-1);
  11. for (int j = 1; j < i; j++) {
  12. int val1=a.get(j);
  13. int val2=a.get(j-1);
  14. row.add(val1+val2);
  15. }
  16. row.add(1);
  17. ret.add(row);
  18. }
  19. return ret;
  20. }
  21. }

3. 150-degree reverse Polish notation evaluation

Note: Polish notation is a formula that can be understood by computers.

. - LeetCode

Idea: Traverse the array and first determine whether the string is a number.If it is a number, convert it into a number and push it into the stack. Otherwise, take out two operands and take out the one after the "+", "-", "*" or "/" after the operator is used., the final answer is what is left in the stack.

  1. class Solution {
  2. public int evalRPN(String[] tokens) {
  3. Stack<Integer> st = new Stack();
  4. for (int i = 0; i < tokens.length; i++) {
  5. String str = tokens[i];
  6. if (o(str) == false) {
  7. int val = Integer.parseInt(str);
  8. st.push(val);
  9. } else {
  10. int val1 = st.pop();
  11. int val2 = st.pop();
  12. switch (str) {
  13. case "+":
  14. st.push(val2+val1);
  15. break;
  16. case "-":
  17. st.push(val2-val1);
  18. break;
  19. case "*":
  20. st.push(val2*val1);
  21. break;
  22. case "/":
  23. st.push(val2/val1);
  24. break;
  25. }
  26. }
  27. }
  28. return st.peek();
  29. }
  30. private boolean o (String s){
  31. if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
  32. return true;
  33. }
  34. return false;
  35. }
  36. }

This is the end of this blog, thank you everyone