构造字典序的最大合并字符串

如果 word1 非空,将 word1 中的第一个字符附加到 merge 的末尾,并将其从 word1 中移除。
    例如,word1 = "abc" 且 merge = "dv" ,在执行此选项操作之后,word1 = "bc" ,同时 merge = "dva" 。
如果 word2 非空,将 word2 中的第一个字符附加到 merge 的末尾,并将其从 word2 中移除。
    例如,word2 = "abc" 且 merge = "" ,在执行此选项操作之后,word2 = "bc" ,同时 merge = "a" 。

长度相同的两个字符串 a 和 b 比较字典序大小,如果在 a 和 b 出现不同的第一个位置,a 中字符在字母表中的出现顺序位于 b 中相应字符之后,就认为字符串 a 按字典序比字符串 b 更大。例如,“abcd” 按字典序比 “abcc” 更大,因为两个字符串出现不同的第一个位置是第四个字符,而 d 在字母表中的出现顺序位于 c 之后。

以下是代码

 

char * largestMerge(char * word1, char * word2){
    int len1=strlen(word1),len2=strlen(word2);
    char *re=(char *)malloc(sizeof(char )*(len1+len2+1));
    int index1=0,index2=0;
    int size=0;
    while(index1!=len1&&index2!=len2){
        if(word1[index1]==word2[index2]){
            if(strcmp(word1+index1,word2+index2)>=0){
                re[size++]=word1[index1++];

            }
            else{
                re[size++]=word2[index2++];

            }
            continue;
        }
        if(word1[index1]>word2[index2]){
            re[size++]=word1[index1++];

        }
        else{
              re[size++]=word2[index2++];

        }
    }
    while(index1<len1){
         re[size++]=word1[index1++];

    }
     while(index2<len2){
         re[size++]=word2[index2++];

    }
re[size]='\0';
return re;

}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值