-
Notifications
You must be signed in to change notification settings - Fork 12.2k
python代码更新,多题提交 #625
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
Merged
Merged
python代码更新,多题提交 #625
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
使用更符合PEP8要求的代码规范来约束
另一种解法进行了不必要的操作,且代码可读性较差。 推荐使用第一种解法
维持原方法的思路,简化了包和匿名函数的用法 原代码使用了reduce包 + 匿名函数来实现 ''.join的操作,去除该部分提升运行效率
减少不必要的变量使用,优化空间复杂度,使用切片替换而不是单个替换
如果不让使用自带函数reversed() 可以使用该方法
python代码简化,符合PEP8标准,可读性加强,效率提升
原方法用了很多不必要的变量和操作,改进后的代码更直观,效率和空间都得到了优化
时间复杂度错误,修正
补充了题主的方法,改写了原先冗余的做法,可读性提升,PEP8标准
补充使用双指针的方法,但是更推荐栈的做法
使用变量名使交换的过程更加清晰,而不是使用之前的next.next这样可读性较差 从后往前换 pre = dummy,cur = 2, post=3 dummpy -> 1 -> 2 -> None 先把cur.next = post.next,此时链表为 1 -> None �再 post.next = cur 此时链表为 2 -> 1 -> None, dummpy -> 1 -> None 最后 pre.next = post, 此时为 dummpy -> 2 -> 1 -> None
添加限制条件,仅在栈不为空的情况下才允许后续pop()和peek()的操作
所有代码经过测试均已通过。 目前看到的python代码主要有几个问题:
def example():
s = 'a'
return s #完全可以直接return 'a'
from functools import reduce
def exmaple():
lst = ['a', 'b', 'c']
return reduce(lambda x, y: x + y) #其目的和''.join(lst)是一样的
很荣幸能在知乎搜到这个项目,一边学习一边能帮仓主完善python代码 |
用format string提升效率,增加可读性,避免使用索引访问,直接使用切片。
感谢支持,优化的很不错👍 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
可能包含了多道题目python代码的更新