周刷题第一期总结(two sum and two numbers)
由于深深的知道自己是事件驱动型的人,一直想补强自己的薄弱环节算法,却完全不知道从哪里入手。所以只能采用最笨的办法,刷题。从刷题中遇到问题就解决问题,最后可能多多少少也能提高一下自己的渣算法吧。
暂时的目标是一周最少两道,可能会多做多想,工作再忙也会完成这个最低目标。
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.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
这道题拿到手,感觉还算有点思路。
要做到O(n)的算法复杂度,肯定最多遍历一遍数组从这个思路入手,大概能想到记录数字的出现,以及用目标减去记录数字,同时拿到其索引的思路
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
look_up = {}
for i, num in enumerate(nums):
if target-num in look_up:
return [look_up[target-num], i]
look_up[num] = i
return
造一个字典,用这个字典的key记录出现的数字,用value记录其出现的索引位置。然后使用target - num 用目标数字减去当前遍历到的数字的结果有没有在look_up里面有key记录,如果有读取其索引 然后 带上现在遍历到的数字索引返回。
Add Two Numbers:
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
这道题首先我读了半天题才理解到题意。要弄出这道题首先你要知道python里面链表一般如何实现,所以就需要去补充一些链表的知识(事件驱动学习就是这个意思)。查找了一些资料之后理解了链表再来看这道题就明白了
首先实现一个ListNode
class Solution(object):
ListNode(self, val):
self.val = val
self.next = None
每当我们新开一个链表,就相当于又多创建一个类似ListNode结构的object。self.next存储的是下一个链表的地址。
这道题其实考察的是使用链表进行大数相加,由于从个位加起,所以是反转存储的方便进位。下面看代码
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
dummy, carry = ListNode(0), 0
current = dummy while l1 or l2:
val = carry
if l1:
val += l1.val
l1 = l1.next
if l2:
val += l2.val
l2 = l2.next
carry, val = val / 10, val % 10
current.next = ListNode(val)
current = current.next if carry == 1:
current.next = ListNode(1) return dummy.next
carry 代表进位的意思,dummy是初始化的存储结果链表中的头部指针,赋给current同样地址,但是current会在随后的操作中被往后移动。
其余的都很简单了。
第一次开始刷题,感觉不是特别习惯,也没有相关的思维。 希望万事开头难经后可以熟练起来。
周刷题第一期总结(two sum and two numbers)的更多相关文章
- 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)
这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...
- python在leecode刷题-第一题和第七题
class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] num ...
- 牛客SQL刷题第一趴——非技术入门基础篇
user_profile表: id device_id gender age university province 1 2138 male 21 北京大学 Beijing 2 3214 male ...
- LeetCode刷题第一天
1 . 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用 ...
- leetcode 刷题之路 66 Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [刷题] 416 Partition Equal Subset Sum
要求 非空数组的所有数字都是正整数,是否可以将这个数组的元素分成两部分,使得每部分的数字和相等 最多200个数字,每个数字最大为100 示例 [1,5,11,5],返回 true [1,2,3,5], ...
- [刷题] 209 Minimum Size Subarray Sum
要求 给定一个含有 n 个正整数的数组和一个正整数 s 找出该数组中满足其和 ≥ s 的长度最小的连续子数组 如果不存在符合条件的连续子数组,返回 0 示例 输入:s = 7, nums = [2,3 ...
- 【刷题-LeetCode】307. Range Sum Query - Mutable
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- 【刷题-LeetCode】304. Range Sum Query 2D - Immutable
Range Sum Query 2D - Immutable Given a 2D matrix matrix, find the sum of the elements inside the rec ...
随机推荐
- MetaMask/sw-controller
https://github.com/MetaMask/sw-controller Service Worker Controller Used to register a service worke ...
- tomcat运行超时问题解决
之前一直懒得记录,不过最近觉得还是记录一下好一些. 由于项目过于庞大启动时间比较长,而tomcat默认超时时间为45秒,很显然对于一个庞大的项目而言是远远不够的. 错误信息如下所示: Server T ...
- 谈谈maven多模块
记得在校的时候,通常用的比较多是动态web工程,动态web工程导入最多就是jar包,这些jar包需要自己手动复制粘贴放入lib目录下,然后build path,有的IDE会自动build path有的 ...
- 个人向 - vscode插件记录
现在用的编译器的是vscode,本身这个编译器很小,很多功能都没有,需要自己下载一些插件来完善功能,不知不觉下载的插件也有三十多个了,感觉需要记录一下. tips:1. vscode插件的安装位置:’ ...
- mysql编码问题:
在my.ini文件改为: [client]default-character-set = utf8port=3306 [mysql] default-character-set=utf8 [mysql ...
- treap学习笔记
treap是个很神奇的数据结构. 给你一个问题,你可以解决它吗? 这个问题需要treap这个数据结构. 众所周知,二叉查找树的查找效率低的原因是不平衡,而我们又不希望用各种奇奇怪怪的旋转来使它平衡,那 ...
- mysql分表操作
一般分表操作有垂直拆分和水平拆分.顾名思义. 1. 垂直拆分是指,这个表的列,即字段,要拆分成两个或多个表. 这个应用场景比如:这个表字段,几个都是int.datetime等,有那么一个是text类 ...
- ASP.NET MVC学习笔记(一) 从路由开始创建mvc
之前一篇写一半发现版本太老了,是基于mvc2的. 两本参考书编写的顺序各方面都不太一样.决定重新写一篇. 我这篇文章基于mvc5,vs2015 参考书:Will保哥的ASP.NET MVC4开发指南 ...
- 【vue】chrome已安装Vue Devtools在控制台却无显示
chrome已安装Vue Devtools在控制台却无显示的解决: 在点亮Vue Devtools图标后,控制台没有vue解读显示. 原因:脚手架配置NODE_ENV直接定义为了production版 ...
- eclipse中文字体大小修改
貌似有不少人苦恼eclipse中文字体大小修改问题,默认的eclipse中文字体很小,和英文字体大小完全不在一个调子上,因为默认的eclipse juno中英文字体是Consolas,字体大小是10, ...