forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/kdn251/interviews
- Loading branch information
Showing
44 changed files
with
1,273 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl | ||
//and it returns a short URL such as http://tinyurl.com/4e9iAk. | ||
// | ||
//Design the encode and decode methods for the TinyURL service. There is no restriction on how your | ||
//encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL | ||
//and the tiny URL can be decoded to the original URL. | ||
|
||
public class EncodeAndDecodeTinyURL { | ||
HashMap<String, String> map = new HashMap<String, String>(); | ||
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
int count = 1; | ||
|
||
public String getKey() { | ||
String key = ""; | ||
while(count > 0) { | ||
count--; | ||
key += characters.charAt(count); | ||
count /= characters.length(); | ||
} | ||
|
||
return key; | ||
} | ||
|
||
// Encodes a URL to a shortened URL. | ||
public String encode(String longUrl) { | ||
String key = getKey(); | ||
map.put(key, longUrl); | ||
count++; | ||
|
||
return "http://tinyurl.com/" + key; | ||
} | ||
|
||
// Decodes a shortened URL to its original URL. | ||
public String decode(String shortUrl) { | ||
return map.get(shortUrl.replace("http://tinyurl.com/", "")); | ||
} | ||
} | ||
|
||
// Your Codec object will be instantiated and called as such: | ||
// Codec codec = new Codec(); | ||
// codec.decode(codec.encode(url)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//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. | ||
|
||
class FirstUniqueCharacterInAString { | ||
public int firstUniqChar(String s) { | ||
HashMap<Character, Integer> characters = new HashMap<Character, Integer>(); | ||
for(int i = 0; i < s.length(); i++) { | ||
char current = s.charAt(i); | ||
if(characters.containsKey(current)) { | ||
characters.put(current, -1); | ||
} else { | ||
characters.put(current, i); | ||
} | ||
} | ||
|
||
int min = Integer.MAX_VALUE; | ||
for(char c: characters.keySet()) { | ||
if(characters.get(c) > -1 && characters.get(c) < min) { | ||
min = characters.get(c); | ||
} | ||
} | ||
|
||
return min == Integer.MAX_VALUE ? -1 : min; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//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. | ||
|
||
class FirstUniqueCharacterInAString { | ||
public int firstUniqChar(String s) { | ||
HashMap<Character, Integer> characters = new HashMap<Character, Integer>(); | ||
for(int i = 0; i < s.length(); i++) { | ||
char current = s.charAt(i); | ||
if(characters.containsKey(current)) { | ||
characters.put(current, -1); | ||
} else { | ||
characters.put(current, i); | ||
} | ||
} | ||
|
||
int min = Integer.MAX_VALUE; | ||
for(char c: characters.keySet()) { | ||
if(characters.get(c) > -1 && characters.get(c) < min) { | ||
min = characters.get(c); | ||
} | ||
} | ||
|
||
return min == Integer.MAX_VALUE ? -1 : min; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl | ||
//and it returns a short URL such as http://tinyurl.com/4e9iAk. | ||
// | ||
//Design the encode and decode methods for the TinyURL service. There is no restriction on how your | ||
//encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL | ||
//and the tiny URL can be decoded to the original URL. | ||
|
||
public class EncodeAndDecodeTinyURL { | ||
HashMap<String, String> map = new HashMap<String, String>(); | ||
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
int count = 1; | ||
|
||
public String getKey() { | ||
String key = ""; | ||
while(count > 0) { | ||
count--; | ||
key += characters.charAt(count); | ||
count /= characters.length(); | ||
} | ||
|
||
return key; | ||
} | ||
|
||
// Encodes a URL to a shortened URL. | ||
public String encode(String longUrl) { | ||
String key = getKey(); | ||
map.put(key, longUrl); | ||
count++; | ||
|
||
return "http://tinyurl.com/" + key; | ||
} | ||
|
||
// Decodes a shortened URL to its original URL. | ||
public String decode(String shortUrl) { | ||
return map.get(shortUrl.replace("http://tinyurl.com/", "")); | ||
} | ||
} | ||
|
||
// Your Codec object will be instantiated and called as such: | ||
// Codec codec = new Codec(); | ||
// codec.decode(codec.encode(url)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number. | ||
// | ||
//Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. | ||
// | ||
//Please note that both secret number and friend's guess may contain duplicate digits. | ||
// | ||
//Example 1: | ||
// | ||
//Input: secret = "1807", guess = "7810" | ||
// | ||
//Output: "1A3B" | ||
// | ||
//Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7. | ||
//Example 2: | ||
// | ||
//Input: secret = "1123", guess = "0111" | ||
// | ||
//Output: "1A1B" | ||
// | ||
//Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow. | ||
//Note: You may assume that the secret number and your friend's guess only contain digits, and their lengths are always equal. | ||
|
||
class BullsAndCows { | ||
public String getHint(String secret, String guess) { | ||
int bulls = 0; | ||
int cows = 0; | ||
int[] counts = new int[10]; | ||
for(int i = 0; i < secret.length(); i++) { | ||
if(secret.charAt(i) == guess.charAt(i)) { | ||
bulls++; | ||
} else { | ||
if(counts[secret.charAt(i) - '0']++ < 0) { | ||
cows++; | ||
} | ||
if(counts[guess.charAt(i) - '0']-- > 0) { | ||
cows++; | ||
} | ||
} | ||
} | ||
|
||
return bulls + "A" + cows + "B"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//Given a list of daily temperatures, produce a list 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 temperatures = [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]. | ||
|
||
class DailyTemperatures { | ||
public int[] dailyTemperatures(int[] temperatures) { | ||
int[] result = new int[temperatures.length]; | ||
Stack<Integer> stack = new Stack<Integer>(); | ||
for(int i = 0; i < temperatures.length; i++) { | ||
while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) { | ||
int index = stack.pop(); | ||
result[index] = i - index; | ||
} | ||
stack.push(i); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl | ||
//and it returns a short URL such as http://tinyurl.com/4e9iAk. | ||
// | ||
//Design the encode and decode methods for the TinyURL service. There is no restriction on how your | ||
//encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL | ||
//and the tiny URL can be decoded to the original URL. | ||
|
||
public class EncodeAndDecodeTinyURL { | ||
HashMap<String, String> map = new HashMap<String, String>(); | ||
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
int count = 1; | ||
|
||
public String getKey() { | ||
String key = ""; | ||
while(count > 0) { | ||
count--; | ||
key += characters.charAt(count); | ||
count /= characters.length(); | ||
} | ||
|
||
return key; | ||
} | ||
|
||
// Encodes a URL to a shortened URL. | ||
public String encode(String longUrl) { | ||
String key = getKey(); | ||
map.put(key, longUrl); | ||
count++; | ||
|
||
return "http://tinyurl.com/" + key; | ||
} | ||
|
||
// Decodes a shortened URL to its original URL. | ||
public String decode(String shortUrl) { | ||
return map.get(shortUrl.replace("http://tinyurl.com/", "")); | ||
} | ||
} | ||
|
||
// Your Codec object will be instantiated and called as such: | ||
// Codec codec = new Codec(); | ||
// codec.decode(codec.encode(url)); |
Oops, something went wrong.