Skip to content

Instantly share code, notes, and snippets.

@zac-xin
zac-xin / TestUnique.java
Created April 16, 2012 12:28
1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
public class TestUnique {
/* use a array to record the occurrence of different characters
* first method uses extra space
* NOTE: the spaces will cause duplicates too
* the time complexity is O(n), the space complexity is also O(n)
*/
public static boolean test(String input){
boolean char_set[] = new boolean[255];
@zac-xin
zac-xin / StringReverse.java
Created April 17, 2012 07:41
1.2 Write code to reverse a C-Style String. (C-String means that “abcd” is represented as !ve characters, including the null character.)
public class StringReverse {
public static void main(String args[]){
String s = "this is a demo test!";
System.out.println(reverse(s));
}
public static String reverse(String input){
char[] data = input.toCharArray();
int i = 0;
int j = data.length - 1;
@zac-xin
zac-xin / RemoveDuplicates.java
Created April 18, 2012 11:32
1.3 Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer.
public class RemoveDuplicates {
public static void main(String args[]){
String s = "1234512345aabbbccccddddddeeefffffggg";
char[] input = s.toCharArray();
removeDup(input);
System.out.println(input);
}
public static void removeDup(char[] data){
int len = data.length;
@zac-xin
zac-xin / TestAnagram.java
Created April 18, 2012 13:03
1.4 Write a method to decide if two strings are anagrams or not.
public class TestAnagram {
public static void main(String args[]){
String a = "tomcat as";
String b = " tamcots a";
System.out.println(test(a, b));
}
public static boolean test(String s1, String s2){
if(s1.length() != s2.length())
return false;
@zac-xin
zac-xin / ReplaceSpace.java
Created April 18, 2012 13:21
1.5 Write a method to replace all spaces in a string with ‘%20’
public class ReplaceSpace {
public static void main(String args[]){
System.out.println(replaceSpace("this is a demo test! "));
}
public static String replaceSpace(String input){
char s[] = input.toCharArray();
int spaceCount = 0;
int index = 0;
@zac-xin
zac-xin / RotateMatrix.java
Created April 20, 2012 08:09
1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
public class RotateMatrix {
public static void main(String args[]){
int[][] matrix = {
{1, 2, 3 ,4 ,5 ,6},
{12, 13, 14, 15, 16, 17},
{23, 24, 25, 26, 27, 28},
{34, 35, 36, 37, 38, 39},
{45, 46, 47, 48, 49, 50},
{66, 67, 68, 69, 70, 71}
};
@zac-xin
zac-xin / SetZero.java
Created April 20, 2012 08:52
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0
public class SetZero {
public static void main(String args[]){
int[][] matrix = {
{1, 2, 3 ,4 ,5 ,6},
{12, 13, 0, 15, 16, 17},
{23, 24, 25, 26, 27, 28},
{34, 35, 36, 0, 38, 39},
{45, 46, 47, 48, 49, 50},
{66, 0, 68, 69, 1, 71},
{33, 46, 47, 48, 49, 50}
/*
* Assume you have a method isSubstring which checks if one word is a substring of
* another.
* Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using
* only one call to isSubstring (i e , “waterbottle” is a rotation of “erbottlewat”)
*/
public class SubString {
public static void main(String args[]){
@zac-xin
zac-xin / LinkedList.java
Created April 27, 2012 09:25
2.1 Remove duplicates in a linkedlist
import java.util.Iterator;
import java.util.HashSet;
import java.util.NoSuchElementException;
/*
* A LinkedList class with a private static inner node class
* based on:
* http://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html
*
@zac-xin
zac-xin / findLastNth.java
Created April 27, 2012 09:42
2.2 Implement an algorithm to !nd the nth to last element of a singly linked list
public AnyType findLastNth(int k){
if(head == null || k <= 0)
return null;
if(size() < k)
return null;
Node<AnyType> tmp = head;
for(int i = 0; i < k; i++)
tmp = tmp.next;