美文网首页
LeetCode | 算法:顺序表求和、单链表相加

LeetCode | 算法:顺序表求和、单链表相加

作者: 松鼠的读书笔记 | 来源:发表于2019-01-04 11:42 被阅读24次

1. Two Sum

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].
笔记:
Tips: 空间换时间,使用哈希表记录(number,index)以加速查找
Tricks: 一边循环一边建哈希表,进一步降低时间复杂度

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        hash_table = {}
        for i,num in enumerate(nums):
            if (target-num) in hash_table:
                return [hash_table[target-num], i]
            hash_table[num] = i

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.
笔记:
(1)创建带头结点的单链表,使得后续的处理可以统一起来;
(2)从头到尾循环一遍即可,关键在于进位传递。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        r = l3 = ListNode(0)  # 初始化求和链表的头结点
        
        p = l1                # 初始化工作指针
        q = l2
        
        carry = 0             #记录是否产生进位
        
        while p or q or carry:
            v1 = v2 = 0
            if p:
                v1 = p.val
                p = p.next
            if q:
                v2 = q.val
                q = q.next
            v3 = (v1 + v2 + carry) % 10
            carry = (v1 + v2 + carry) // 10

            r.next = ListNode(v3)
            r = r.next
            
        return l3.next
 

相关文章

  • LeetCode | 算法:顺序表求和、单链表相加

    1. Two Sum Given an array of integers, return indices of ...

  • 算法学习——线性表

    线性表分为:顺序表和链表 1.腾讯面试题(快速找到未知长度单链表的中间节点?) 普通算法:先遍历一个遍单链表以确定...

  • 线性表之顺序表和链表(单/双链表,单/双循环链表)

    线性表按存储方式分为顺序表和链表两种,其中链表又分为单链表,循环链表,双链表,双向循环链表。 顺序表 顺序表采用顺...

  • 线性表总结

    线性表总结 顺序表和链表的定义 链表的结构解析 顺序表类型定义 例 单链表的存储结构定义 例 链表的结构解析 单链...

  • 线性表

    1.线性表 1.1 顺序表(顺序存储) 静态分配 动态分配 1.2 单链表 单链表定义 不带头节点的单链表插入操作...

  • 顺序表与单链表

    接口 顺序表(线性表)实现方式 单链表的节点 单链表的实现

  • 数据与算法结构

    线性表 顺序表 链表(物理上离散,逻辑上连续) 链表的类别 单链表 循环链表 双链表 链表的操作 顺序表与链表的比...

  • 数据结构错题收录(一)

    1、以下属于逻辑结构的是() A:顺序表 B:哈希表 C:有序表 D:单链表 解析 顺序表、哈希表和单链表是三种不...

  • 线性表:顺序表和链表

    顺序表(数组)优缺点 链表优点 单链表使用 单链表结构 单链表初始化 单链表初始化 单链表建立: 头插法 尾插法 ...

  • 总结

    Android篇 数据结构与算法顺序表 - ArrayList源码链表 - 单向链表、双向链表 - LinkedL...

网友评论

      本文标题:LeetCode | 算法:顺序表求和、单链表相加

      本文链接:https://www.haomeiwen.com/subject/jffhrqtx.html