this.nums = [...nums];和this.nums=nums;有什么区别?

#灵感来源:昨天的力扣;

this.nums = [...nums]; 和 this.nums = nums; 主要的区别在于它们复制数组的方式

this.nums = [...nums]; 使用的是展开运算符(spread operator),它进行的是浅拷贝。这意味着它复制数组的引用,而不是创建一个新的数组。因此,如果原数组 nums 发生变化,this.nums 也会相应地发生变化。

而 this.nums = nums; 则是直接将 nums 的引用赋值给 this.nums,这是深拷贝。也就是说,如果原数组 nums 发生变化,this.nums 不会受到影响。

例如,考虑以下的 JavaScript 代码:

let nums = [1, 2, 3];  
let obj = { nums };  
obj.nums.push(4);  
console.log(nums); // [1, 2, 3, 4]  
console.log(obj.nums); // [1, 2, 3, 4]

在这个例子中,如果你使用 obj.nums = [...nums];,那么 obj.nums 和 nums 就是两个完全独立的数组,一个改变不会影响另一个。但是如果你使用 obj.nums = nums;,那么改变 obj.nums 就会影响 nums。 

#include<stdio.h> #include<mem.h> #define OK 1 #define ERROR 0 #define MAX_SIZE 100 typedef int Status; typedef struct{ int nums[3]; int id; int operation; int parentId; int level; }ElemType; int cmpArray(int a[], int b[], int n) { int i; for(i=0;i<n;i++) if(a[i] != b[i]) return false; return true; } void BFS(int start[3], int end[3]) { ElemType openTable[MAX_SIZE]; int openFront = 0; int openRear = 0; ElemType CloseTable[MAX_SIZE]; int closeLen=0; bool isSuccess = false; ElemType tmpElem; int count; for(int i=0;i<3;i++) tmpElem.nums[i] = start[i]; // memcpy(tmpElem.nums, start, sizeof(start)); printf("%d %d %d\n", tmpElem.nums[0],tmpElem.nums[1],tmpElem.nums[2]); tmpElem.level = 1; tmpElem.parentId = -1; tmpElem.operation = -1; openTable[openRear] = tmpElem; openRear = (openRear + 1) % MAX_SIZE; while(openRear != openFront) { tmpElem = openTable[openFront]; openFront = (openFront + 1)%MAX_SIZE; printf("[%d %d %d], %c, %d\n", tmpElem.nums[0], tmpElem.nums[1] , tmpElem.nums[2], tmpElem.operation, tmpElem.level ); if(cmpArray(tmpElem.nums, end, 3) && tmpElem.level == 4) { isSuccess = true; break; } else if(tmpElem.level >= 4) { break; } ElemType childElem; for(int i=0;i<3;i++) { if(tmpElem.operation != 'a'+i) { count++; for(int i=0;i<3;i++) childElem.nums[i] = tmpElem.nums[i]; childElem.operation = 'a' + i; // memcpy(childElem.nums, tmpElem.nums, sizeof(tmpElem.nums)); childElem.nums[i] = (childElem.nums[i] + 1) % 2; childElem.parentId = tmpElem.id; childElem.id = count; childElem.level = tmpElem.level + 1; openTable[openRear] = childElem; openRear = (openRear + 1)%MAX_SIZE; } } } if(isSuccess) { printf("success!"); } else { printf("failed"); } } int main() { int a[3]={0,1,0}; int b[3]={1,1,1}; int c[3] = {0,0,0}; BFS(a,b); return 0; } 使用以上程序框架编写C语言代码,解决八数码问题
07-13
您可以使用以下代码来解决八数码问题: ```c #include<stdio.h> #include<string.h> #include<stdbool.h> #define MAX_SIZE 100 typedef struct{ int nums[3][3]; int id; int operation; int parentId; int level; }ElemType; void printMatrix(int matrix[3][3]){ for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ printf("%d ", matrix[i][j]); } printf("\n"); } } bool cmpMatrix(int a[3][3], int b[3][3]){ for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ if(a[i][j] != b[i][j]) return false; } } return true; } void BFS(int start[3][3], int end[3][3]){ ElemType openTable[MAX_SIZE]; int openFront = 0; int openRear = 0; ElemType CloseTable[MAX_SIZE]; int closeLen=0; bool isSuccess = false; ElemType tmpElem; int count = 0; memcpy(tmpElem.nums, start, sizeof(start)); printMatrix(tmpElem.nums); tmpElem.level = 1; tmpElem.parentId = -1; tmpElem.operation = -1; openTable[openRear] = tmpElem; openRear = (openRear + 1) % MAX_SIZE; while(openRear != openFront){ tmpElem = openTable[openFront]; openFront = (openFront + 1) % MAX_SIZE; printMatrix(tmpElem.nums); printf("Operation: %c, Level: %d\n", tmpElem.operation, tmpElem.level); if(cmpMatrix(tmpElem.nums, end) && tmpElem.level == 4){ isSuccess = true; break; } else if(tmpElem.level >= 4){ break; } ElemType childElem; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ if(tmpElem.nums[i][j] == 0){ if(i > 0 && tmpElem.operation != 'd'){ // move up count++; memcpy(childElem.nums, tmpElem.nums, sizeof(tmpElem.nums)); childElem.nums[i][j] = childElem.nums[i-1][j]; childElem.nums[i-1][j] = 0; childElem.operation = 'u'; childElem.parentId = tmpElem.id; childElem.id = count; childElem.level = tmpElem.level + 1; openTable[openRear] = childElem; openRear = (openRear + 1) % MAX_SIZE; } if(i < 2 && tmpElem.operation != 'u'){ // move down count++; memcpy(childElem.nums, tmpElem.nums, sizeof(tmpElem.nums)); childElem.nums[i][j] = childElem.nums[i+1][j]; childElem.nums[i+1][j] = 0; childElem.operation = 'd'; childElem.parentId = tmpElem.id; childElem.id = count; childElem.level = tmpElem.level + 1; openTable[openRear] = childElem; openRear = (openRear + 1) % MAX_SIZE; } if(j > 0 && tmpElem.operation != 'r'){ // move left count++; memcpy(childElem.nums, tmpElem.nums, sizeof(tmpElem.nums)); childElem.nums[i][j] = childElem.nums[i][j-1]; childElem.nums[i][j-1] = 0; childElem.operation = 'l'; childElem.parentId = tmpElem.id; childElem.id = count; childElem.level = tmpElem.level + 1; openTable[openRear] = childElem; openRear = (openRear + 1) % MAX_SIZE; } if(j < 2 && tmpElem.operation != 'l'){ // move right count++; memcpy(childElem.nums, tmpElem.nums, sizeof(tmpElem.nums)); childElem.nums[i][j] = childElem.nums[i][j+1]; childElem.nums[i][j+1] = 0; childElem.operation = 'r'; childElem.parentId = tmpElem.id; childElem.id = count; childElem.level = tmpElem.level + 1; openTable[openRear] = childElem; openRear = (openRear + 1) % MAX_SIZE; } } } } } if(isSuccess){ printf("Success!\n"); } else{ printf("Failed\n"); } } int main(){ int start[3][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8} }; int end[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 0} }; BFS(start, end); return 0; } ``` 在上述代码中,`start`和`end`分别表示初始状态和目标状态的矩阵。`BFS()`函数使用广度优先搜索算法来解决八数码问题。在该函数中,我们使用了一个队列来存储待扩展的节点,以及一个数组来存储已扩展的节点。通过遍历每个节点的相邻节点,并将其加入队列中,直到找到目标状态或达到最大搜索深度。如果找到目标状态,则输出"Success!",否则输出"Failed"。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值