careercup1.3: remove duplicate without extra space.
2013-01-31 11:38
113 查看
First, ask yourself, what does the interviewer mean by an additional bu#er? Can we use an
additional array of constant size?
Algorithm—No (Large) Additional Memory:
1. For each character, check if it is a duplicate of already found characters.
2. Skip duplicate characters and update the non duplicate characters.
Time complexity is O(N2)
Test Cases:
1. String does not contain any duplicates, e.g.: abcd
2. String contains all duplicates, e.g.: aaaa
3. Null string
4. String with all continuous duplicates, e.g.: aaabbb
additional array of constant size?
Algorithm—No (Large) Additional Memory:
1. For each character, check if it is a duplicate of already found characters.
2. Skip duplicate characters and update the non duplicate characters.
Time complexity is O(N2)
Test Cases:
1. String does not contain any duplicates, e.g.: abcd
2. String contains all duplicates, e.g.: aaaa
3. Null string
4. String with all continuous duplicates, e.g.: aaabbb
package careercup; import java.util.Arrays; public class test1 { public static String checkUnique(char[] s) { if(s.length<=1) return new String(s); int tail = 1; for(int i=1; i<s.length; i++){ int j; for(j=0; j<tail; j++){ if(s[i]==s[j]){ break; } } if(j==tail){ s[tail++] = s[i]; } } return new String(s, 0, tail); } public static void main(String[] args){ String s = "abcafasdfgasghfgjkghkfgkffasfasf"; System.out.println( checkUnique(s.toCharArray()) ); } }
相关文章推荐
- careercup1.3: remove duplicate without extra space.
- CareerCup Convert a BST to max heap without using extra memory and in as optimum time as possible
- CareerCup calculate (x^y)%z without pow();
- Determine whether an integer is a palindrome. Do this without extra space.
- Swap values of two variables without extra space
- [CrackCode] 1.3 Remove the duplicate characters in a string
- 【LeetCode】 Determine whether an integer is a palindrome. Do this without extra space
- Determine whether an integer is a palindrome. Do this without extra space.
- Determine whether an integer is a palindrome. Do this without extra space.
- How do you remove the duplicate characters in a given string without using any additional buffer.
- CH1-3: remove duplicate char in a string, with/without additional buffer
- Determine whether an integer is a palindrome. Do this without extra space.
- CareerCup 1.3
- 【leetcode c++】26 Remove Duplicates from Sorted Array
- leetcode question 26:Remove Duplicates from Sorted Array
- LeetCode Remove Duplicates from Sorted Array
- leetcode-Remove Duplicates from Sorted List II
- leetcode系列(3)--Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List
- Easy 18 Remove Duplicates from Sorted List(83)