GithubHelp home page GithubHelp logo

leetcode's Introduction

典型题型

Min-Max

A,B 轮流选择一个队尾和队首的元素,当队列为空时,判断A,B谁拿取的总数最大

StoneGame 给定一个数组,Alex 每次从数组的最左边或者最右边挑选一个,Lee 也是同样的操作,每次每人的选择都是最优的
求最后的输赢关系

Example 1:

Input: [5,3,4,5]
Output: true
Explanation: 
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.

规律

寻找数字间存在的规律,划分区间

NumberOfDigitOne 给定一个n,求 [1,n] 区间内,1 出现的次数

Example:
Input: 13
Output: 6
Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

NthDigit 给定一个无限长的字符串,规律是 123456789101112..x(x+1)...n
求第 n 个数字 所在的整数x

Input: 11
Output: 0
Explanation: The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.

丑数: 2i3j5k
求第 n 个丑数

Input: n=10
Output: 12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

N数之和

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

值二分法

求最大化的最小值、最小化的最大值问题,通过预测目标结果,进行决策 Rick 有 n 个空的篮子,第 i 个篮子的位置在 position[i] ,Morty 想把 m 个球放到这些篮子里,使得任意两球间 最小磁力 最大。 已知两个球如果分别位于x和y,那么它们之间的磁力为|x - y

给你一个整数数组position和一个整数m,请你返回最大化的最小磁力

输入:position = [1,2,3,4,7], m = 3
输出:3
解释:将 3 个球分别放入位于 1,4 和 7 的三个篮子,两球间的磁力分别为 [3, 3, 6]
最小磁力为 3 

img 最小磁力

相关题型:

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

位运算Single Number

todo: 进阶版Single NumberIII

Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Example :

Input: [2,2,1]
Output: 1

排序的变种(O(n2)算法时考虑)

  1. 基于归并排序的递归回退思路,从小数组到大数组
  2. 基于快排分治的思路,将数组划分成左右数组

当在数组中使用 O(n2)的时间复杂度时,可以考虑使用排序的变种

Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j].

You need to return the number of important reverse pairs in the given array.

Example1:

Input: [1,3,2,3,1]
Output: 2

给定一个序列,按照 output的顺序进行输出:

Input: 
9
25 84 21 47 15 27 68 35 20
output:
  15 20 21 25 47 27 68 35 84
  15 20 21 25 47 27 68 35 84
  15 20 21 25 47 27 68 35 84
  15 20 21 25 35 27 47 68 84
  15 20 21 25 27 35 47 68 84
  15 20 21 25 27 35 47 68 84

二分查找(有序 O(n))

基于二分的**,将 O(n) 变成 O(NlogN)

给定一个有序递增数组,查找目标 target 的最左端和最右端

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

假设一个单调递增的数组里的每个元素都是整数并且是唯一的。

实现一个函数找出数组中任意一个数值等于其下标的元素。

Example:

Input: nums = [-3, -1, 1, 3, 5]
Output: 3

二叉树的查找

  1. 前序遍历的利用
  2. 中序遍历的利用
  3. 后序遍历的利用[需要对左右子树先行进行特殊处理]

非递归前序遍历树的最大深度

Given a binary tree, find its maximum depth.

Example:

Input:
    3
   / \
  9  20
    /  \
   15   7

output: 3

非递归中序遍历Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

Example:

Input: root = [3,1,4,null,2], k = 1
   3
  / \
 1   4
  \
  2
Output: 1

给定一棵二叉树,判断该树是不是平衡二叉树

任意左右子树,高度差不大于1

Example:

Input: root = [3,3,8,null,4]
   5
  / \
 3   8
  \
  4
Output: true

回溯法

回溯法适合由多个步骤组成的问题,每一个步骤都有多个选择, 如果用树形象的表示,在某一个节点有n个子节点,树的叶子节点为最终状态。 在当前叶子节点不满足约束时,回溯到上一个节点

字典中回溯寻找单词

WordSearch

数字全排序

Permutations

双指针

一维数组中使用双指针排序

SortArrayByParity

LinkedList

  • 快慢指针寻找中间节点 第一次快慢指针从 Head 节点出发,快指针走两步,慢指针走一步,在慢指针走到环头节点前,快慢指针就会相遇,

令 头节点到环形入口节点 长度为a,环形长度为 b

令 fast 指针走 f 步,slow 指针走 s 步,则 f=2s

由于存在环,两个指针速度差距为1,则 fast = nb + s = 2s

则 s=nb

考虑一种新的情况,一个节点走 a+ nb ,那么该节点一定会走到环形入口处 综上,在两指针相遇后,slow 走了nb步,此时在走a步就可以到达环形入口处,此时用 另一个指针从 head 节点出发走 a 步,则两指针会在环形入口处相遇

LinkedListCycleII

Palindrome Linked List 两者的结合

SP

Dijkstra

NetworkDelayTime

Bellman-Ford

CheapestFlightsWithinKStops

DP

将大问题分解成小问题,求解每个子问题的最优解,从而求出大问题的解

  1. 每个子问题都有最优解且不会改变
  2. 同样的子问题会重复出现

无重复就退化成了 divide and conquer

  • 使用递归方式【计划递归】

  • 使用循环 【动态规划】

  • I: 输入规模

  • S: 子问题规模

  • D: 依赖规模

https://www.youtube.com/watch?v=eLlZEYzZVyQ

I(n) S(n) D(1)

D(1) 即依赖于有限个解 DominoAndTrominoTiling

使用如下: 一 和 L 型 进行覆盖2 *N 的面板

XX  <- domino

XX  <- "L" tromino
X

Given N, how many ways are there to tile a 2 x N board? Return your answer modulo 10^9 + 7.

Example:
Input: 3
Output: 5
Explanation:
The five different ways are listed below, different letters indicates different tiles:
XYZ XXZ XYY XXY XYY
XYZ YYZ XZZ XYY XXY

类似题型:

Climbing Stairs

MinCostClimbingStairs

House Robber

UniquePaths

交换数组序列【Hard】MinimumSwapsToMakeSequencesIncreasing

反转字符'0' 和 ’1‘FlipStringToMonotoneIncreasing

I(n) S(n) D(n)

D(n) 依赖于所有比当前 N 规模小的子问题

Word Break

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

Example 1:

Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".

Example 2:

Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
            Note that you are allowed to reuse a dictionary word.

I(n) S(n2) D(i..j)

D(i..j) 依赖与 i..j 内的全部子问题,dp(i,j) 主要依赖于 dp(i,j),dp(i,k),dp(k,j)

S(n2 ) 每个子问题都需要 O(n) 的时间复杂度

BurstBalloons

Given n balloons, indexed from 0 to n-1.

Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons.

If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins.

Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

Find the maximum coins you can collect by bursting the balloons wisely.

Example1:

Input: [3,1,5,8]
Output: 167

类似题目:

StrangePrinter

划分成两个和相等的子序列【0-1背包问题】PartitionEqualSubsetSum

给定金额求有多少种组合【完全背包问题】 给定金额求硬币的最少数量【完全背包问题】

I(O(m)+O(n)) S(MN) D(1)

I(O(m)+O(n)) 两个数组或字符串 S(MN) 二维dp 且通常依赖 dp[i-1][j-1]||dp[i-1][j]||dp[i][j-1]

EditDistance

Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.

You have the following 3 operations permitted on a word:

  1. Insert a character
  2. Delete a character
  3. Replace a character

Example 1:

Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')

Example 2:

Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')

类似题型: Minimum ASCII Delete Sum for Two Strings

I(MN) S(MN) D(1)

Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

Example :

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1\. Right -> Right -> Down
2\. Right -> Down -> Right
3\. Down -> Right -> Right

类似题型

I(MN) S(MN) D(NN)[k,i,j]

dp(k,i,j) := sol of (A[0->i][0->j] after k steps) 只依赖于 1个子问题

Out of Boundary Paths

There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ball to adjacent cell or cross the grid boundary in four directions (up, down, left, right). However, you can at most move N times. Find out the number of paths to move the ball out of grid boundary. The answer may be very large, return it after mod 109 + 7.

Example :

Input: m = 1, n = 3, N = 3, i = 0, j = 1
Output: 12
Explanation:

OutOfBoundary

数据结构

跳表的构建过程

  1. 在插入过程中,只保证向下和向左查找
  2. 直到最下层中找到插入位置,将target 置成随机层高,如果当前随机层高大于当前最大层高,则从head中的每一层的末尾指向当前target 自我丑陋版SkipList

skiplist.png

并查集

使用一个数组保存各节点之间的连接信息,可以用来判断给定的节点是否存在环路 简陋版UF

uf.png

Kotlin 使用

谨慎 if 表达式

carefulIfExpression

private fun add(i: Int, j: Int): Int = 2 + if (i == 1) 0 else i + if (j == 1) 0 else j

中 + 操作符优先于 return , 实际为

   private final int add(int i, int j) {
      return  2 + (i == 1 ? 0 : i + (j == 1 ? 0 : j));
   }

正确写法

    private fun add(i: Int, j: Int): Int = 2 + if (i == 1) 0 else {
        i
    } + if (j == 1) 0 else j

leetcode's People

Contributors

wezhyn avatar

Watchers

James Cloos avatar  avatar

Forkers

sddai

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.