博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters...
阅读量:6252 次
发布时间:2019-06-22

本文共 2821 字,大约阅读时间需要 9 分钟。

[Q1] 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]. Solution
class Solution(object):    def twoSum(self, nums, target):        """        :type nums: List[int]        :type target: int        :rtype: List[int]        """        idx = {}        for l,x in enumerate(nums):            if target - x in idx: return [idx[target-x], l] idx[x]=l

问题:可能存在两个数相同的情况,而index()函数对于重复元素只可返回第一个下标。

解决:使用字典

 

[Q2] 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 -> 8Explanation: 342 + 465 = 807. Solutions: https://leetcode.com/problems/add-two-numbers/discuss/1016/Clear-python-code-straight-forward
# Definition for singly-linked list.# class ListNode:#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution:    def addTwoNumbers(self, l1, l2):        """        :type l1: ListNode        :type l2: ListNode        :rtype: ListNode        """        carry = 0        l = l3 = ListNode(0)        while l1 or l2 or carry: cur1 = cur2 = 0 # avoid when l1 goes to end before l2 if l1: cur1 = l1.val # current value l1 = l1.next # go to next node if l2: cur2 = l2.val l2 = l2.next carry,cur3 = divmod(cur1+cur2+carry,10) l3.next = ListNode(cur3) l3 = l3.next return l.next

 

[Q3] 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: 1Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"Output: 3Explanation: 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. Solution:  if s[j]s[j] have a duplicate in the range [i, j)[i,j) with index j'j′, we don't need to increase ii little by little. We can skip all the elements in the range [i, j'][i,j′] and let ii to be j' + 1j′+1 directly. 创建左节点i和右节点j,扫描窗口,若s【i:j】内有与s【j】相同的数,且该数位置为j`,则直接将i跳至j`,而j+1
class Solution:    def lengthOfLongestSubstring(self, s):        """        :type s: str        :rtype: int        """        i,j = 0,1        maxL,L = 0,0    # window length        if(len(s)<=1):            return len(s)        while i

 

 

转载于:https://www.cnblogs.com/YunyiGuang/p/10340409.html

你可能感兴趣的文章
小米武汉总部开工雷军亲自出席:远期在汉要招上万人
查看>>
传贾跃亭将FF股份交给友人代持以规避失信人限制
查看>>
豆盟递交招股书:单季利润1394万 蓝标为第二大股东
查看>>
申小雨命案审理延期至3月5日 警方将翻译嫌犯口供
查看>>
第五届中欧文化艺术节开幕 谭盾“领衔”献艺
查看>>
财政部:2018年全国财政收入超18万亿元 同比增6.2%
查看>>
C罗失点 尤文图斯3:0切沃延续联赛不败纪录
查看>>
湖北整治清退非法码头 为长江“留白增绿”
查看>>
为什么要把网站升级到HTTPS
查看>>
【Hello CSS】序章-起源
查看>>
转行IT要趁早,多迪教育新就业数据告诉你真相
查看>>
JavaScript深入之参数按值传递
查看>>
Fragment总结
查看>>
Flutter进阶:深入探究 ListView 和 ScrollPhysics
查看>>
深入了解virtual dom
查看>>
spring事物应该注意的地方
查看>>
浅析 Vue 2.6 中的 nextTick 方法
查看>>
一篇文章搞懂闭包。
查看>>
结合实际场景谈一谈微服务配置
查看>>
我的前端面试总结(套路篇)
查看>>