#1 Two Sum——Top 100 Liked Questions

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

 

解:

"""

第一次:两遍遍历,外层循环从i到len(nums),内层循环从i+1到len(nums),如果下标i和下标j未出现过,且满足num[i]+num[j]=target,则保存i,j,在本地运行是可以的,但在LeetCode上显示运行超时

"""

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        result = []
        for i in range (len(nums)):
                for j in range (i + 1, len(nums)):
                    if i not in result and j not in result:
                        if nums[j] == target - nums[i]:
                            result.append(i)
                            result.append(j)
        return result

"""

Time Limit Exceeded

"""

"""

第二次:思路与第一次相同,加了一些break语句,时间上稍微提高一点,通过了,注意在判断时,先写不满足条件的语句,使其break。

"""

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        result = []
        for i in range (len(nums)):
            if i in result:
                break
            else:
                for j in range (i + 1, len(nums)):
                    if j in result:
                        break
                    else:
                        if nums[j] == target - nums[i]:
                            result.append(i)
                            result.append(j)
        return result

"""

Runtime: 6044 ms, faster than 5.99% of Python online submissions for Two Sum.

Memory Usage: 12.6 MB, less than 5.29% of Python online submissions for Two Sum.

"""

"""

第三次:遍历一遍nums,判断每个num与taget的差是否在nums中,且差的index不能与num的index相同,同时满足num的index与差的index都是第一次出现。

学会使用enumerate函数及list类型的extend函数

"""

 class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        result = []
        for index, num in enumerate(nums):
             if (target - num) in nums and index != nums.index(target - num) and index not in result and nums.index(target - num) not in result:
                result.extend((index, nums.index(target - num)))
        return result

"""

Runtime: 1908 ms, faster than 31.93% of Python online submissions for Two Sum.

Memory Usage: 12.5 MB, less than 5.56% of Python online submissions for Two Sum.

"""

 以上三个都可以实现nums中存在多组num,其和为target。

要实现在点击点赞按钮时,点赞数加1并且爱心图标变成红色,可以结合HTML、CSS和JavaScript来完成。下面是一个示例的代码: ```html <!DOCTYPE html> <html> <head> <style> .like-button { display: inline-block; padding: 5px 10px; border: 1px solid #ccc; border-radius: 5px; cursor: pointer; } .like-button .heart-icon { color: gray; transition: color 0.3s; } .like-button .like-count { margin-left: 5px; } .liked { color: red; } </style> </head> <body> <div class="like-button" onclick="likeButtonClicked()"> <span class="heart-icon">❤</span> <span class="like-count">0</span> </div> <script> var likeCount = 0; var likeButton = document.querySelector(".like-button"); var heartIcon = document.querySelector(".heart-icon"); var likeCountElement = document.querySelector(".like-count"); function likeButtonClicked() { likeCount++; likeCountElement.textContent = likeCount; // 添加/移除liked类来改变爱心图标颜色 heartIcon.classList.toggle("liked"); } </script> </body> </html> ``` 在上面的示例中,我们创建了一个点赞按钮,包含一个爱心图标和一个用于显示点赞数的文本。通过在JavaScript中监听按钮的点击事件,我们可以在点击时将点赞数加1,并通过添加/移除CSS类来改变爱心图标的颜色。 当你点击点赞按钮时,点赞数会增加,并且爱心图标会变成红色。再次点击按钮,点赞数会继续增加,并且爱心图标会恢复原来的颜色。你可以根据需要修改CSS样式和图标来适应你的项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值