首页 > 其他分享 >leetcode-1880-easy

leetcode-1880-easy

时间:2022-11-03 08:23:57浏览次数:40  
标签:arr easy val int String letter firstWord leetcode 1880

Check if Word Equals Summation of Two Words

The letter value of a letter is its position in the alphabet starting from 0 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, etc.).

The numerical value of some string of lowercase English letters s is the concatenation of the letter values of each letter in s, which is then converted into an integer.

For example, if s = "acb", we concatenate each letter's letter value, resulting in "021". After converting it, we get 21.
You are given three strings firstWord, secondWord, and targetWord, each consisting of lowercase English letters 'a' through 'j' inclusive.

Return true if the summation of the numerical values of firstWord and secondWord equals the numerical value of targetWord, or false otherwise.

思路一: 用一个 26 长度的数组映射 a-z 的值,然后计算字符串对应的值即可

public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
	int[] arr = new int[26];
	for (char i = 0; i < 26; i++) {
		arr[i] = i;
	}

	return val(arr, firstWord) + val(arr, secondWord) == val(arr, targetWord);
}

public int val(int[] arr, String str) {
	int sum = 0;
	int n = 1;
	for (int i = str.length() - 1; i >= 0; i--) {
		sum += arr[str.charAt(i) - 'a'] * n;
		n *= 10;
	}

	return sum;
}

标签:arr,easy,val,int,String,letter,firstWord,leetcode,1880
From: https://www.cnblogs.com/iyiluo/p/16853174.html

相关文章

  • leetcode-566-easy
    ReshapetheMatrixInMATLAB,thereisahandyfunctioncalledreshapewhichcanreshapeanmxnmatrixintoanewonewithadifferentsizerxckeepingits......
  • LeetCode刷题记录.Day4
    移除链表元素题目链接203.移除链表元素-力扣(LeetCode)classSolution{public:ListNode*removeElements(ListNode*head,intval){ListNode*varHe......
  • leetcode-104. 二叉树的最大深度
    题目描述给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,......
  • 【leetcode 952. 按公因数计算最大组件大小】【欧拉筛+并查集】
    importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;classSolution{List<Integer>list=newArrayList<>();intprimeNum=0......
  • Oeasyvim - 如打怪一般的学习
    Oeasyvim-如打怪一般的学习项目地址:https://github.com/TonyK922/oeasy-vim-tutorial这是overmind1980做的一个vim入手到进阶的教程很适合vim初学者.项目共98章......
  • leetcode-67. 二进制求和
    题目描述给你两个二进制字符串a和b,以二进制字符串的形式返回它们的和。示例输入:a="11",b="1"输出:"100"思路分析我们可以先将其转化为整数,相加之后再转为......
  • leetcode股票系列问题
    本文整合了一些大佬的文章加上自己的一些认识,供自己复习转载:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/solutions/8753/yi-ge-fang-fa-tuan-mie-6-d......
  • AI智能检测识别EasyCVR视频融合平台告警抓拍图片的逻辑优化
    将智能分析网关的AI智能识别能力与EasyCVR视频融合平台的视频服务能力融合,构建基于云边端协同架构的安全风险监测与视频监管平台,可对接入的多路视频流进行智能检测、智能识......
  • EasyCVR平台https协议用不了rtc,WebRTC视频无法播放该如何解决?
    EasyCVR平台可支持多类型设备、多协议方式接入,具体包括:国标GB28181协议、RTMP、RTSP/Onvif、海康Ehome,以及海康SDK、大华SDK、华为SDK、宇视SDK、乐橙SDK、萤石SDK等,可覆盖......
  • LeetCode刷题第一周
    数组:内存空间连续,数据类型统一,下标从0开始二分查找704classSolution{publicintsearch(int[]nums,inttarget){//方法一:暴力解法//for......