## leetcode Shuffle an Array

Shuffle a set of numbers without duplicates.

Example:

123456789101112 // Init an array with set 1, 2, and 3.int[] nums = {1,2,3};Solution solution = new Solution(nums);// Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.solution.shuffle();// Resets the array back to its original configuration [1,2,3].solution.reset();// Returns the random shuffling of array [1,2,3].solution.shuffle();

# Java

## leetcode Ransom Note

## leetcode Ransom Note

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.

Each letter in the magazine string can only be used once in your ransom note.

Note:

You may assume that both strings contain only lowercase letters.

123 canConstruct("a", "b") -> falsecanConstruct("aa", "ab") -> falsecanConstruct("aa", "aab") -> true

## leetcode Linked List Random Node

## leetcode Linked List Random Node

Given a singly linked list, return a random node’s value from the linked list. Each node must have the

same probabilityof being chosen.

Follow up:

What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?

Example:

12345678 // Init a singly linked list [1,2,3].ListNode head = new ListNode(1);head.next = new ListNode(2);head.next.next = new ListNode(3);Solution solution = new Solution(head);// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.solution.getRandom();

## leetcode Kth Smallest Element in a Sorted Matrix

## leetcode Kth Smallest Element in a Sorted Matrix

Given a

nxnmatrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not the kth distinct element.

Example:

12345678 matrix = [[ 1, 5, 9],[10, 11, 13],[12, 13, 15]],k = 8,return 13.

Note:

You may assume k is always valid, 1 ≤ k ≤ n^{2}.

## leetcode Combination Sum IV

## leetcode Combination Sum IV

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

123456789101112131415 nums = [1, 2, 3]target = 4The possible combination ways are:(1, 1, 1, 1)(1, 1, 2)(1, 2, 1)(1, 3)(2, 1, 1)(2, 2)(3, 1)Note that different sequences are counted as different combinations.Therefore the output is 7.

Follow up:

What if negative numbers are allowed in the given array?

How does it change the problem?

What limitation we need to add to the question to allow negative numbers?

## leetcode Wiggle Subsequence

## leetcode Wiggle Subsequence

A sequence of numbers is called a

wiggle sequenceif the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.For example,

`[1,7,4,9,2,5]`

is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast,`[1,4,7,2,5]`

and`[1,7,4,5,5]`

are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.

Examples:

12345678910 Input: [1,7,4,9,2,5]Output: 6The entire sequence is a wiggle sequence.Input: [1,17,5,10,13,15,10,5,16,8]Output: 7There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].Input: [1,2,3,4,5,6,7,8,9]Output: 2

Follow up:

Can you do it in O(n) time?

## leetcode Guess Number Higher or Lower II

## leetcode Guess Number Higher or Lower II

We are playing the Guess Game. The game is as follows:

I pick a number from

1ton. You have to guess which number I picked.Every time you guess wrong, I’ll tell you whether the number I picked is higher or lower.

However, when you guess a particular number x, and you guess wrong, you pay

$x. You win the game when you guess the number I picked.

Example:

123456789 n = 10, I pick 8.First round: You guess 5, I tell you that it's higher. You pay $5.Second round: You guess 7, I tell you that it's higher. You pay $7.Third round: You guess 9, I tell you that it's lower. You pay $9.Game over. 8 is the number I picked.You end up paying $5 + $7 + $9 = $21.Given a particular

n ≥ 1, find out how much money you need to have to guarantee awin.

Hint:

- The best strategy to play the game is to minimize the maximum loss you could possibly face. Another strategy is to minimize the expected loss. Here, we are interested in the
firstscenario.- Take a small example (n = 3). What do you end up paying in the worst case?
- Check out this article if you’re still stuck.
- The purely recursive implementation of minimax would be worthless for even a small n. You MUST use dynamic programming.
- As a follow-up, how would you modify your code to solve the problem of minimizing the expected loss, instead of the worst-case loss?

## leetcode Guess Number Higher or Lower

## leetcode Guess Number Higher or Lower

We are playing the Guess Game. The game is as follows:

I pick a number from

1to. You have to guess which number I picked.nEvery time you guess wrong, I’ll tell you whether the number is higher or lower.

You call a pre-defined API

`guess(int num)`

which returns 3 possible results (`-1`

,`1`

, or`0`

):

123 -1 : My number is lower1 : My number is higher0 : Congrats! You got it!

Example:

123 n = 10, I pick 6.Return 6.

## leetcode Super Pow

## leetcode Super Pow

Your task is to calculate

a^{b}mod 1337 whereais a positive integer andbis an extremely large positive integer given in the form of an array.

Example1:

1234 a = 2b = [3]Result: 8

Example2:

1234 a = 2b = [1,0]Result: 1024

## leetcode Sum of Two Integers

## leetcode Sum of Two Integers

Calculate the sum of two integers

aandb, but you arenot allowedto use the operator`+`

and`-`

.

Example:

Givena= 1 andb= 2, return 3.