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
/** | |
* Given an input string, reverse the string word by word. | |
* For example, | |
* Given s = "the sky is blue", | |
* return "blue is sky the". | |
* Clarification: | |
* What constitutes a word? | |
***A sequence of non-space characters constitutes a word. | |
* Could the input string contain leading or trailing spaces? | |
***Yes. However, your reversed string should not contain leading or trailing spaces. |
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
/** | |
* Divide two integers without using multiplication, division and mod operator. | |
*/ | |
public class Solution { | |
public int divide(int dividend, int divisor) { | |
if(dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE; | |
int res = 0; | |
boolean isNeg = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0); | |
// a / 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
/** | |
* You are given a string, S, and a list of words, L, that are all of the same length. | |
* Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. | |
* For example, given: | |
* S: "barfoothefoobarman" | |
* L: ["foo", "bar"] | |
* You should return the indices: [0,9]. | |
* (order does not matter). | |
*/ | |
public class Solution { |
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
/** | |
* Given two numbers represented as strings, return multiplication of the numbers as a string. | |
* Note: The numbers can be arbitrarily large and are non-negative. | |
*/ | |
public class Solution { | |
public String multiply(String num1, String num2) { | |
if(num1.equals("0") || num2.equals("0")) | |
return "0"; | |
int l1 = num1.length(), l2 = num2.length(); |
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
/** | |
* Implement regular expression matching with support for '.' and '*'. | |
* '.' Matches any single character. | |
* '*' Matches zero or more of the preceding element. | |
* The matching should cover the entire input string (not partial). | |
* The function prototype should be: | |
* bool isMatch(const char *s, const char *p) | |
* Some examples: | |
* isMatch("aa","a") → false | |
* isMatch("aa","aa") → true |
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
/** | |
* Implement wildcard pattern matching with support for '?' and '*'. | |
* '?' Matches any single character. | |
* '*' Matches any sequence of characters (including the empty sequence). | |
* The matching should cover the entire input string (not partial). | |
* The function prototype should be: | |
* bool isMatch(const char *s, const char *p) | |
* Some examples: | |
* isMatch("aa","a") → false | |
* isMatch("aa","aa") → true |
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
/** | |
* Given a collection of intervals, merge all overlapping intervals. | |
* For example, | |
* Given [1,3],[2,6],[8,10],[15,18], | |
* return [1,6],[8,10],[15,18]. | |
*/ | |
/** | |
* Definition for an interval. | |
* public class Interval { |
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
/** | |
* Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. | |
* You should pack your words in a greedy approach; that is, pack as many words as you can in each line. | |
* Pad extra spaces ' ' when necessary so that each line has exactly L characters. | |
* Extra spaces between words should be distributed as evenly as possible. | |
* If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. | |
* For the last line of text, it should be left justified and no extra space is inserted between words. | |
* For example, | |
* words: ["This", "is", "an", "example", "of", "text", "justification."] | |
* L: 16. |
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
/** | |
* Given an absolute path for a file (Unix-style), simplify it. | |
* For example, | |
* path = "/home/", => "/home" | |
* path = "/a/./b/../../c/", => "/c" | |
* Corner Cases: | |
* Did you consider the case where path = "/../"? | |
* In this case, you should return "/". | |
* Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/". | |
* In this case, you should ignore redundant slashes and return "/home/foo". |
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
/** | |
* Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). | |
* For example, | |
* S = "ADOBECODEBANC" | |
* T = "ABC" | |
* Minimum window is "BANC". | |
* Note: | |
* If there is no such window in S that covers all characters in T, return the emtpy string "". | |
* If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S. | |
*/ |
NewerOlder