5772.检查某单词是否等于两单词之和

5772.检查某单词是否等于两单词之和

字母的 字母值 取决于字母在字母表中的位置,从 0 开始 计数。即,'a' -> 0'b' -> 1'c' -> 2,以此类推。

对某个由小写字母组成的字符串 s 而言,其 数值 就等于将 s 中每个字母的 字母值 按顺序 连接转换 成对应整数。

  • 例如,s = "acb" ,依次连接每个字母的字母值可以得到 "021" ,转换为整数得到 21

给你三个字符串 firstWordsecondWordtargetWord ,每个字符串都由从 'a''j' 'a''j' )的小写英文字母组成。

如果 firstWordsecondWord数值之和 等于 targetWord 的数值,返回 true ;否则,返回 false

示例 1:

输入:firstWord = "acb", secondWord = "cba", targetWord = "cdb"
输出:true
解释:
firstWord 的数值为 "acb" -> "021" -> 21
secondWord 的数值为 "cba" -> "210" -> 210
targetWord 的数值为 "cdb" -> "231" -> 231
由于 21 + 210 == 231 ,返回 true

示例 2:

输入:firstWord = "aaa", secondWord = "a", targetWord = "aab"
输出:false
解释:
firstWord 的数值为 "aaa" -> "000" -> 0
secondWord 的数值为 "a" -> "0" -> 0
targetWord 的数值为 "aab" -> "001" -> 1
由于 0 + 0 != 1 ,返回 false

示例 3:

输入:firstWord = "aaa", secondWord = "a", targetWord = "aaaa"
输出:true
解释:
firstWord 的数值为 "aaa" -> "000" -> 0
secondWord 的数值为 "a" -> "0" -> 0
targetWord 的数值为 "aaaa" -> "0000" -> 0
由于 0 + 0 == 0 ,返回 true

提示:

  • 1 <= firstWord.length, ``secondWord.length, ``targetWord.length <= 8
  • firstWordsecondWordtargetWord 仅由从 'a''j' 'a''j' )的小写英文字母组成。

代码与思路

#include <iostream>
#include <string>

using namespace std;

class Solution {
public:
	//某菜的
    bool isSumEqual(string firstWord, string secondWord, string targetWord) {
        int a=0,b=0,c=0;
        for(int i=0;i<firstWord.length();i++){
            int tmp = (firstWord[i]-'a');
            a = a*10 + tmp;
        }
        for(int i=0;i<secondWord.length();i++){
            int tmp = (secondWord[i]-'a');
            b = b*10 + tmp;
        }
        for(int i=0;i<targetWord.length();i++){
            int tmp = (targetWord[i]-'a');
            c = c*10 + tmp;
        }
        return c-a==b;
    }
    //官方的
    bool isSumEqual_2(string firstWord, string secondWord, string targetWord) {
        auto decode = [](const string& s)->int{
            int res = 0;
            for(char ch:s){
                res *=10;
                res +=ch-'a';
            }
            return res;
        };
        return decode(firstWord)+decode(secondWord)==decode(targetWord);
    }
};

int main()
{
    Solution s;
    string firstWord="acb";
    string secondWord="cba";
    string targetWord="cdb";
    cout << s.isSumEqual(firstWord,secondWord,targetWord) << endl;
}

其它

C++ 11 Lambda表达式

声明Lambda表达式
Lambda表达式完整的声明格式如下:

[capture list] (params list) mutable exception-> return type { function body }

各项具体含义如下

  1. capture list:捕获外部变量列表
  2. params list:形参列表
  3. mutable指示符:用来说用是否可以修改捕获的变量
  4. exception:异常设定
  5. return type:返回类型
  6. function body:函数体

此外,我们还可以省略其中的某些成分来声明“不完整”的Lambda表达式,常见的有以下几种:

序号格式
1[capture list] (params list) -> return type {function body}
2[capture list] (params list) {function body}
3[capture list] {function body}

其中:

  • 格式1声明了const类型的表达式,这种类型的表达式不能修改捕获列表中的值。
  • 格式2省略了返回值类型,但编译器可以根据以下规则推断出Lambda表达式的返回类型: (1):如果function body中存在return语句,则该Lambda表达式的返回类型由return语句的返回类型确定; (2):如果function body中没有return语句,则返回值为void类型。
  • 格式3中省略了参数列表,类似普通函数中的无参函数。

C++ 11 Lambda表达式 参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值