Skip to content

Instantly share code, notes, and snippets.

View raymondino's full-sized avatar
keep up all the good work!

Rui Yan raymondino

keep up all the good work!
View GitHub Profile
@raymondino
raymondino / cc2.7
Created August 1, 2017 19:42
check if two linked lists are panlindromic
public static boolean checkPan(Node a, Node b) {
if(a == null && b == null) return true;
else if (a == null || b == null) return false;
Node ra = reverseR(a);
while(ra != null && b != null) {
if(ra.v != b.v)
return false;
else {
ra = ra.next;
b = b.next;
@raymondino
raymondino / cc2.6
Created August 1, 2017 19:23
find the starting node of the circular linked list
public static Node findCircleStart(Node n) {
if(n == null) return n;
HashSet hs = new HashSet<Integer>();
while(n != null) {
if(hs.add(n.hashCode())) {
n = n.next;
}
else
return n;
}
@raymondino
raymondino / cc2.5
Created August 1, 2017 19:02
linked list addition
// reverse add
public static Node add(Node a, Node b) {
if(a == null && b == null) return null;
if(a == null) return b;
if(b == null) return a;
Node sumHead = new Node(-1);
Node sumListPtr = sumHead;
int carry = 0;
while(a != null || b != null) {
int sum = (a == null ? 0: a.v) + (b == null ? 0: b.v) + carry;
@raymondino
raymondino / cc2.4
Created August 1, 2017 17:16
partition a list such that all elements smaller than a value is at the left side
public static Node partition(Node n, int v) {
if(n == null) return null;
Node smallHead = new Node(-1);
Node smallHeadMover = smallHead;
Node bigHead = new Node(-1);
Node bigHeaderMover = bigHead;
while(n != null) {
if(n.v < v) {
if(smallHead.next == null) {
smallHead.next = n;
@raymondino
raymondino / cc2.2
Created August 1, 2017 16:43
find the last kth element in the linked list
public static Node findLastK(Node n, int k) {
if(n == null) return null;
int counter = 0;
Node slow = n;
Node fast = n;
while(fast != null && counter <= k) {
fast = fast.next;
counter++;
}
if(counter < k) return null; // k is longer than the list size
@raymondino
raymondino / cc1.7
Created July 31, 2017 18:53
set column and row of a 0 element to be all 0
public static void setZero(int[][] m) {
if(m == null) return;
int d1 = m.length;
int d2 = m[0].length;
// get all the zero element's locations
ArrayList<Zero> z = new ArrayList<Zero>();
for(int i = 0; i < d1; i++) {
for(int j = 0; j < d2; j++) {
@raymondino
raymondino / cc1.6
Created July 31, 2017 18:37
rotate a matrix by 90 degrees (in place)
public static void rotate(int[][] m) {
if(m == null || m.length <= 1)
return;
// rotate by 90: do transponse, then flip the columns.
// transpose:
for(int i = 0; i < m.length; i++) {
for(int j = i; j < m.length; j++) {
int temp = m[i][j];
m[i][j] = m[j][i];
m[j][i] = temp;
@raymondino
raymondino / cc1.5
Created July 31, 2017 18:18
compress the string without in place
public static String compress(String t) {
int i = 0; // fast pointer
int j = 0; // slow pointer
StringBuilder sb = new StringBuilder();
while(i < t.length()) {
if(t.charAt(j) == t.charAt(i)) i++;
else {
sb.append(t.charAt(j)).append(i-j);
j = i;
}
@raymondino
raymondino / cc1.4
Created July 31, 2017 17:17
replace space with 20%
// if x.length() < t.length(), pointers start from the string tail
public static String backReplace(String x, String t) {
// null check
if(x == null || t == null) return x;
// convert x into char arrays so as to replace in place
char[] ax = new char[x.length()];
for(int i = 0; i < x.length(); i++) {
ax[i] = x.charAt(i);
}
int i = x.length() - 1; // front pointer
@raymondino
raymondino / cc1.3
Created July 31, 2017 16:35
check if two strings are permutations
// assume all the letters are small
public static boolean checkPermutation(String a, String b) {
if(a == null && b == null) return true;
else if(a == null || b == null) return false;
else if(a.length() != b.length()) return false;
else {
int[] aa = new int[26];
int[] ab = new int[26];
for(int i = 0; i < a.length(); i++) {
aa[a.charAt(i) - 'a'] ++;