LeetCode前100题(EASY难度)
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, return [0, 1].
Approach 1: Brute Force
- 两个循环,两两相加
- TC:O(n^2) SC:O(1)
Approach 2: Two-pass Hash Table
- 引入哈希表,用空间换时间(利用了哈希表判断key是否存在的复杂度为O(1)的特性)
- 第一次遍历建表(key为num[i], value为i),第二次遍历判断target-num[i]的key是否存在(注意要加一个判断来排除自己加自己等于target的情况)
- TC:O(n) SC:O(n)
Approach 3: One-pass Hash Table
- 在第一次遍历建表时,就判断目标元素是否存在,从而进一步压缩算法用时。
7 Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example:
Input: 123, Output: 321
Input: -123, Output: -321
Input: 120, Output: 21
Note: the 32-bit signed integer range: [−2^31, 2^31 − 1]. Returns 0 when the reversed integer overflows.
Approach:
- 解题思路与翻转字符串类似:不断地取出input的最后一位数,把它装到另一个数中
- 由于整数的特殊性,因此我们可以用数学方法代替辅助的栈/数组
- input先对10取余,然后整除10,这样我们就取出了最后一个数
- rev先乘以10,再加上从input中取出的数,就完成了这位数的装入
- 注意,在rev乘10之前,要先对溢出做判断,比较它与溢出限除以10的大小,如果二者相等,则需要对尾数(装入的那位数)做判断
- 正负上限分开判别:
if (rev > INT_MAX/10 || (rev == INT_MAX / 10 && pop > 7))
if (rev < INT_MIN/10 || (rev == INT_MIN / 10 && pop < -8))
- 正负上限分开判别:
- TC: O(log(n)), SC:O(1)
- 时间复杂度与数据的位数线性相关,因此近似lgn,对应O(log(n))
9 Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example: 121: true, -121: false
Approach:
- python赖皮解法:
return str(x) == str(x)[::-1]
- 如果不用字符串的话,可以像第7题一样用数学方法翻转数字然后比较,不过这种方法无法处理大数溢出的问题
- 最好的解法是:只翻转一半的整数,然后和另一半比较(翻转方法与第7题类似)
- TC:O(lgn), SC(O(1))
13 Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
(I-1, V-5, X-10, L-50, C-100, D-500, M-1000)
Note that I can be placed before V (5) and X (10) to make 4 and 9. The same as 40, 90, 400, 900. etc.
Approach:
- 从左往右判断,如果左边的字符对应的数大于或等于右边的,就把二者加起来,如果右边的大于左边的,就把左边的减去
- python解法:
dic = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
res = 0
for i in range(len(s)-1):
if dic[s[i]] < dic[s[i+1]]:
res = res - dic[s[i]]
else:
res = res + dic[s[i]]
return res + dic[s[-1]] # 负数索引就是从右往左数
14 Longest Common Prefix(前缀)
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string ""
All given inputs are in lowercase letters a-z.
Example: ["flower","flow","flight"] -> "fl", ["dog","racecar","car"] -> ""
Approach 1: Horizontal scanning
- 先找出前两个字符串的LCP,然后依次把得到的LCP与下一个字符串比较,得到新的LCP,直到比较完所有字符串或LCP为空
- LCP(S1…Sn)=LCP(LCP(LCP(S1,S2),S3),…Sn)
- TC: O(S), SC: O(1)
- 这里的S指所有字符串的字符数之和
Approach 2: Vertical scanning
- 竖着来,先比较所有字符串的第一位,再比较第二位,直到比完或出现差异
- TC: O(S), SC: O(1)
Approach 3: Divide and conquer(分治法)
- 利用这个性质:LCP(S1…Sn)=LCP(LCP(S1…Sk),LCP(Sk+1…Sn)),把问题不断向下分解成小问题,最后再合并结果
- TC: O(S), SC: O(mlogn)
Approach 4: Binary search
- 先找到最短的字符串,然后把它分成前后两半,进行二分前缀匹配
- TC: O(S⋅logn), SC: O(1)
20 Valid Parentheses(括号)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets(括号).
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example: "()"->true, "()[]{}"->true, "(]"->false, "([)]"->false, "{[]}"->true
Approach:
用一个栈就可以轻松实现
def isValid(s: str) -> bool:
symble = {" ":0, "(":1, "[":2, "{":3, ")":-1, "]":-2, "}":-3}
stack = 0
for i in s:
if symble[i] > 0:
stack = stack*10 + symble[i]
print(stack)
if symble[i] < 0:
stack += symble[i]
stack /= 10
print(stack)
if stack == 0:
return True
else:
return False
print(isValid(" ([( )[] ])"))
21 Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new linked list. The new list should be made by splicing(插接) together the nodes of the first two lists.
Example: Input: 1->2->4, 1->3->4, Output: 1->1->2->3->4->4
Approach:
def mergeTwoLists1(self, l1, l2):
dummy = cur = ListNode(0)
while l1 and l2:
if l1.val < l2.val:
cur.next = l1
l1 = l1.next
else:
cur.next = l2
l2 = l2.next
cur = cur.next
cur.next = l1 or l2
return dummy.next
26 Remove Duplicates from Sorted Array
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example:
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the returned length.
Approach:
- 双指针,开始时i在j前面
- 如果num[j]==num[i],那么j前进一格
- 如果num[j] != num[i],
- 那么i前进一格,并把j对应的值赋值到num[i]
27 Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Approach:
- 双指针i,j,j在i后面
- 这时会有四种可能的情况:
- 如果j!=val且i==val,那么把j赋值给i,然后i,j各前进一格
- 如果jval且ival,那么j前进一格
- 如果j!=val且i!= val, 那么i,j各前进一格
- 如果j==val且i!=val,那么i,j各前进一格
优化:
- 双指针,ij初始时都指向同一个元素:
- 如果j!=val,那么ij各前进一格
- 如果j==val,那么j前进一格
28 Implement strStr()
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example:
Input: haystack = "hello", needle = "ll"
Output: 2
Approach:
- 双指针ij,i慢j快:
- 如果j等于needle[k] (k默认为0),那么j前进,k前进
- 如果j不等于needle[k],那么i前进,j=i,k=0
35 Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
Example:
Input: [1,3,5,6], 5
Output: 2
Input: [1,3,5,6], 2
Output: 1
Approach:
- 遍历数组,判断:
- 如果i==val,输出i
- 如果i<val<=i+1,输出i+1
- 如果遍历完了还没找到,那么输出length(arr)
38 Count and Say
题目没看懂。。。LeetCode-Count and Say
53 Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Approach
def maxSubArray(self, nums: List[int]) -> int:
globalsum,localsum=0,0
if max(nums)<0:
return max(nums)
for i in nums:
localsum=max(0,localsum+i)
globalsum=max(localsum,globalsum)
return globalsum
Go over the array from left to right. For each element (with index n) of the array calculate the maximum contiguous subarray sum, with the condition that the contiguous subarray contains this element n. This can be done using DP approach by keeping track of only one number "prev". When moving from index n to index n+1 we decide on whether to include the "prev" we calculated for n, or just take the single n+1th element. This simply depends on whether "prev" for n was negative or not. If it was negative, we don't want to append the associated array to n+1'st element. In which case the best "prev" for the subarray from index 0 ending and including n+1'st element, is prev=n+1 (corresponding to array of just one element, the n+1'st one). If "prev" for n was positive, we would want to append it to n+1'st element.
While doing this forward pass also try to save the largest prev in the r, which then will be returned.
class Solution(object):
def maxSubArray(self, nums):
if len(nums) == 0:
return 0
prev = nums[0]
r = nums[0]
for n in nums[1:]:
if prev > 0:
prev += n
else:
prev = n
if prev > r:
r = prev
return r
LeetCode前100题(EASY难度)的更多相关文章
- bzoj 前100题计划
bzoj前100题计划 xz布置的巨大的坑.. 有空填题解... 1002 轮状病毒 用python手动matrixtree打表. #include<bits/stdc++.h> #def ...
- 两个月刷完Leetcode前400题经验总结
更新:气死了,挂个傻逼: 每次做个分享.组织个活动,就会有一些傻逼冒泡生怕别人不知道他是傻逼,气死我了!自己好好看看非法集资的概念,我办这个活动,一分钱都没收,入群99元是督促大家完成刷题任务,最后完 ...
- 【总结】 BZOJ前100题总结
前言 最近发现自己trl,所以要多做题目但是Tham布置的题目一道都不会,只能来写BZOJ HA(蛤)OI 1041 复数可以分解成两个点,所以直接把\(R^2\)质因数分解一下就可以了,注意计算每一 ...
- LeetCode Top 100 Liked 点赞最高的 100 道算法题
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:刷题顺序,刷题路径,好题,top100,怎么刷题,Leet ...
- LeetCode面试常见100题( TOP 100 Liked Questions)
LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- Leetcode重点 250题-前400 题
删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
随机推荐
- SDOI2019R1游记
差点退役,真是开心 Day -2 吐了一晚上,差点死掉 被拉去医院打针,结果蛇皮的被扎了两针,真是好疼啊嘤嘤嘤 决定第二天在家里咕一天 Day -1 结果在家里也得做\(loli\)昨天的不知道从哪里 ...
- JS正则四个反斜杠的含义
我们首先来看如下代码,在浏览器中输出的是什么? // 在浏览器中输出的 console.log('\\'); // 输出 \ console.log('\\\\'); // 输出 \\ 一:js正则直 ...
- [Oracle]Sqlplus 中使用 new_value
通过再sqlplus 中使用 new_value,可以把从表中查询出来的值,放置到 变量中.然后使用变量时,类似与宏定义一样,就可以像使用表中字段一样方便. 这使得sqlplus 的脚本具备和pl/s ...
- mapreduce map 的个数
在map阶段读取数据前,FileInputFormat会将输入文件分割成split.split的个数决定了map的个数.影响map个数(split个数)的主要因素有: 1) 文件的大小.当块(dfs. ...
- 【学习总结】Git学习-本地仓库覆盖式更新对于Git仓库的影响以及pull/push到GitHub
< 许久不用Git之后的探索 > 准备日常更新自己的GitHub了.但是编写的文件平时不放在Git仓库路径下. 故测试覆盖式更新对于仓库是否有影响 直接说结论: 通过对已有库的测试发现覆盖 ...
- 图论专题1考试Problem1
Problem 1. bricksInput file: bricks.inOutput file: bricks.outTime limit: 1 secondjyb 在BUAA 天天被大神虐,所以 ...
- python json数据的转换
1 Python数据转json字符串 import json json_str = json.dumps(py_data) 参数解析: json_str = json.dumps(py_data,s ...
- [模板] 积性函数 && 线性筛
积性函数 数论函数指的是定义在正整数集上的实或复函数. 积性函数指的是当 \((a,b)=1\) 时, 满足 \(f(a*b)=f(a)*f(b)\) 的数论函数. 完全积性函数指的是在任何情况下, ...
- python登录网页版微信发送消息
# coding=utf-8 import datetime import time from selenium import webdriver url = "https://wx2.qq ...
- JS获取字符串实际长度(包含汉字)的简单方法
方法一: var jmz = {}; jmz.GetLength = function(str) { ///<summary>获得字符串实际长度,中文2,英文1</summary&g ...