-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第 30 题:请把俩个数组 [A1, A2, B1, B2, C1, C2, D1, D2] 和 [A, B, C, D],合并为 [A1, A2, A, B1, B2, B, C1, C2, C, D1, D2, D]。 #39
Comments
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
const arr2 = ['A', 'B', 'C', 'D']
const ret = []
let tmp = arr2[0]
let j = 0
for (let i=0;i<arr1.length;i++) {
if (tmp === arr1[i].charAt(0)){
ret.push(arr1[i])
}else {
ret.push(tmp)
ret.push(arr1[i])
tmp=arr2[++j]
}
if(i===arr1.length-1){
ret.push(tmp)
}
}
console.log(ret) |
var a = ['A1','A2','B1','B2','C1','C2','D1','D2']
var b = ['A','B','C','D']
// 对需要排序的数字和位置的临时存储
var mapped = a.concat(b).map(function(el, i) {
return { index: i, value: /\D$/.test(el) ? (el + 4) : el };
})
mapped.sort(function(a, b) {
return +(a.value > b.value) || +(a.value === b.value) - 1;
});
var result = mapped.map(function(el){
return a.concat(b)[el.index];
}); 利用mdn对sort映射改善排序的方法进行的处理,不过对数组进行了多次处理,感觉方法不太好 |
let a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let a2 = ['A', 'B', 'C', 'D'].map((item) => {
return item + 3
})
let a3 = [...a1, ...a2].sort().map((item) => {
if(item.includes('3')){
return item.split('')[0]
}
return item
}) |
var arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"]
var arr2 = ["A", "B", "C", "D"]
var arr3 = arr1.concat(arr2);
arr3.sort().sort(function(a,b){
if (a.charAt(0) == b.charAt(0) && a.length > b.length){
return -1
}
})``` |
var arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']; function fn (arr1, arr2) { console.log(fn(arr1, arr2)); // [ 'A1', 'A2', 'A', 'B1', 'B2', 'B', 'C1', 'C2', 'C', 'D1', 'D2', 'D' ] |
我想问下,这题想考的是哪方面的知识? |
var arrOne =["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"]; for (let i = 0; i < arrTwo.length; i++) { 这样是否可以呢? |
|
var arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"];
var arr2 = ["A", "B", "C", "D"];
arr2.forEach((it, index) => {
arr1.splice((index + 1) * 2 + index, 0, it);
});
console.log(arr1); |
假设有一种情况,让你在一个列表中插入一个广告,不光是数组,对象依然有这种需求,这道题其实就是平常经常需要用到的一个小功能。 |
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const arr2 = ['A', 'B', 'C', 'D']
const arr = [...arr1,...arr2]
let targetArr = [];
arr2.forEach(item => {
arr.forEach(ele=>{
if(ele.includes(item)){
targetArr.push(ele)
}
})
});
console.log('targetArr',targetArr); |
如果只是单纯解这道题的话,我这样做: const res = ["A", "B", "C", "D"].reduce(
(memo, item) => {
const tmp = [...memo].reverse();
const idx = memo.length - tmp.findIndex(i => i.startsWith(item)) - 1;
return [...memo.slice(0, idx + 1), item, ...memo.slice(idx + 1)];
},
["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"]
); 这样即使是 |
|
const matchIndex = str => str.match(/\d+/) || []
const getCharCode = str => str.match(/\w/)[0].charCodeAt()
const result = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
.concat(['A', 'B', 'C', 'D'])
.sort((a,b) => {
const [[aIndex = Infinity], [bIndex = Infinity]] = [matchIndex(a), matchIndex(b)]
const [aChar, bChar] = [getCharCode(a), getCharCode(b)]
return aChar === bChar
? aIndex - bIndex
: aChar - bChar
})
console.log(result) 提一个新思路的版本,从修改sort入手,不依赖数组下标,通用性更强 |
let arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"]
, arr2 = ["A", "B", "C", "D"]
function concatArr(arr1, arr2) {
let newArr = []
while (arr2.length !== 0) {
let tag2 = arr2.pop()
newArr.unshift(tag2)
while (arr1.length !== 0) {
let tag1 = arr1.pop()
if (tag1.includes(tag2)) {
newArr.unshift(tag1)
} else {
arr1.push(tag1)
break
}
}
}
return newArr
}
console.log(arr1)
console.log(arr2)
console.log(concatArr(arr1, arr2)) |
其实解法很简单的 let arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"];
let arr2 = ["A", "B", "C", "D"];
console.log(
[...arr1, ...arr2]
.sort(
(v2, v1) => (
v2.codePointAt(0) - v1.codePointAt(0) ||
v1.length - v2.length ||
v2.codePointAt(1) - v1.codePointAt(1)
)
)
); |
@liuliangsir 解法是对的,就是你这个函数能不能换行啊,这样长看着多不舒服 |
|
借鉴了前面各位大神的。 |
var arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
var arr2 = ['A', 'B', 'C', 'D']
const func = (arr1, arr2) => arr2.reduce((acc, cur) => [...acc, ...arr1.filter(item => item.startsWith(cur)), cur], []) |
题目测试用例有点少规则太模糊了。。 写一个用sort的吧 const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const arr2 = ['A', 'B', 'C', 'D'];
const arr3 = arr1.concat(arr2);
const comp = function(a,b){
const len = Math.max(a.length, b.length);
for(let i = 0; i < len; i++){
if(a.charAt(i) === "") return 1;
if(b.charAt(i) === "") return -1;
if(a.charAt(i) !== b.charAt(i)){
return a.charAt(i) > b.charAt(i) ? 1:-1;
}
}
return 0;
}
arr3.sort(comp);
console.log(arr3); |
|
let a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let b = ['A', 'B', 'C', 'D']
let arr = a.concat(b).sort()
let tmp = '', r = []
arr.forEach((item, index, a) => {
if (item.length === 1 && tmp === '') {
tmp = a[0]
} else if (item.length === 1) {
r.push(tmp)
tmp = item
} else {
r.push(item)
}
// 这里是将最后获取的单个值,push到最后。
if (index === a.length -1) {
r.push(tmp)
}
})
console.log(r) // ["A1", "A2", "A", "B1", "B2", "B", "C1", "C2", "C", "D1", "D2", "D"] 想法是将合并后的数组sort完,将如A、B替换到A1等的后面。这样的好处是只需要一次遍历。缺点是仅仅针对题目的数据格式,即A1 和 A的字符串长度。 其实一开始想到的是下面这种替换合并后的数组。不过感觉不如新起一个数组 let a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let b = ['A', 'B', 'C', 'D']
let arr = a.concat(b).sort()
let tmp = ''
arr.forEach((item, index, a) => {
if(item.length === 1 && tmp === '') {
tmp = a.splice(0, 1)
} else if(item.length === 1) {
tmp = a.splice(index, 1, ...tmp)
} else if(index === a.length -1) {
a.push(...tmp)
}
})
console.log(arr) // ["A1", "A2", "A", "B1", "B2", "B", "C1", "C2", "C", "D1", "D2", "D"] |
|
|
|
|
//利用字符串的charCodeAt 进行排序,some 只要找到一个符合条件的就不要在进行循环了 |
方法比较low但是应该是能实现
|
您的邮件已收到!O(∩_∩)O!--------Pan
|
这是来自QQ邮箱的假期自动回复邮件。
邮件收到,欢迎常联系!。
|
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] // 不确保合并的顺序可以先对长度排序 |
您的来信已收到,我会尽快回复,谢谢
|
可以利用aplisce插入元素的特性,实现一个更简单的方法 |
同首字母比大小,大的先放,不同首字母比大小,小的先放,最后两个把数组全部放完 function merge(arr1, arr2) {
let res = []
let len1 = arr1.length;
let len2 = arr2.length;
let l1 = 0, l2 = 0;
while (l1 < len1 && l2 < len2) {
if (arr1[l1].charAt(0) === arr2[l2].charAt(0)) {
if (arr1[l1] > arr2[l2]) {
res.push(arr1[l1++])
} else {
res.push(arr2[l2++])
}
} else {
if (arr1[l1] < arr2[l2]) {
res.push(arr1[l1++])
} else {
res.push(arr2[l2++])
}
}
}
while(l1 < len1) {
res.push(arr1[l1++])
}
while(l2 < len2) {
res.push(arr2[l2++])
}
return res;
}
let arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let arr2 = ['A', 'B', 'C', 'D']
merge(arr1, arr2)
merge(arr2, arr1) |
|
您的来信已收到,我会尽快回复,谢谢
|
这是来自QQ邮箱的假期自动回复邮件。
邮件收到,欢迎常联系!。
|
const arr1 = ['A1', "A2", "B1", 'B2', 'C1', 'C2', 'D1', 'D2']
}) |
这是来自QQ邮箱的假期自动回复邮件。
邮件收到,欢迎常联系!。
|
您的邮件已收到!O(∩_∩)O!--------Pan
|
您的来信已收到,我会尽快回复,谢谢
|
var a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] function fn1(a1, a2) { |
var a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
var a2 = ['A', 'B', 'C', 'D'];
function concatArr(arr1, arr2) {
const map = new Map();
const res = [];
for (let i of arr2) {
let temp = arr1.filter(j => j.includes(i));
map.set(i, temp);
}
for (let [k, v] of map.entries()) {
res.push(...v, k);
}
return res;
}
concatArr(a1, a2)
// ['A1', 'A2', 'A', 'B1', 'B2', 'B', 'C1', 'C2', 'C', 'D1', 'D2', 'D'] |
您的来信已收到,我会尽快回复,谢谢
|
您的邮件已收到!O(∩_∩)O!--------Pan
|
1 similar comment
您的邮件已收到!O(∩_∩)O!--------Pan
|
您的来信已收到,我会尽快回复,谢谢
|
代码很少的方法 |
凑个热闹! let a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let b = ['A', 'B', 'C', 'D'];
let r = [...a, ...b].sort((c, d) => {
if (c[0] < d[0] || (c[0] == c[d] && c.length > d.length)) {
return -1;
}
});
console.log(r); |
您的邮件已收到!O(∩_∩)O!--------Pan
|
您的来信已收到,我会尽快回复,谢谢
|
function mergeArr(arr1, arr2) {
const result = []
for(let i=1;i < arr1.length;i++) {
if (arr1[i].substring(0, 1) !== arr1[i-1].substring(0, 1)) {
result.push(arr1[i-1])
result.push(arr2.shift())
} else {
result.push(arr1[i-1])
}
if (i === arr1.length - 1) {
result.push(arr1[i])
result.push(arr2.shift())
}
}
return result
}
mergeArr(['A1', 'A2', 'A3', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2', 'E1'], ['A', 'B', 'C', 'D', 'E']) |
您的来信已收到,我会尽快回复,谢谢
|
您的邮件已收到!O(∩_∩)O!--------Pan
|
var a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] function concatArray(arr1, arr2) { var resultArr = concatArray(a1, a2) |
您的来信已收到,我会尽快回复,谢谢
|
您的邮件已收到!O(∩_∩)O!--------Pan
|
以上是我个人想法,有更好方法的欢迎讨论
The text was updated successfully, but these errors were encountered: