【3】Remove the duplicate characters in a string
2013-04-09 21:47
447 查看
Question: Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional
variables are fine An extra copy of the array is not.
package CareerCup;
public class RemoveDuplicateChar
{
public RemoveDuplicateChar(){}
public String removeDuplicate(String str)
{
if(str.length()<=1 || str==null) return str;
String strRemove = "";
char ch = str.charAt(0);
strRemove += str.charAt(0);
for(int i=1;i<str.length();i++)
{
if(str.charAt(i)!=ch)
{
strRemove += str.charAt(i);
ch = str.charAt(i);
}
}
return strRemove;
}
public static void main(String[] args)
{
String str = "seeyeeeey";
RemoveDuplicateChar rdc = new RemoveDuplicateChar();
String strRemove = rdc.removeDuplicate(str);
System.out.println("The orignal string:"+str);
System.out.println("The removed string:"+strRemove);
}
}
variables are fine An extra copy of the array is not.
package CareerCup;
public class RemoveDuplicateChar
{
public RemoveDuplicateChar(){}
public String removeDuplicate(String str)
{
if(str.length()<=1 || str==null) return str;
String strRemove = "";
char ch = str.charAt(0);
strRemove += str.charAt(0);
for(int i=1;i<str.length();i++)
{
if(str.charAt(i)!=ch)
{
strRemove += str.charAt(i);
ch = str.charAt(i);
}
}
return strRemove;
}
public static void main(String[] args)
{
String str = "seeyeeeey";
RemoveDuplicateChar rdc = new RemoveDuplicateChar();
String strRemove = rdc.removeDuplicate(str);
System.out.println("The orignal string:"+str);
System.out.println("The removed string:"+strRemove);
}
}
相关文章推荐
- How do you remove the duplicate characters in a given string without using any additional buffer.
- [CrackCode] 1.3 Remove the duplicate characters in a string
- //Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the in
- A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Gi
- How to remove the duplicated value in a String array
- CH1-3: remove duplicate char in a string, with/without additional buffer
- python: remove the duplicates in a list
- Find the longest substring with k unique distinct characters in a given string (Google)
- How to remove the component VOB in Components?
- Given a string S, find the longest palindromic substring in S.
- IOS 字符串中去除特殊符号 stringByTrimmingCharactersInSet 应该用于账号登录等
- iOS 字符串中去除特殊符号 stringByTrimmingCharactersInSet
- Is the Improvement of String.substring() in Java7 Really Reasonable?
- chang color of some characters in a string
- [Leetcode] Remove Duplicates in Sorted Array
- 指定字符串与主题所要求的形式不符The specified string is not in the form required for a subject
- SmtpClient发送邮件遭遇The specified string is not in the form required for a subject.
- Remove the empty xmlns attribute in generated xml
- Remove duplicates in a sorted array
- SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data