leetcode做题记录

博主分享了在LeetCode上刷题的经验,包括IP地址转换、宝石与石头计数、BST节点范围求和等算法题目的解题思路,涉及Python编程和递归等技巧。并鼓励自己和读者坚持刷题,期待更多解题交流。
摘要由CSDN通过智能技术生成

给自己定下目标每天刷几道题,这周刚开始,还是有些吃力。 有的是很久以前刷过的,一边复习一边贴下自己的心得,勉强分享一下我的拙见。

  1. 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('.', '[.]')
  1. 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
  1. 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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值