Leetcode问题的Python技巧

背景 (Background)

While practising Leetcode questions with Python, I came across some tips and decided to share them.

在使用Python练习Leetcode问题时,我遇到了一些技巧,并决定分享它们。

遍历数组/字符串 (Iterating through arrays/strings)

Say we have an array of numbers, called nums.

假设我们有一个数字数组,称为nums

Normally when we iterate through an array or a string, we do this: range(0, len(nums))

通常,当我们遍历数组或字符串时,我们这样做: range(0, len(nums))

The first parameter is 0 by default, so we can dorange(len(nums)) instead.

默认情况下,第一个参数为0,因此我们可以改为使用range(len(nums))

nums = [1,2,3,4,5]


for i in range(len(nums)):
  print(i)

反转顺序 (Reversing a sequence)

What if we want to count down, say from 7 to 0?

如果我们想倒数从7倒数到0,该怎么办?

From Python’s documentation:range(start, stop, [,step]) produces a sequence from start (inclusive) to stop(exclusive). The optionalstep parameter determines the difference between two adjacent terms in the sequence.

根据Python的文档: range(start, stop, [,step])产生一个从start (包括)到stop (不包括)的序列。 可选的step参数确定序列中两个相邻项之间的差。

The start parameter of range can be larger than stop , so we can do something like range(7, 0) . But since range() creates a sequence from start (inclusive) to stop (exclusive), the stop parameter should be -1 so the last term of the sequence is 0.

range的start参数可以大于stop ,因此我们可以执行range(7, 0) 。 但由于range()创建了一个从序列start (含)至stop (不含),将stop参数应为-1,因此序列的最后期限为0。

And since we are creating a list in descending order, the step parameter supplied to it should be -1.

并且由于我们是按降序创建列表,因此提供给它的step参数应该为-1。

Therefore we have the following:

因此,我们有以下内容:

# Want to get a sequence from 7 to 0


for i in range(7, -1, -1):
  print(i)
  
# alternative way of getting a sequence from 7 to 0
for i in reversed(range(0, 8)):
  print(i)

For a more intuitive way to create a sequence from 7 to 0, we can do the following: reversed(range(0, 8)) which first creates a sequence from 0 to 7, then reverses the sequence.

对于创建从7到0的序列的更直观方法,我们可以执行以下操作: reversed(range(0, 8))首先创建从0到7的序列,然后反转该序列。

在同一行中一起声明变量 (Declaring variables together in the same line)

Generally, it is good practice to only declare a variable when we are about to use it.

通常,优良作法是仅在我们要使用变量时声明它。

But if two variables are related, it’s handy to declare them in the same line. Take for example, row and col . In that case, we can declare them in the same line like this:

但是,如果两个变量相关联,则在同一行中声明它们很方便。 以rowcol为例。 在这种情况下,我们可以像这样在同一行中声明它们:

row, col = 0, 0


print(row) # 0
print(col) # 0

This initialises both row and col to 0.

这会将rowcol都初始化为0。

初始化最大和最小数量 (Initialising largest and smallest number)

To initialise the smallest number, we can do: float('-inf')to get negative infinity, and float('inf') to represent infinity, the largest number.

要初始化最小数字,我们可以做: float('-inf')得到负无穷大,而float('inf')代表最大无穷大。

largest = float('inf')


smallest = float('-inf')

结论 (Conclusion)

I hope you learnt something from these tips that you could apply to your next technical interview or coding work.

我希望您从这些技巧中学到了一些知识,可以应用于下一次技术面试或编码工作。

翻译自: https://medium.com/python-in-plain-english/python-tips-for-leetcode-questions-bfacacea9971

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值