一个字符串,找出这个字符串的第一对重复的字符
如 abcdefgjikuoa 第一对重复的字符是’a’
如 abcdegfeter 第一对重复的字符 ‘e’
具体做法是:读取字符串里的第一个字符,将字符对应的ASC码值作为数组下标,并将ASC码值作为内容存入该下标对应的字符数组元素中,当读取下个字符时判断该字符的ASC码下标对应的字符串元素中是否有值(ASC码值不等于0),没有值时付上对应的ASC码值,有值时,返回该值,此ASC码对应的字符即为第一个重复的字符。时间复杂度为O(n),简单吧。具体代码如下(可以运行的哦):
#include
#include
#define STRSIZE 256
char eFind( char * , int );
int
main( int argc, char **argv )
{
char strSample_a[] = “abcdefgjikuoa”;
char strSample_b[] = “abcdegfeter”;
printf( “First same letter in \”%s\” is : %c.\n”, strSample_a, eFind( strSample_a, sizeof(strSample_a) ) );
printf( “First same letter in \”%s\” is : %c.\n”, strSample_b, eFind( strSample_b, sizeof(strSample_b) ) );
return 0;
}
char
eFind( char* strSample, int iSize )
{
int iLoop = 0;
int iAsc = 0;
char cTemp = 0;
char strFind[ STRSIZE ];
/* Init */
memset( (void *)strFind, 0×00, STRSIZE * sizeof(char) );
for (iLoop = 0; iLoop < iSize; iLoop ++)
{
/* Get the AscII from string */
iAsc = (int)strSample[ iLoop ] ;
if (strFind[ iAsc ] == 0)
{
strFind[ iAsc ] = iAsc;
} else {
/* Find the same letter */
cTemp = strFind[ iAsc ];
break;
}
}
return cTemp;
}
第二个答案:
建一个26个字母的hash表,字母名作为key(”a”、”b”…),所有的volue初始化为0。
字符串遍历,每个字母去hash表取值,取得到值的,就是第一对重复的字母了。
上代码:
public static void main(String[] fuck){
String s = "duasdasda";
String re=null;
Map map = new HashMap();
//char[] ch = s.toCharArray();
for(int i=0;i String key = s.substring(i, i+1);
if(map.get(key)!=null){
re = key;
break;
}else{
map.put(key, 0);
}
}
if(re!=null){
System.out.println("The char is " + re);
}else{
System.out.println("There is no char!");
}
}
第三个答案:
找个hash map往里存,如果发现已经有值了就是找到了第一对,时间复杂度o(n)
JAVA版本
import java.util.HashMap;
public class QuickFind {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap hashMap=new HashMap();
String str="rbcdefabc";
int len=str.length();
for(int i=0;i {
char c=str.charAt(i);
if(hashMap.get(c)!=null)
{
System.out.println("found first match character is \""+c+"\"");
break;
}
else
{
hashMap.put(c, new Object());
}
}
return;
}
}
c版本
#define HASH_BUCKET_SIZE 128
static int hashBucket[HASH_BUCKET_SIZE];
int easyHashMap(char aChar)
{
int ret=0;
if(hashBucket[aChar]==0)//not a match one
{
hashBucket[aChar]=1;
ret=0;
}//if
else//got a match one
{
ret=1;
}//else
return ret;
}
int main()
{
char* str="abcbadefg";
//initial hash bucket
memset(hashBucket,0,HASH_BUCKET_SIZE*sizeof(int));
int len=strlen(str);
//check loop
for(int i=0;i
{
if(easyHashMap(str[i]))
{
printf("found first match character pair \"%c\"\n",str[i]);
break;
}//if
}//for
return 0;
}
第四个答案:
空间时间复杂度均为O(n), 具体就是1* n。对这个算法题来讲, 肯定是最高效的了。
代码如下, 可以跑的哦
JUDEGE_TABLE = [0]*24
str = "abcdegfeter".split(//)
DICT = ("a".."z").inject(Hash.new){ |h,e| {e => e[0]-97}.merge(h) }
result = nil
str.each{|e|
if(i=JUDEGE_TABLE[DICT[e]])>0
result = e
break
end
JUDEGE_TABLE[DICT[e]]=1
}
puts result
第五个答案:
可以使用搜索二叉树
定义节点
public class Node {
private char value;
private Node rightNode;
private Node leftNode;
public Node(){
}
public Node(char value){
this.value=value;
}
public Node(char value, Node rightNode, Node leftNode) {
super();
this.value = value;
this.rightNode = rightNode;
this.leftNode = leftNode;
}
public char getValue() {
return value;
}
public void setValue(char value) {
this.value = value;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
t