Description
Given an unsorted array nums
, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]...
.
Example:
1 | Input: nums = [3,5,2,1,6,4] |
https://leetcode.com/problems/wiggle-sort/description/
Solution
1 | class Solution { |
Why don't you come to your senses?
Given an unsorted array nums
, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]...
.
Example:
1 | Input: nums = [3,5,2,1,6,4] |
https://leetcode.com/problems/wiggle-sort/description/
1 | class Solution { |
https://leetcode.com/problems/sparse-matrix-multiplication/description/
1 | class Solution { |
https://leetcode.com/problems/design-in-memory-file-system/description/
`
class Node {
public:
bool type; //true is file, false is directory
map<string, Node*> mapping;
string str;
string name;
Node (string fileName, bool isFile) {
name = fileName;
type = isFile;
}
};
class FileSystem {
private:
Node* root;
public:
FileSystem() {
root = new Node(“LLL”, false);
}
vector<string> ls(string path) {
int size = path.size();
Node* traverse = root;
int index = 1;
while (index < size) {
int count = 0;
while(index + count < size && path[index + count] != '/') ++count;
string target = path.substr(index, count);
index += count;
++index; //skip the '/'
auto iter = traverse->mapping.find(target);
if (iter == traverse->mapping.end()) return vector<string>();
traverse = traverse->mapping[target];
}
vector<string> ret;
//Is a file
if (traverse->type) {
ret.push_back(traverse->name);
return ret;
}
//Is a directory
for (auto item : traverse->mapping) {
ret.push_back(item.first);
}
return ret;
}
void mkdir(string path) {
int size = path.size();
if (size == 1) return;
Node* traverse = root;
int index = 1;
while (index < size) {
int count = 0;
while(index + count < size && path[index + count] != '/') ++count;
string target = path.substr(index, count);
index += count;
++index; //skip the '/'
auto iter = traverse->mapping.find(target);
if (iter == traverse->mapping.end()) traverse->mapping[target] = new Node(target, false);
traverse = traverse->mapping[target];
}
return;
}
void addContentToFile(string filePath, string content) {
int size = filePath.size();
if (size == 1) return;
Node* traverse = root;
int index = 1;
while (index < size) {
int count = 0;
while(index + count < size && filePath[index + count] != '/') ++count;
string target = filePath.substr(index, count);
index += count;
++index; //skip the '/'
auto iter = traverse->mapping.find(target);
if (iter == traverse->mapping.end()) traverse->mapping[target] = new Node(target, true); //Add a new file
traverse = traverse->mapping[target];
}
traverse->str += content;
return;
}
string readContentFromFile(string filePath) {
int size = filePath.size();
if (size == 1) return string();
Node* traverse = root;
int index = 1;
while (index < size) {
int count = 0;
while(index + count < size && filePath[index + count] != '/') ++count;
string target = filePath.substr(index, count);
index += count;
++index; //skip the '/'
traverse = traverse->mapping[target];
}
return traverse->str;
}
};
/**
You are given several logs that each log contains a unique id and timestamp. Timestamp is a string that has the following format: Year:Month:Day:Hour:Minute:Second
, for example, 2017:01:01:23:59:59
. All domains are zero-padded decimal numbers.
Design a log storage system to implement the following functions:
void Put(int id, string timestamp)
: Given a log’s unique id and timestamp, store the log in your storage system.
int[] Retrieve(String start, String end, String granularity)
: Return the id of logs whose timestamps are within the range from start to end. Start and end all have the same format as timestamp. However, granularity means the time level for consideration. For example, start = “2017:01:01:23:59:59”, end = “2017:01:02:23:59:59”, granularity = “Day”, it means that we need to find the logs within the range from Jan. 1st 2017 to Jan. 2nd 2017.
Example 1:
1 | put(1, "2017:01:01:23:59:59"); |
Note:
1 | class Time { |
https://leetcode.com/problems/number-of-islands-ii/description/
1 | class Solution { |
Design a search autocomplete system for a search engine. Users may input a sentence (at least one word and end with a special character '#'
). For each character they type except ‘#’, you need to return the top 3 historical hot sentences that have prefix the same as the part of sentence already typed. Here are the specific rules:
Your job is to implement the following functions:
The constructor function:
AutocompleteSystem(String[] sentences, int[] times):
This is the constructor. The input is historical data. Sentences
is a string array consists of previously typed sentences. Times
is the corresponding times a sentence has been typed. Your system should record these historical data.
Now, the user wants to input a new sentence. The following function will provide the next character the user types:
List<String> input(char c):
The input c
is the next character typed by the user. The character will only be lower-case letters ('a'
to 'z'
), blank space (' '
) or a special character ('#'
). Also, the previously typed sentence should be recorded in your system. The output will be the top 3 historical hot sentences that have prefix the same as the part of sentence already typed.
Example:
Operation: AutocompleteSystem([“i love you”, “island”,”ironman”, “i love leetcode”], [5,3,2,2])
The system have already tracked down the following sentences and their corresponding times:"i love you"
: 5
times"island"
: 3
times"ironman"
: 2
times"i love leetcode"
: 2
times
Now, the user begins another search:
Operation: input(‘i’)
Output: [“i love you”, “island”,”i love leetcode”]
Explanation:
There are four sentences that have prefix "i"
. Among them, “ironman” and “i love leetcode” have same hot degree. Since ' '
has ASCII code 32 and 'r'
has ASCII code 114, “i love leetcode” should be in front of “ironman”. Also we only need to output top 3 hot sentences, so “ironman” will be ignored.
Operation: input(‘ ‘)
Output: [“i love you”,”i love leetcode”]
Explanation:
There are only two sentences that have prefix "i "
.
Operation: input(‘a’)
Output: []
Explanation:
There are no sentences that have prefix "i a"
.
Operation: input(‘#’)
Output: []
Explanation:
The user finished the input, the sentence "i a"
should be saved as a historical sentence in system. And the following input will be counted as a new search.
Note:
1 | #define TOP 3 |
https://leetcode.com/problems/meeting-rooms-ii/description/
1 | /** |
Suppose you are at a party with n
people (labeled from 0
to n - 1
) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1
people know him/her but he/she does not know any of them.
Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: “Hi, A. Do you know B?” to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
You are given a helper function bool knows(a, b)
which tells you whether A knows B. Implement a function int findCelebrity(n)
, your function should minimize the number of calls to knows
.
Note: There will be exactly one celebrity if he/she is in the party. Return the celebrity’s label if there is a celebrity in the party. If there is no celebrity, return -1
.
1 | // Forward declaration of the knows API. |
https://leetcode.com/problems/alien-dictionary/description/
1 | class Solution { |
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/
1 | /** |