ZMR小站

很高兴遇见你~


  • 首页

  • 标签

  • 分类

  • 归档

p380

发表于 2016-10-01 | 分类于 blog

LeetCode p380 Insert Delete GetRandom O(1) 题解

1.题目:

Design a data structure that supports all following operations in average O(1) time.
insert(val): Inserts an item val to the set if not already present.
remove(val): Removes an item val from the set if present.
getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.

Example:

// Init an empty set.
RandomizedSet randomSet = new RandomizedSet();

// Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomSet.insert(1);

// Returns false as 2 does not exist in the set.
randomSet.remove(2);

// Inserts 2 to the set, returns true. Set now contains [1,2].
randomSet.insert(2);

// getRandom should return either 1 or 2 randomly.
randomSet.getRandom();

// Removes 1 from the set, returns true. Set now contains [2].
randomSet.remove(1);

// 2 was already in the set, so return false.
randomSet.insert(2);

// Since 1 is the only number in the set, getRandom always return 1.
randomSet.getRandom();

阅读全文 »

p215

发表于 2016-09-30 | 分类于 blog

LeetCode p215 Kth Largest Element in an Array 题解

1.题目:

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4] and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.

阅读全文 »

p48

发表于 2016-09-30 | 分类于 blog

LeetCode p48 Rotate Image 题解

1.题目:

You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?

阅读全文 »

p107

发表于 2016-09-29 | 分类于 blog

LeetCode p107 Binary Tree Level Order Traversal II 题解

1.题目:

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7],

3

/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:

[
[15,7],
[9,20],
[3]
]

阅读全文 »

p141

发表于 2016-09-27 | 分类于 blog

LeetCode p141 Linked List Cycle 题解

1.题目:

Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?

阅读全文 »

p300

发表于 2016-09-27 | 分类于 blog

LeetCode p300 Longest Increasing Subsequence 题解

1.题目:

Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?

阅读全文 »

p313

发表于 2016-09-24 | 分类于 blog

LeetCode p313 Super Ugly Number 题解

1.题目:

Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4.

阅读全文 »

1…161718…33
ZhangMengRou

ZhangMengRou

ZMR小站

230 日志
4 分类
14 标签
© 2019 ZhangMengRou
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.4