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难度)的更多相关文章

  1. bzoj 前100题计划

    bzoj前100题计划 xz布置的巨大的坑.. 有空填题解... 1002 轮状病毒 用python手动matrixtree打表. #include<bits/stdc++.h> #def ...

  2. 两个月刷完Leetcode前400题经验总结

    更新:气死了,挂个傻逼: 每次做个分享.组织个活动,就会有一些傻逼冒泡生怕别人不知道他是傻逼,气死我了!自己好好看看非法集资的概念,我办这个活动,一分钱都没收,入群99元是督促大家完成刷题任务,最后完 ...

  3. 【总结】 BZOJ前100题总结

    前言 最近发现自己trl,所以要多做题目但是Tham布置的题目一道都不会,只能来写BZOJ HA(蛤)OI 1041 复数可以分解成两个点,所以直接把\(R^2\)质因数分解一下就可以了,注意计算每一 ...

  4. LeetCode Top 100 Liked 点赞最高的 100 道算法题

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:刷题顺序,刷题路径,好题,top100,怎么刷题,Leet ...

  5. LeetCode面试常见100题( TOP 100 Liked Questions)

    LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...

  6. 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 ...

  7. Leetcode重点 250题-前400 题

    删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...

  8. Leetcode OJ 刷题

    Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...

  9. 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 + ...

随机推荐

  1. OpenCV 与 OpenGL 的关系是什么?

    OpenCV是 Open Source Computer Vision LibraryOpenGL是 Open Graphics LibraryOpenCV主要是提供图像处理和视频处理的基础算法库,还 ...

  2. 多线程学习笔记-深入理解ThreadPoolExecutor

    java多线程中,线程池的最上层接口是Executor,ExecutorService实现了Executor,是真正的管理线程池的接口,ThreadPoolExecutor间接继承了ExecutorS ...

  3. LOJ2396 JOISC2017 长途巴士 斜率优化

    传送门 将乘客按照\(D_i\)从小到大排序并重新标号.对于服务站\(j\),如果\(S_j \mod T \in (D_i , D_{i+1})\),那么可以少接一些水,在保证司机有水喝的情况下让编 ...

  4. Lepus搭建企业级数据库慢查询分析平台

    前言 Lepus的慢查询分析平台是独立于监控系统的模块,该功能需要使用percona-toolkit工具来采集和记录慢查询日志,并且需要部署一个我们提供的shell脚本来进行数据采集.该脚本会自动开启 ...

  5. Elastic Stack初篇-Logstash

     一.Logstash简介      Logstash是一个开源数据收集引擎,具有实时管道功能.Logstash可以动态地将来自不同数据源的数据统一起来,并将数据标准化到你所选择的目的地. 二.Log ...

  6. 通过secureCRT连接虚拟机VMware workstation问题记录

    很急没有使用虚拟机了,今天再登录的时候,发现用secureCRT连接不上VMware workstation 1.连接步骤: 1)打开secureCRT,点击+ 新建一个连接 2)按照流程一步一步配置 ...

  7. python获取list列表随机数据

    第一种方法(推荐)适用于随机取一个值, 返回一个值import randomlist1 = ['佛山', '南宁', '北海', '杭州', '南昌', '厦门', '温州']a = random.c ...

  8. 好坑吖,几乎GG,关于Nginx和supervisor的配置

    从没想过部署个站点居然这么费劲.这还是搞了好多回且环境都具备的前提一下. 不过问题还是挺刁钻的,下面详细说一下. 1.net core编译少dll文件问题 发布到服务器上执行dotnet FileSe ...

  9. python之序列化模块、双下方法(dict call new del len eq hash)和单例模式

    摘要:__new__ __del__ __call__ __len__ __eq__ __hash__ import json 序列化模块 import pickle 序列化模块 补充: 现在我们都应 ...

  10. MySQL——修改一个表的自增值

    语句 alter table <table name> auto_increment=<value>; 示例 mysql; Query OK, rows affected (0 ...