leetcode 138. Copy List with Random Pointer

leetcode
九章

题目描述

A linked list is given such that each node contains an additional random
pointer which could point to any node in the list or null.

Return a deep
copy
of the list.

The Linked List is represented in the input/output as a list of n nodes.
Each node is represented as a pair of [val, random_index] where:

  • val: an integer representing Node.val
  • random_index: the index of the node (range from 0 to n-1) where random pointer points to, or null if it does not point to any node.

Example 1:


Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]] Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]

Example 2:


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

Example 3:


Input: head = [[3,null],[3,0],[3,null]] Output: [[3,null],[3,0],[3,null]]

Example 4:
Input: head = [] Output: [] Explanation: Given linked list is empty (null pointer), so return null.

Constraints:

  • -10000 <= Node.val <= 10000
  • Node.random is null or pointing to a node in the linked list.
  • Number of Nodes will not exceed 1000.

Tags: Hash Table, Linked List

Difficulty: Medium

答案



leetcode 718. Maximum Length of Repeated Subarray

leetcode
九章

题目描述

Given two integer arrays A and B, return the maximum length of an subarray
that appears in both arrays.

Example 1:

Input:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
Output: 3
Explanation: 
The repeated subarray with maximum length is [3, 2, 1].

Note:

  1. 1 <= len(A), len(B) <= 1000
  2. 0 <= A[i], B[i] < 100

Tags: Array, Hash Table, Binary Search, Dynamic Programming

Difficulty: Medium

答案



leetcode 739. Daily Temperatures

leetcode
九章

题目描述

Given a list of daily temperatures T, return a list such that, for each day
in the input, tells you how many days you would have to wait until a warmer
temperature. If there is no future day for which this is possible, put 0
instead.

For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].

Note: The length of temperatures will be in the range [1, 30000]. Each
temperature will be an integer in the range [30, 100].

Tags: Hash Table, Stack

Difficulty: Medium

答案

LeetCode 图解 | 739.每日温度 - 每日温度 - 力扣(LeetCode)



leetcode 387. First Unique Character in a String

leetcode
九章

题目描述

Given a string, find the first non-repeating character in it and return it’s
index. If it doesn’t exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

Note: You may assume the string contain only lowercase letters.

Tags: Hash Table, String

Difficulty: Easy

答案



leetcode 525. Contiguous Array

leetcode
九章

题目描述

Given a binary array, find the maximum length of a contiguous subarray with
equal number of 0 and 1.

Example 1:

Input: [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.

Example 2:

Input: [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.

Note: The length of the given binary array will not exceed 50,000.

Tags: Hash Table

Difficulty: Medium

答案



leetcode 3. Longest Substring Without Repeating Characters

难度:Middle

leetcode
九章

题目描述

Given a string, find the length of the longest substring without repeating
characters.

Example 1:

Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3. 

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. 
             Note that the answer must be a **substring** , "pwke" is a _subsequence_ and not a substring.

Tags: Hash Table, Two Pointers, String, Sliding Window

Difficulty: Medium

答案

leetcode 409. Longest Palindrome

难度:Easy

leetcode
九章

题目描述

Given a string which consists of lowercase or uppercase letters, find the
length of the longest palindromes that can be built with those letters.

This is case sensitive, for example "Aa" is not considered a palindrome
here.

Note:
Assume the length of given string will not exceed 1,010.

Example:

Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.

Tags: Hash Table

Difficulty: Easy

答案

leetcode 380. Insert Delete GetRandom O(1)

难度:Middle

leetcode
九章

题目描述

Design a data structure that supports all following operations in average
O(1) time.

  1. insert(val): Inserts an item val to the set if not already present.
  2. remove(val): Removes an item val from the set if present.
  3. 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 2 is the only number in the set, getRandom always return 2.
randomSet.getRandom();

Tags: Array, Hash Table, Design

Difficulty: Medium

答案

leetcode 350. Intersection of Two Arrays II

难度:Easy

leetcode
九章

题目描述

Given two arrays, write a function to compute their intersection.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1 ‘s size is small compared to nums2 ‘s size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

Tags: Hash Table, Two Pointers, Binary Search, Sort

Difficulty: Easy

答案

leetcode 118. Pascal's Triangle

难度:Easy

leetcode
九章

题目描述

Given two arrays, write a function to compute their intersection.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1 ‘s size is small compared to nums2 ‘s size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

Tags: Hash Table, Two Pointers, Binary Search, Sort

Difficulty: Easy

答案


:D 一言句子获取中...