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.Scanner; | |
public class UniqueString { | |
public static boolean unique(String s) { | |
if (s == null) return false; | |
boolean[] flag = new boolean[256]; | |
for (int i = 0; i < s.length(); i++) { | |
if (flag[s.charAt(i)]) | |
return false; | |
else |
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.Scanner; | |
import java.util.HashSet; | |
public class UniqueStringHashSet { | |
public static boolean unique(String s) { | |
HashSet<Character> set = new HashSet<Character>(); | |
for (int i = 0; i < s.length(); i++) { | |
if (!set.add(s.charAt(i))) | |
return false; |
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
#include <iostream> | |
using namespace std; | |
void reverseString(char *str) { | |
char *start = str; | |
for (;*str;str++); //loop to '\0' terminal of the string | |
if (*start) str--; //in case string is blank | |
while (start < str) { | |
*start ^= *str; //swap two elements using XOR | |
*str ^= *start; |
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.HashMap; | |
public class permutation { | |
public static boolean isPermut(String a, String b) { | |
if (a == null || b == null) throw new NullPointerException(); | |
if (a.length() != b.length()) return false; | |
HashMap<Character, Integer> aMap = new HashMap<Character, Integer>(); | |
HashMap<Character, Integer> bMap = new HashMap<Character, Integer>(); | |
traverse(a, aMap); |
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 replace { | |
public static String replaceBlank (String s) { | |
if(s == null) return null; | |
int count = 0; | |
for (int i = 0; i < s.length(); i++) | |
if (s.charAt(i) == ' ') count++; | |
char[] newStr = new char[s.length() + count << 1]; | |
for (int i = 0 , j = 0; i < s.length() ; i++) { | |
if (s.charAt(i) == ' ') { |
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 compressStr { | |
public static String compress(String s) { | |
StringBuilder sb = new StringBuilder(); | |
int idx = 0; | |
int l = s.length(); | |
boolean first = true; | |
int count = 0; | |
while (idx < l) { | |
if (first) { |
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 Image { | |
private int N; | |
private int[][] img; | |
public Image(int[][] image) { | |
if (image == null) throw new NullPointerException(); | |
this.img = image; | |
if (image.length != image[0].length) throw new IllegalArgumentException(); | |
this.N = image[0].length; | |
} |
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
//* Time O(MN) | |
// Use two extra boolean arrays to record which rows and cols should be set to 0. | |
//* Space O(M+N) | |
public class Solution{ | |
private int[][] matrix; | |
private int M; | |
private int N; | |
public Solution(int[][] m) { |
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 isRotation { | |
public static boolean isRotate(String s1, String s2) { | |
if (s1.length() != s2.length()) return false; | |
return isSubstring(s2, s1.concat(s1)); | |
} | |
public static boolean isSubstring(String s1, String s2) { |
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.LinkedList; | |
import java.util.HashSet; | |
public class rmDuplicate { | |
//* why return a new LinkedList rather than modifiying the original one? | |
//* remove(object) in LinkedList(Java) could be O(N) in worst case (i.e. closer to tail of the LinkedList) | |
//* for LinkedList [1,1,...,1,1,1] (length N), remove dupicate elements would cost (1+2+...+N-1) ~ O(N^2) | |
//* write your own LinkedList where you can manipulate the reference could solve the problem | |
public static<Item> LinkedList<Item> rm (LinkedList<Item> list) { |
OlderNewer