Skip to content

Instantly share code, notes, and snippets.

@WOLOHAHA
WOLOHAHA / CC150-1.1.java
Last active August 29, 2015 14:02
Implement an algorithm to determine if a string has all unique characters. Whatif you cannot use additional data structures?
public boolean uniqueChar(String s) {
if (s == null)
throw new IllegalArgumentException();
if (s.length() <= 1)
return true;
if (s.length() > 256)
return false;
boolean[] iFlag = new boolean[256];
for (int i = 0; i < s.length(); i++) {
@WOLOHAHA
WOLOHAHA / CC150-1.2.java
Last active August 29, 2015 14:02
Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Main so = new Main();
so.reverse("abcdefghijk");
}
@WOLOHAHA
WOLOHAHA / CC150-1.3.java
Last active August 29, 2015 14:02
Given two strings, write a method to decide if one is a permutation of the other.
// Ask the interviewer about details: are the characters case sensitive,
// are the white space and special characters significant?
/**
* Solution 1
* Assume the string falls under a certain encoding.
* Check if the two strings have the same character count.
* Use an extra constant
* size array of integers to store the frequency of each character in str1,
* for each character in str2, minus the frequency array's value by 1. In
@WOLOHAHA
WOLOHAHA / CC150-1.4.java
Last active August 29, 2015 14:03
Write a method to replace all spaces in a string with'%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.) EXAMPLE …
public class Main {
/**
* @* Runtime & space
* runtime: O(n)
* space: O(1)
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Main so = new Main();
@WOLOHAHA
WOLOHAHA / CC150-1.5.java
Last active August 29, 2015 14:03
Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string.
public class Main {
/**
* @Runtime & spaces ||runtime: O(n) ||space: O(n)
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Main so = new Main();
String s = "a";
System.out.println(so.compress(s));
@WOLOHAHA
WOLOHAHA / CC150-1.6.java
Last active August 29, 2015 14:03
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?
/**
* 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?
* @Runtime & spaces
* runtime: O(n^2)
* space: O(1)
* Assumption: the rotation is clockwise.
*/
@WOLOHAHA
WOLOHAHA / CC150-1.7.java
Last active August 29, 2015 14:03
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
/**
* 1.7 Write an algorithm such that if
* an element in an MxN matrix is 0, its
* entire row and column are set to 0.
*
* @Runtime & spaces
* runtime: O(mn)
* space: O(1)
*/
//Solution: idea got from zhenzhenanan
@WOLOHAHA
WOLOHAHA / CC150-1.8.java
Last active August 29, 2015 14:03
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 (e.g.,"waterbottle"is a rotation of"erbottlewat").
public class Main {
/**
* 1.8 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 (e.g.,"waterbottle"is
* a rotation of"erbottlewat").
*
* Solution: just the same as CTCI
@WOLOHAHA
WOLOHAHA / CC150-2.1.java
Last active August 29, 2015 14:03
Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed?
/**
* 2.1 Write code to remove duplicates from an unsorted linked list.
* FOLLOW UP
* How would you solve this problem if a temporary buffer is not allowed?
*
* Solution1:
* @Runtime & spaces
* runtime: O(n)
* space: O(n)
*
@WOLOHAHA
WOLOHAHA / CC150-2.2.java
Last active August 29, 2015 14:03
Implement an algorithm to find the kth to last element of a singly linked list.
public class Main {
/**
* 2.2 Implement an algorithm to find the k th
* to last element of a singly linked list.
*
* we have defined k such that passing in k = 1 would return
* the last element,k = 2 would return to the second to last
* element,and soon.It is equally acceptable to define k such
* that k = 0 would return the last element.