1662. 检查两个字符串数组是否相等-while单层循环解决

1662. 检查两个字符串数组是否相等-while单层循环解决

给你两个字符串数组 word1 和 word2 。如果两个数组表示的字符串相同,返回 true ;否则,返回 false 。

数组表示的字符串 是由数组中的所有元素 按顺序 连接形成的字符串。

示例 1:

输入:word1 = [“ab”, “c”], word2 = [“a”, “bc”]
输出:true
解释:
word1 表示的字符串为 “ab” + “c” -> “abc”
word2 表示的字符串为 “a” + “bc” -> “abc”
两个字符串相同,返回 true

示例 2:

输入:word1 = [“a”, “cb”], word2 = [“ab”, “c”]
输出:false

示例 3:

输入:word1 = [“abc”, “d”, “defg”], word2 = [“abcddefg”]
输出:true

对于这一题,博主写了一个很棒的代码,非常简洁易懂,代码如下,感兴趣的可以学习学习:

bool arrayStringsAreEqual(char ** word1, int word1Size, char ** word2, int word2Size){
   int word1_index=0,word2_index=0;
   int w1po=0,w2po=0;
    while(true){
        if(word1[word1_index][w1po]=='\0'){
            word1_index++;
            w1po=0;
        }
        if(word2[word2_index][w2po]=='\0'){
            word2_index++;
            w2po=0;
        }
        if(word1_index==word1Size||word2_index==word2Size){
            break;
        }
        if(word1[word1_index][w1po]==word2[word2_index][w2po]){
            w1po++;
            w2po++;
        }
        else{
            return false;
        }
    }
     if(word1_index==word1Size&&word2_index==word2Size){
           return true;
        }
    return false;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值