加载中...
2023-连接后等于目标字符串的字符串对(Number of Pairs of Strings With Concatenation Equal to Target)
发表于:2021-12-03 | 分类: 中等
字数统计: 720 | 阅读时长: 3分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target

英文原文

Given an array of digit strings nums and a digit string target, return the number of pairs of indices (i, j) (where i != j) such that the concatenation of nums[i] + nums[j] equals target.

 

Example 1:

Input: nums = ["777","7","77","77"], target = "7777"
Output: 4
Explanation: Valid pairs are:
- (0, 1): "777" + "7"
- (1, 0): "7" + "777"
- (2, 3): "77" + "77"
- (3, 2): "77" + "77"

Example 2:

Input: nums = ["123","4","12","34"], target = "1234"
Output: 2
Explanation: Valid pairs are:
- (0, 1): "123" + "4"
- (2, 3): "12" + "34"

Example 3:

Input: nums = ["1","1","1"], target = "11"
Output: 6
Explanation: Valid pairs are:
- (0, 1): "1" + "1"
- (1, 0): "1" + "1"
- (0, 2): "1" + "1"
- (2, 0): "1" + "1"
- (1, 2): "1" + "1"
- (2, 1): "1" + "1"

 

Constraints:

  • 2 <= nums.length <= 100
  • 1 <= nums[i].length <= 100
  • 2 <= target.length <= 100
  • nums[i] and target consist of digits.
  • nums[i] and target do not have leading zeros.

中文题目

给你一个 数字 字符串数组 nums 和一个 数字 字符串 target ,请你返回 nums[i] + nums[j] (两个字符串连接)结果等于 target 的下标 (i, j) (需满足 i != j)的数目。

 

示例 1:

输入:nums = ["777","7","77","77"], target = "7777"
输出:4
解释:符合要求的下标对包括:
- (0, 1):"777" + "7"
- (1, 0):"7" + "777"
- (2, 3):"77" + "77"
- (3, 2):"77" + "77"

示例 2:

输入:nums = ["123","4","12","34"], target = "1234"
输出:2
解释:符合要求的下标对包括
- (0, 1):"123" + "4"
- (2, 3):"12" + "34"

示例 3:

输入:nums = ["1","1","1"], target = "11"
输出:6
解释:符合要求的下标对包括
- (0, 1):"1" + "1"
- (1, 0):"1" + "1"
- (0, 2):"1" + "1"
- (2, 0):"1" + "1"
- (1, 2):"1" + "1"
- (2, 1):"1" + "1"

 

提示:

  • 2 <= nums.length <= 100
  • 1 <= nums[i].length <= 100
  • 2 <= target.length <= 100
  • nums[i] 和 target 只包含数字。
  • nums[i] 和 target 不含有任何前导 0 。

通过代码

高赞题解

81F89CDA6EB3015C4F340F39921F2213.jpg

就按照题目描述来写

image.png

  1. i != j
  2. nums [ i ] + nums [ j ] = target ( 必须满足条件一 )

然后我就想两个for循环暴力过去,代码如下:

class Solution:
    def numOfPairs(self, nums: List[str], target: str) -> int:
        n = len ( nums )
        res = 0
        for i in range ( n ) :
            for j in range ( n ) :
                if i != j :
                    if nums [ i ] + nums [ j ] == target :
                        res += 1
        return res

跑起来的结果是我没有想到的(可以算是混过去了?)

E67202E06353BA5C4F41450A0F80D85A.jpg

题解要是有哪些地方不对的,各位大佬可以提出来;若是有不同解法,也欢迎提出来

image.png

题外话 :我要被自己蠢哭了,呜呜呜,Markdown语法好难

统计信息

通过次数 提交次数 AC比率
3122 4167 74.9%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
2022-将一维数组转变成二维数组(Convert 1D Array Into 2D Array)
下一篇:
2024-考试的最大困扰度(Maximize the Confusion of an Exam)
本文目录
本文目录