This file contains 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
// Cracking the code interview | |
// 1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? | |
class UniqueCharacters { | |
public static boolean hasUniqueCharacters(String s) { | |
if (s == null || s.isEmpty()) return true; | |
int len = s.length(); | |
if (len > 256) return false; | |
boolean[] hash = new boolean[256]; |
This file contains 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
// Cracking the Coding Interview | |
// 1.2 Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string. | |
#include <assert.h> | |
#include <iostream> | |
void reverse(char* str) { | |
int len = 0; | |
char *cur = str; | |
char tmp = ' '; | |
if (cur == NULL) return; |
This file contains 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
// Cracking the Coding Interview | |
// 1.3 Given two strings, write a method to decide if one is a permutation of the other. | |
class Permutation { | |
public static boolean isPermuation(String str1, String str2) { | |
if (str1 == str2) return true; | |
if (str1 == null || str2 == null || str1.length() != str2.length()) return false; | |
int buf[] = new int[256]; | |
int len = str1.length(); | |
for(int i = 0; i< len; i++) { |
This file contains 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
// Craking the Coding Interview | |
// 1.4 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 | |
// Input: "Mr John Smith " | |
// Output: "Mr%20Dohn%20Smith" | |
import java.util.Arrays; | |
class SpaceReplacement { | |
public static char[] replaceSpace(char[] arr, int len) { | |
if (arr == null || len <= 0) return arr; | |
// calculate space number |
This file contains 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
// Cracking the Coding Interview | |
// 1.5 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. | |
class StringCompress { | |
public static String compressString(String s) { | |
if (s == null) return null; | |
boolean needCompressing = false; |
This file contains 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
// Cracking the Coding Interview | |
// 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? | |
import java.util.Arrays; | |
class MatrixRotation { | |
// assuming the matrix is NxN | |
public static void rotateMatrix(int[][] m) { | |
if (m == null || m.length <= 1) return; | |
int len = m.length; |
This file contains 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
// Cracking the Coding Interview | |
// 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. | |
import java.util.Arrays; | |
class MatrixZeros { | |
public static void setZeros(int[][] m) { | |
if (m.length <= 1 && m[0].length <= 1) return; | |
boolean zeroAtFirstRow = false; | |
boolean zeroAtFirstColumn = false; | |
// check first row |
This file contains 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
// CC_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? | |
class Node { | |
public Node next; | |
public int val; | |
Node(int _v) { |
This file contains 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
// Cracking the Coding Interview | |
// 2.2 Implement an algorithm to find the kth to last element of a singly linked list. | |
class Node { | |
public Node next; | |
public int val; | |
Node(int _v) { | |
val = _v; | |
} | |
} |
This file contains 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
// Cracking the Coding Interview | |
// 2.3 | |
// Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node. | |
// EXAMPLE | |
// Input: the node c from the linked list a->b->c->d->e | |
// Result: nothing isreturned, but the new linked list looks like a- >b- >d->e | |
class Node { | |
public Node next; | |
public int val; |
OlderNewer