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.io.BufferedReader; | |
| import java.io.InputStreamReader; | |
| public class Game { | |
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
| public static void main(String[] argv) throws Exception { | |
| Game game = new Game(); | |
| game.entry(); |
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
| public class StringRotate { | |
| int count; | |
| public static void main(String[] argv) { | |
| StringRotate sr = new StringRotate(); | |
| System.out.println(sr.wordSwap("0123456789", 1)); | |
| System.out.println(sr.wordSwap("012345", 2)); | |
| } | |
| public String wordSwap(String string, int firstWordLength) { |
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
| public boolean ensambleWords(String str, Set<String> dict) { | |
| return str.equals("") ? false : ensambleWords(str, 0, dict); | |
| } | |
| public boolean ensambleWords(String str, int start, Set<String> dict) { | |
| if (start == str.length()) { | |
| return false; | |
| } else { | |
| for (int end = start; end < str.length(); end++) { | |
| if (dict.contains(str.substring(start, end + 1)) && |
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.Arrays; | |
| import java.util.HashSet; | |
| import java.util.Random; | |
| import java.util.Set; | |
| public class FindUniqueBinarySearch { | |
| public static void main(String[] argv) { | |
| FindUniqueBinarySearch bs = new FindUniqueBinarySearch(); | |
| Set <Integer> set = new HashSet<Integer>(); | |
| Random rnd = new Random(); |
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
| public class Solution { | |
| public boolean search(int [] A, int val) { | |
| int low = 0; | |
| int high = A.length - 1; // inclusive | |
| while (low <= high) { | |
| int mid = (low + high) / 2; | |
| if (A[mid] == val) { | |
| return true; | |
| } else if (A[low] < A[mid]) { |
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
| public int search(int[] A, int target) { | |
| int low = 0; | |
| int high = A.length - 1; | |
| while (low <= high) { | |
| int mid = (high + low) / 2; | |
| if (A[mid] == target) { | |
| return mid; | |
| } else if (A[low] <= A[mid]) { | |
| if (A[low] <= target && target < A[mid]) { |
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
| public int binarySearch(int []num, int target) { | |
| int min = 0; | |
| int max = num.length - 1; | |
| while (min <= max) { | |
| int mid = min + (max - min)/2; | |
| if (num[mid] == target) { | |
| return mid; | |
| } else if (target < num[mid]) { | |
| max = mid - 1; |
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.Comparator; | |
| public class IncreasingDecreasingBinarySearch { | |
| public static void main(String []argv) { | |
| IncreasingDecreasingBinarySearch idbs = new IncreasingDecreasingBinarySearch(); | |
| int []num = new int[]{1, 3, 4, 5, 7, 8, 2, 0}; | |
| System.out.println(num[idbs.search(num, 1)]); | |
| System.out.println(num[idbs.search(num, 2)]); |
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
| public class Solution { | |
| public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] S) { | |
| Arrays.sort(S); | |
| ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>(); | |
| boolean []selected = new boolean[S.length]; | |
| subset(S, 0, selected, ans); | |
| return ans; | |
| } |
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.Comparator; | |
| import java.util.PriorityQueue; | |
| public class MedianHeap { | |
| PriorityQueue <Integer> lower = null; // max heap | |
| PriorityQueue <Integer> upper = null; // min heap | |
| float median; | |
| public MedianHeap() { | |
| lower = new PriorityQueue <Integer>(16, new Comparator<Integer>() { |