Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better methods, lower time complexity or space complexity #55

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Dylanmxh
Copy link

@Dylanmxh Dylanmxh commented Nov 4, 2024

Question 8:

  1. By directly using the mathematical formula sum = (n * (n + 1) * (n + 2)) / 6 instead of a loop, the time complexity is reduced to O(1).

Question 59:

  1. Create a linked list node using a loop and check whether the memory allocation is successful.
  2. Perform remainder processing on the step size and handle special cases.
  3. Delete the node and free the memory.
  4. Recursively call itself.
  5. Add input validation.

Question 195:

  1. std::stack is used to directly replace the custom sequential stack structure and all related operations.
  2. Using the standard library container std::stack simplifies stack operations, eliminating the need to manually manage stack initialization, determining whether it is empty or full, pushing and popping operations, and so on.
  3. Simplified the logic of loops and conditional judgments.
  4. The use of exception handling mechanisms enhances the robustness of the program.

Topic 212:

  1. randomize(); Replace it with srand(time(0));, Ensure that different random numbers are generated each time it runs.
  2. Using rand() % 4 to generate a random number between 0 and 3 simplifies the selection of a random direction.
  3. In the drawman function, the various parts of the little man are explicitly drawn and colored.
  4. When the villain moves, clear the old position before drawing the new position to avoid residual images.
  5. Comments have been added to each function and key code segment to explain the function and logic of the code, making it easier to understand and maintain.

题目8:

  1. 通过直接使用数学公式 sum = (n * (n + 1) * (n + 2)) / 6 代替循环,减少了时间复杂度,降到了 O(1)。

题目59:

  1. 使用循环创建链表节点,并检查内存分配是否成功。
  2. 对步长进行求余处理,并处理特殊情况。
  3. 删除节点并释放内存。
  4. 递归调用自身。
  5. 增加输入验证。

题目195:

  1. 使用了std::stack,直接替换了自定义的顺序栈结构和所有相关操作。
  2. 使用标准库容器std::stack简化了栈的操作,不再需要手动管理栈的初始化、判断是否为空或满、压栈、弹栈等操作。
  3. 简化了循环和条件判断逻辑。
  4. 使用异常处理机制增强了程序的健壮性。

题目212:

  1. 将 randomize(); 替换为 srand(time(0));,确保每次运行时生成不同的随机数。
  2. 使用 rand() % 4 生成0到3之间的随机数,简化了随机方向的选择。
  3. 在 drawman 函数中,明确地绘制了小人的各个部分,并设置了颜色。
  4. 在小人移动时,先清除旧位置再绘制新位置,避免残留图像。
  5. 在每个函数和关键代码段添加了注释,解释了代码的功能和逻辑,便于理解和维护。

shan-zeng-xin and others added 8 commits October 20, 2024 09:37
…d() % 4 生成0到3之间的随机数,简化了随机方向的选择。(3)在 drawman 函数中,明确地绘制了小人的各个部分,并设置了颜色。(4)在小人移动时,先清除旧位置再绘制新位置,避免残留图像。(5)在每个函数和关键代码段添加了注释,解释了代码的功能和逻辑,便于理解和维护
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants