LeetCode 做题记录

声明:

  1. 此文档只做自我的LeetCode记录,并无其他想法。
  2. 答案代码全为Python 3 版本解答,其他语言目前不做解答。
  3. 如需沟通,请发邮件、进群或者直接评论。

记录如下:

1、Two Sum

描述:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

思路:

遍历nums,然后在nums中找到Target减去nmus[i]的值,并且这个在nums中的位置不能和i相同。

答案:

参考资料:资料1

172、Factorial Trailing Zeroes

描述:

Given an integer n, return the number of trailing zeroes in n!.

Example 1:

Example 2:

Note: Your solution should be in logarithmic time complexity.

思路:
阶乘查看末尾为零个数,实际就是看有多少个5*2,因为末尾零的个数是指有多少个10相乘,阶乘中的1-9中能够拆为2和5 的有5和2的倍数,显然2个数要比5多,此时我们只要找到有多少个5,就可以知道有多少个末尾零。
答案:

参考资料: 资料1

771. Jewels and Stones

描述:

You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in Sis a type of stone you have.  You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

Example 1:

Example 2:

Note:

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.
思路:
从S字符串里循环取值去和J字符串里的值对比。
答案:

看到一个是这样的:
描述:

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

Example 1:

Example 2:

Example 3:

思路:

python 3 中的Str 自带有一个把大写字母转换为小写字母的内置方法:str.lower()

答案:


905. Sort Array By Parity

描述:

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

You may return any answer array that satisfies this condition.

Example 1:

Note:

  1. 1 <= A.length <= 5000
  2. 0 <= A[i] <= 5000

思路:

奇偶校验主要看是否能够被2整除,python 中使用 % 进行求余。

答案:


461. Hamming Distance

描述:

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ xy < 231.

Example:

思路:

这道题的思路:
把每个数,转化为二进制,然后比较对应位是否一致,统计不一致的数量,就是return的结果。

此答案的妙处:
1、python 中的运算符:^,解释:按位异或运算符:当两对应的二进位相异时,结果为1 。
通过^直接做二级制转换和对位比较。
2、bin()方法的结果是str()类型,str类型中的内置count()方法,统计其中的“1”,来得到想要的结果。

难点:

1、得找到一个好的方式来比较二级制的位数和对应位的值是否一致

2、通过自己的循环方式不够简练

运算符^ 正好解决了以上的所有问题。

答案:


657. Robot Return to Origin

描述:

There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0)after it completes its moves.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

Example 1:

Example 2:

思路:

机器人能否回到原味,主要判断相反的动作是否能够互相抵消。

通过str()的cout()内置方法,统计各个移动操作:U、D、L、R,如果相反操作可以抵消,True,反之,False。

答案:


929

思路:

第一步:通过“@”拆分邮箱地址,分为local和domain,domain是唯一的,不允许操作

第二步:获取“+”之前的local地址,因为地址在不记录“+”之后的内容

第三步:去除掉local中的“.”,因为带不带“.”都被认作一个邮箱地址

第四步:把经过“+”和“.”处理的local和“”@和domain合并起来

最后,通过for循环得到一个set集合,输出len(set())得到数量

参考答案:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Young文人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值