leetcode top 100 liked questions

1.Two Sum --Easy

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:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums)):
            add_one=target-nums[i] # temp+nums[i]=target
            if nums.count(add_one) !=0: #判断temp 是否存在
                add_one_index=nums.index(add_one)
                if add_one_index!=i: # 判断是否是同一个数字同一个位置,例如6=3+3
                    return [i,add_one_index]

2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
class ListNode:
    ''' __init__ 创建实例时,将一些属性绑定,第一个参数self,表示创建的实例本身,
    在创建实例的时候,需要把x等属性绑定上去,不能传入空的参数,必须传入与__init__方法
    匹配的参数,但是self不需要传
    '''
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        carry=0 # carry 表示相加的进位
        phead=result=ListNode(0)
        while l1!=None or l2!=None:
            # l1 或 l2 确定加数add_one add_two
            if l1!=None:
                add_one=l1.val
            else:
                add_one=0
            if l2!=None:
                add_two=l2.val
            else:
                add_two=0

            temp=add_one+add_two+carry

            if temp>=10:
                carry=1
                sum=temp%10
            else:
                sum=temp
                carry=0

            temp_node=ListNode(sum)
            result.next=temp_node
            result=result.next

            if l1!=None:
                l1=l1.next
            if l2!=None:
                l2=l2.next

        # 最后一位的进位
        if carry>0:
            temp_node=ListNode(carry)
            result.next=temp_node

        return phead.next

references:

class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
    carry = 0
    root = n = ListNode(0)
    while l1 or l2 or carry:
        v1 = v2 = 0
        if l1:
            v1 = l1.val
            l1 = l1.next
        if l2:
            v2 = l2.val
            l2 = l2.next
        #divmod(a,b) 返回一个包含商和余数的元组(a//b,a%b)
        carry, val = divmod(v1+v2+carry, 10)
        n.next = ListNode(val)
        n = n.next
    return root.next
def addTwoNumbers(self, l1, l2):
    dummy = cur = ListNode(0)
    carry = 0
    while l1 or l2 or carry:
        if l1:
            carry += l1.val
            l1 = l1.next
        if l2:
            carry += l2.val
            l2 = l2.next
        cur.next = ListNode(carry%10)
        cur = cur.next
        carry //= 10
    return dummy.next

3.Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3. 

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. 
             Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

references:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        start=maxLength=0
        usedChar={} # 字典
        
        for i in range(len(s)): # 遍历
            if s[i] in usedChar and start<=usedChar[s[i]]: # 当前字符s[i]出现过,并且当前字符s[i]出现过的位置在start之前
                start=usedChar[s[i]]+1 # start位置移动到字典中出现s[i]字符后移一位
            else:
                maxLength=max(maxLength,i-start+1) 
                
            usedChar[s[i]]=i # 建立并更新字典 
            
        return maxLength

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值