当前位置:网站首页>1640. 能否连接形成数组-c语言实现
1640. 能否连接形成数组-c语言实现
2022-07-27 09:14:00 【Mr Gao】
1640. 能否连接形成数组-c语言实现
给你一个整数数组 arr ,数组中的每个整数 互不相同 。另有一个由整数数组构成的数组 pieces,其中的整数也 互不相同 。请你以 任意顺序 连接 pieces 中的数组以形成 arr 。但是,不允许 对每个数组 pieces[i] 中的整数重新排序。
如果可以连接 pieces 中的数组形成 arr ,返回 true ;否则,返回 false 。
示例 1:
输入:arr = [15,88], pieces = [[88],[15]]
输出:true
解释:依次连接 [15] 和 [88]
示例 2:
输入:arr = [49,18,16], pieces = [[16,18,49]]
输出:false
解释:即便数字相符,也不能重新排列 pieces[0]
示例 3:
输入:arr = [91,4,64,78], pieces = [[78],[4,64],[91]]
输出:true
解释:依次连接 [91]、[4,64] 和 [78]
个人觉得这题本来应该是非常难得,leedcode中c语言也没几个题解,解题代码如下,代码强度还是挺高的:
bool canFormArray(int* arr, int arrSize, int** pieces, int piecesSize, int* piecesColSize){
int r[piecesSize];
int i;
for(i=0;i<piecesSize;i++){
r[i]=1;
}
for(i=0;i<arrSize;i++){
int rz=0;
for(int k=0;k<piecesSize;k++){
int j=i;
if(r[k]==1&&pieces[k][0]==arr[j]){
int p;
for( p=1;p<piecesColSize[k];p++){
if(pieces[k][p]!=arr[++j]){
break;
}
}
if(p==piecesColSize[k]){
r[k]=0;
i=j;
rz=1;
break;
}
}
}
if(rz==0){
return false;
}
}
return true;
}
边栏推荐
猜你喜欢

PVT's spatial reduction attention (SRA)

软件测试功能测试全套常见面试题【功能测试-零基础】必备4-1

5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了

巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...

Data interaction based on restful pages

拍卖行做VC,第一次出手就投了个Web3

对 int 变量赋值的操作是原子的吗?

MySQL transaction

Antdesign a-modal自定义指令实现拖拽放大缩小

【微服务~Sentinel】Sentinel之dashboard控制面板
随机推荐
ES6 new - object part
Some practical, commonly used and increasingly efficient kubernetes aliases
pollFirst(),pollLast(),peekFirst(),peekLast()
Tensorflow package tf.keras module construction and training deep learning model
D3.v3.js data visualization -- pictures and tips of force oriented diagram
Explanation of common basic controls for C # form application (suitable for Mengxin)
Solve the problem of Chinese garbled code on the jupyter console
ArkUI中的显式动画
Deep understanding of Kalman filter (2): one dimensional Kalman filter
js call和apply
5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了
[C language _ review _ learn Lesson 2] what is base system? How to convert between hexadecimals
You haven't heard of such 6 question brushing websites, have you? Are you out?
Analog library function
Nut weather
The lifecycle of arkui development framework components
CUDA Programming -03: thread level
Nut joke based on arkui ETS
苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击
[micro service ~sentinel] sentinel dashboard control panel