LeetCode 代码记录

 我怕超的 - https://www.cnblogs.com/iAmSoScArEd/p/16153162.html

1. 两数之和

https://leetcode-cn.com/problems/two-sum/

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

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

 

执行用时:55 ms, 在所有 Java 提交中击败了24.73%的用户
内存消耗:41.3 MB, 在所有 Java 提交中击败了57.43%的用户
class Solution {
    public int[] twoSum(int[] nums, int target) {
        for(int i=0;i<nums.length;i++){
            for(int j=i+1;j<nums.length;j++){
                if(nums[i]+nums[j]==target){
                    return (new int[]{i,j});
                }
            }
        }
        return null;
    }
}
 

 2. 两数相加

https://leetcode-cn.com/problems/add-two-numbers/

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

 

执行用时: 48 ms
内存消耗: 13.1 MB
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def addTwoNumbers(self, l1, l2):
        resHead = ListNode()
        tempNode = resHead
        carry = 0
        while(l1 or l2):
            i = 0
            j = 0
            if l1:
                i = l1.val
                l1 = l1.next
            if l2:
                j = l2.val
                l2 = l2.next
            sum = i+j+carry
            carry = sum // 10
            tempNode.next = ListNode(sum%10)
            tempNode = tempNode.next
        if carry>0:
            tempNode.next=ListNode(1)
        return resHead.next

 

3. 无重复字符的最长子串

https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

 

执行用时: 4028 ms
内存消耗: 15 MB
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        resLen = 0
        if len(s)==1:
            return 1
        fL = []
        for n in range(len(s)):
            for i in range(n, len(s)):
                if s[i] not in fL:
                    fL.append(s[i])
                else:
                    if resLen<len(fL):
                        resLen = len(fL)
                    fL = []
                    break
        return resLen

 

执行用时: 36 ms
内存消耗: 13.9 MB
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        loc = {}
        res = 0
        start = -1
        for i in range(len(s)):
            if s[i] in loc:
                start = max(start,loc[s[i]])
            loc[s[i]] = i
            res = max(res,i-start)
        return res

 

4. 寻找两个正序数组的中位数

https://leetcode-cn.com/problems/median-of-two-sorted-arrays/comments/

给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。

算法的时间复杂度应该为 O(log (m+n)) 。

 

# 执行用时:40 ms, 在所有 Python 提交中击败了62.04%的用户
# 内存消耗:13.1 MB, 在所有 Python 提交中击败了60.04%的用户
class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        nums1.extend(nums2)
        nums1.sort()
        total = len(nums1)
        if total%2 == 0:
            h = total/2
            return float(nums1[h]+nums1[h-1])/2
        else:
            h = (total+1)/2
            return nums1[h-1]

 

posted @ 2022-04-16 16:02  我超怕的  阅读(74)  评论(0编辑  收藏  举报