原文链接: 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]
andtarget
consist of digits.nums[i]
andtarget
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 。
通过代码
高赞题解
就按照题目描述来写
- i != j
- 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
跑起来的结果是我没有想到的(可以算是混过去了?)
题解要是有哪些地方不对的,各位大佬可以提出来;若是有不同解法,也欢迎提出来
题外话 :我要被自己蠢哭了,呜呜呜,Markdown语法好难
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
3122 | 4167 | 74.9% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|