//深copy
function deepClone(obj){
var newObj= obj instanceof Array ? [] : Object;
if(typeof obj !== "object"){
return newObj;
}
else{
for(var i in obj){
newObj[i] = typeof obj[i] === 'object' ? deepClone(obj[i]):obj[i];
}
return newObj;
}
}
// 測試用例
var obj1 = {
a: 1,
b: [1, 2, 3],
c: {}
};
deepClone(obj1);
function deepClone1(o1, o2) {
for (let k in o2) {
if (typeof o2[k] === 'object') {
o1[k] = {};
deepClone(o1[k], o2[k]);
} else {
o1[k] = o2[k];
}
}
}
// 測試用例
let obj = {
a: 1,
b: [1, 2, 3],
c: {}
};
let emptyObj = Object.create(null);
deepClone1(emptyObj, obj);
console.log(emptyObj.a);
console.log(emptyObj.a == obj.a);
console.log(emptyObj.b == obj.b);
//淺copy
var a = [1,2,{name: 'amy'}];
var b = a.concat([]);
console.log(b);
b[2].name = 'sam';
console.log(a[2].name);// 輸出‘sams
const Arr = [85, 24, 63, 45, 17, 31, 96, 50];
function quickSort(Arr){
if(Arr.length<=0){
return Arr;
}
var pivotIndex= Math.floor(Arr.length/2);
var pivot = Arr.splice(pivotIndex,1)[0]
console.log(pivot);
var left=[];
var right=[];
for(var i=0; i=0){
if(array[i][j]target){
j--;
}
else{
return true;
}
}
return false;
}
//測試用例
console.log(Find(10, [
[1, 2, 3, 4],
[5, 9, 10, 11],
[13, 20, 21, 23]
])
);