Press "Enter" to skip to content

Posts published in December 2017

花花酱 LeetCode 653. Two Sum IV – Input is a BST

题目大意:给你一棵二叉搜索树,返回树中是否存在两个节点的和等于给定的目标值。

Problem:

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.

Example 1:

Example 2:

Solution:

C++

 

花花酱 LeetCode 748. Largest Number At Least Twice of Others

题目大意:给你一个数组,问你其中最大的数是不是比剩下的数大2倍以上,返回这样的数的索引。如果不存在,返回-1.

Problem:

In a given integer array nums, there is always exactly one largest element.

Find whether the largest element in the array is at least twice as much as every other number in the array.

If it is, return the index of the largest element, otherwise return -1.

Example 1:

Example 2:

Note:

  1. nums will have a length in the range [1, 50].
  2. Every nums[i] will be an integer in the range [0, 99].

Solution:

C++

 

花花酱 LeetCode 17. Letter Combinations of a Phone Number

题目大意:给你一串电话号码,输出可以由这串电话号码打出的所有字符串。

Problem:

Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

Solution 1: DFS

C++

Java

Python3

Solution 2:  BFS

C++

Java

Python

 

花花酱 LeetCode Input Size V.S. Time Complexity SP2

本期节目介绍了输入数据规模和时间复杂度上限的关系,可以通过数据规模推算使用算法的类型。

 

  • < 10: O(n!) permutation
  • < 15: O(2^n) combination
  • < 50: O(n^4) DP
  • < 200: O(n^3) DP, all pairs shortest path
  • < 1,000: O(n^2) DP, all pairs, dense graph
  • < 1,000,000: O(nlogn), sorting-based (greedy), heap, divide & conquer
  • < 1,000,000: O(n), DP, graph traversal / topological sorting (V+E), tree traversal
  • < INT_MAX: O(sqrt(n)), prime, square sum
  • < INT_MAX: O(logn), binary search
  • < INT_MAX: O(1) Math

花花酱 LeetCode 752. Open the Lock

Problem:

You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.

The lock initially starts at '0000', a string representing the state of the 4 wheels.

You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.

Given a target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.

Example 1:

Example 2:

Example 3:

Example 4:

Note:

  1. The length of deadends will be in the range [1, 500].
  2. target will not be in the list deadends.
  3. Every string in deadends and the string target will be a string of 4 digits from the 10,000 possibilities '0000' to '9999'.


题目大意:

给你一个4位密码锁,0可以转到1和9,1可以转到0和2,。。。,9可以转到0和8。

另外给你一些死锁的密码,比如1234,一但转到任何一个死锁的密码,锁就无法再转动了。

给你一个目标密码,问你最少要转多少次才能从0000转到目标密码。

Solution:

C++

C++ / Bidirectional BFS

C++ / Bidirectional BFS / int state / Array

Java

Python

Python / Int state