This file contains hidden or 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
| /* | |
| Say you have an array for which the ith element is the price of a given stock on day i. | |
| If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. | |
| */ | |
| import java.util.*; | |
| public class Solution { | |
| public class Stock implements Comparable<Stock>{ | |
| public int index; | |
| public int price; |
This file contains hidden or 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
| /* | |
| Say you have an array for which the ith element is the price of a given stock on day i. | |
| Design an algorithm to find the maximum profit. You may complete as many transactions | |
| as you like (ie, buy one and sell one share of the stock multiple times). However, you | |
| may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). | |
| */ | |
| import java.util.*; | |
| public class Solution { | |
| Hashtable<Integer, Boolean> seen = new Hashtable<Integer, Boolean>(); |
This file contains hidden or 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
| /* | |
| Say you have an array for which the ith element is the price of a given stock on day i. | |
| Design an algorithm to find the maximum profit. You may complete at most two transactions. | |
| Note: | |
| You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). | |
| */ | |
| public class Solution { | |
| public int maxProfit(int[] prices) { |
This file contains hidden or 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
| import java.util.*; | |
| public class Solution { | |
| public int lengthOfLongestSubstring(String s) { | |
| // Start typing your Java solution below | |
| // DO NOT write main() function | |
| int count = 0; | |
| int max = 0; | |
| int startIndex = 0; | |
| int endIndex = 0; |
This file contains hidden or 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
| /** | |
| * Definition for singly-linked list. | |
| * public class ListNode { | |
| * int val; | |
| * ListNode next; | |
| * ListNode(int x) { | |
| * val = x; | |
| * next = null; | |
| * } | |
| * } |
This file contains hidden or 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
| /* | |
| Reverse digits of an integer. | |
| Example1: x = 123, return 321 | |
| Example2: x = -123, return -321 | |
| Have you thought about this? | |
| Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! | |
| If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. |
This file contains hidden or 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
| /* | |
| Determine whether an integer is a palindrome. Do this without extra space. | |
| Some hints: | |
| Could negative integers be palindromes? (ie, -1) | |
| If you are thinking of converting the integer to string, note the restriction of using extra space. | |
| You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case? |
This file contains hidden or 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 linked list, swap every two adjacent nodes and return its head. | |
| For example, | |
| Given 1->2->3->4, you should return the list as 2->1->4->3. | |
| Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. | |
| */ | |
| /** |
This file contains hidden or 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 digit string, return all possible letter combinations that the number could represent. | |
| A mapping of digit to letters (just like on the telephone buttons) is given below. | |
| Input:Digit string "23" | |
| Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. | |
| Note: | |
| Although the above answer is in lexicographical order, your answer could be in any order you want. | |
| */ |
This file contains hidden or 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 n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. | |
| For example, given n = 3, a solution set is: | |
| "((()))", "(()())", "(())()", "()(())", "()()()" | |
| */ | |
| public class Solution { | |
| public ArrayList<String> generateParenthesis(int n) { |