给自己定下目标每天刷几道题,这周刚开始,还是有些吃力。 有的是很久以前刷过的,一边复习一边贴下自己的心得,勉强分享一下我的拙见。
- Given a valid (IPv4) IP address, return a defanged version of that IP address.
Input: address = “1.1.1.1”
Output: “1[.]1[.]1[.]1”
这个题很简单,直接把 . 换成 [.] 就好了,所以直接用python 的replace就好。
return address.replace('.', '[.]')
- You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is 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”.
Input: J = “aA”, S = “aAAbbbb”
Output: 3
这个也算是比较基础的题,也就是说只要J里面有的值如果S里面也有,那么答案+1 就好了。所以我遍历了J,然后如果遍历的值在S中,里面count 这个值出现的次数,然后加入,得到答案。
def numJewelsInStones(self, J: str, S: str) -> int:
result = 0
for j in J:
if j in S:
result += S.count(j)
return result
- Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).
The binary search tree is guaranteed to have unique values.
这个题实际考察了遍历或者递归(recursion)