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
/* | |
Implement an algorithm to determine if a string has all unique characters what if you can't use additional data structure | |
Date: 08/26/2014 | |
*/ | |
class solution{ | |
// Time complexity O(n^2), Space complexity O(1) | |
public boolean check( String s ){ | |
boolean result = true; | |
if( s.length() < 2 )// include string is empty | |
result = true; |
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
class unique{ | |
public boolean check(String s){ | |
if(s.length == 0) | |
return true; | |
String sorted = Sort(s); | |
for(int i = 0; i < sorted.length - 1 ; i ++){ | |
if ( sorted.charAt( i ) == sorted.charAt( i + 1 )) | |
return 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
/**Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string. | |
Time complexity O(n), space complexity O(1) | |
Date: 08/27/2014 | |
*/ | |
void swap(char* a, char* b){ | |
char c = *a; | |
*a = *b; | |
*b = c; | |
} |
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
/*1.3 Given two strings, write a method to decide if one is a permutation of the other. | |
space complexity: | |
time complexity: | |
date: 09/01/2014 | |
*/ | |
public class permutationString{ | |
public static boolean permutation(String s1, String s2){ | |
if(s1 ==NULL || s2 == NULL) | |
return false; | |
if(s1.isEmpty()&& s2.isEmpty()) |
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
/*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%20John%20Smith" | |
Time complexity: O(n) | |
Space complexity: O(1) | |
Date: 09/02/2014*/ | |
public class replaceSpace{ | |
public void replace(char[] str){ | |
int len = strlen(str); |
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
/*implement a method to perform basic string compression using the | |
counts of repeated characters. For example, the string aabcccccaaa would | |
become a2b1c5a3. If the "compressed" string would not become smaller than | |
the original string, your method should return the original string. | |
Time complexity: O(n) | |
Space complexity: O(n) | |
Date: 09/02/2014 | |
*/ | |
public class compressed{ |
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
public static int[][] setMatrixzero(int[][] matrix){ | |
int M = matrix.length; | |
int N = matrix[0].length; | |
boolean setRowZero[] = new boolean [M]; | |
boolean setColumnZero[] = new boolean[N]; | |
for(int i = 0; i < M; i++){ | |
for(int j=0; j<N; j++) | |
if(matrix[i][j] ==0){ | |
setRowZero[i]=0; |
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
/* | |
Date: 09/14/2014 | |
Time Complexity: | |
Space Complexity: | |
*/ | |
public class solution{ | |
public static boolean isSubString(String s1, String s2){ | |
if(s1.contain(s2)) | |
return true; |
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
class deleteDup{ | |
public void deleteDup(LinkNode* head){ | |
if(head == NULL) | |
return; | |
LinkNode* curr = head; | |
while(curr!=NULL){ | |
LinkNode runner = curr; | |
while(runner.next!=NULL){ | |
if(runner.next.value == curr.value){ | |
runner.next = runner.next.next; |
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
/* | |
2.2 Implement an algorithm to find the kth to last element of a singly linked list. | |
*/ | |
public class findElement{ | |
public int findElement(LinkNode* head,int k){ | |
if(head == NULL) | |
return; | |
LinkNode* curr = head; | |
LinkNode* kStep = head; |
OlderNewer