-
Notifications
You must be signed in to change notification settings - Fork 77
/
array.js
332 lines (312 loc) · 9.84 KB
/
array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* @authors : qieguo
* @date : 2016/11/24
* @version : 1.0
* @description : 数组、字符串相关;逻辑实现,不使用API等现成接口
*/
/**
* 数组、字符串的子集位置查询
* @param : <Array>||<String> target,目标字符串
* @param : <Array>||<String> tool,子串
* @return : <Number> 返回子串在目标中的位置,目标中找不到子串则返回-1
* @description : 数组、字符串的子集位置查询;可转成字符串用正则匹配,
*/
function indexOfArray(target, tool) {
for (var i = 0, targetLen = target.length; i < targetLen; i++) {
for (var j = 0, toolLen = tool.length; j < toolLen; j++) {
if (target[i + j] !== tool[j]) {
break; // j++是在循环体结束后自增,使用break则在自增前就跳出循环了
}
}
if (j === toolLen) {
return i;
}
}
return -1;
}
function indexOfArrayTest() {
console.log('indexOfArray test: ', indexOfArray('abcdedfg', 'e'));
}
/**
* 根据筛选条件从目标数组中返回符合条件的子集
* @param : <Array> target 要筛选的数据(数组)
* @param : <Object> tool 筛选条件(对象)
* @return : 返回符合条件的子集数组,否则返回空数组
* @description : 数组、字符串的子集位置查询;可用filter高阶函数代替循环
*/
function arrayFilter(target, tool) {
var result = [];
for (var i = 0, len = target.length; i < len; i++) {
var keep = true;
for (var key in tool) {
if (typeof target[i][key] === 'undefined' || target[i][key] !== tool[key]) {
keep = false;
break;
}
}
if (keep) {
result.push(target[i]);
}
}
return result;
}
function arrayFilterTest() {
var target = [
{name: 'Jack', age: 18, sex: 'f'},
{name: 'Jack', age: 20},
{name: 'Mike', age: 25}
];
console.log('\narrayFilter test: \n', arrayFilter(target, {age: 20}));
console.log('\narrayFilter test: \n', arrayFilter(target, {num: 10}));
console.log('\narrayFilter test: \n', arrayFilter(target, {}));
}
/**
* 数组去重
* @param : <Array> target 要去重的数组
* @description : 数组去重;可用es6 set、正则、sort等实现
*/
function arrayUnique(target) {
// 纯数组硬比较方式,这里要注意是否需要空对象{}的去重,暂不处理
// var result = [target[0]];
// for (var i = 1, targetLen = target.length; i < targetLen; i++) {
// for (var j = 0, resultLen = result.length; j < resultLen; j++) {
// if (result[j] === target[i]) {
// break; // j++是在循环体结束后自增,使用break则在自增前就跳出循环了
// }
// }
// if (j === resultLen) {
// result.push(target[i]);
// }
// }
// 对于去重这种无序的集合,可使用js对象的哈希特性来提高效率,但无法直接区分数字、字符,统一转为字符了
// Note: 数据量少的情况下,哈希算法本身的复杂度就超过了循环对比,所以性能上反而更差
var result = [target[0]];
var temp = Object.create(null);
temp[target[0]] = {};
temp[target[0]][(typeof target[0])] = 1;
// 要区分数字、字符、布尔值、null等类型,必须保存 temp[target[i]][(typeof target[i])] 作为标志
for (var i = 1, targetLen = target.length; i < targetLen; i++) {
if (typeof temp[target[i]] === 'undefined' || !temp[target[i]].hasOwnProperty(typeof target[i])) {
result.push(target[i]);
temp[target[i]] = {};
temp[target[i]][(typeof target[i])] = 1;
}
}
return result;
}
function arrayUniqueTest() {
// var target = [1, 2, 3, 3, '3', '3', 'length', '__proto__', 'prototype', true, false, true, {}, {}, null, null];
var target = [1, 2, 3, 3, '3', '3', '__proto__', '__proto__', '__proto__', 'prototype', 'prototype', true, false, true, {}, {}, null, null];
// var target = [1, '1', true, 'true'];
console.log('\narrayUnique test:\n', arrayUnique(target));
}
/**
* 合并两个有序数组
* @param : <Array> arrA 要合并的有序数组
* @param : <Array> arrB 要合并的有序数组
* @description : 将两个已经排序的数组合并成一个数组;
*/
function mergeSortedArray(arrA, arrB) {
var result = [];
var i = 0, j = 0, targetLen = arrA.length, toolLen = arrB.length;
while (i < targetLen && j < toolLen) {
if (arrA[i] < arrB[j]) {
result.push(arrA[i++]);
} else {
result.push(arrB[j++]);
}
}
while (i < targetLen) {
result.push(arrA[i++])
}
while (j < toolLen) {
result.push(arrB[j++])
}
return result;
}
function mergeSortedArrayTest() {
var target = [1, 5, 11, 18, 25, 40, 100, 120];
var tool = [3, 6, 11, 30, 31, 80, 90, 97];
console.log('\ncombineArray test:\n', mergeSortedArray(target, tool));
}
/**
* 数组最长无重复子串查找
* @param : <Array> target 要查找的数组
* @description : 查找没有重复的最长子串,若使用哈希表判断重复的话,就要重新定位,可以将哈希的value=数组元素当前序号
*/
function longestSubArray(target) {
var lastStart, lastLen, maxStart, maxLen;
lastStart = maxStart = 0;
lastLen = maxLen = 1;
for (var i = 1, len = target.length; i < len; i++) {
var noRepeat = true;
for (var j = lastStart; j < lastStart + lastLen; j++) {
if (target[i] === target[j]) {
noRepeat = false;
if (maxLen < lastLen) {
maxLen = lastLen;
maxStart = lastStart;
}
i = lastStart = i - (lastStart + lastLen - j) + 1;
lastLen = 1;
break;
}
}
if (noRepeat) {
lastLen++;
}
}
if (maxLen < lastLen) {
maxLen = lastLen;
maxStart = lastStart;
}
return target.slice(maxStart, maxLen + maxStart);
}
// 哈希版本,js实现起来简单、在数据量大的情况下会有优势
function longestSubArrayHash(target) {
var lastStart, lastLen, maxStart, maxLen, last = {};
lastStart = maxStart = 0;
lastLen = maxLen = 1;
last[target[lastStart]] = lastStart;
for (var i = 1, len = target.length; i < len; i++) {
if (typeof last[target[i]] === 'undefined') {
lastLen++;
last[target[i]] = i;
} else {
if (maxLen < lastLen) {
maxLen = lastLen;
maxStart = lastStart;
}
i = lastStart = last[target[i]] + 1;
last = {};
last[target[lastStart]] = lastStart;
lastLen = 1;
}
}
if (maxLen < lastLen) {
maxLen = lastLen;
maxStart = lastStart;
}
return target.slice(maxStart, maxLen + maxStart);
}
function longestSubArrayTest() {
var target = [1, 2, 3, 4, 3, 6, 8, 9, 10, 14, 15, 8, 9];
console.log('\nlongestSubArray test:\n', longestSubArray(target));
console.log('\nlongestSubArrayHash test:\n', longestSubArrayHash(target));
}
/**
* 数组重复次数最多的子串查找
* @param : <Array> target 要查找的数组
* @description : 查重,输出重复次数最多的元素及其重复次数。
*/
function countSubArray(target) {
var sub = {};
var max = {
num: 1,
index: 0
};
for (var i = 0, len = target.length; i < len; i++) {
if (!sub.hasOwnProperty(target[i])) {
sub[target[i]] = {
num: 1,
index: i
};
} else {
var current = sub[target[i]];
current.num++;
if (max.num < current.num) {
max.num = current.num;
max.index = current.index;
}
}
}
return {
element: target[max.index],
index: max.index,
count: max.num
};
}
function countSubArrayTest() {
var target = [2, 2, 2, 4, 4, 11, 11, 5, 15, 11, 17, 11, 80, 11];
var target1 = ['qwe', 'as', 'dsfw', 'as', 'kou', 'lpi', 'as', 'jei', 'as'];
console.log('\ncountSubArrayTest test:\n', countSubArray(target1));
};
/**
* 查询字符串第一个不重复字母
* @param : <Array> target 要查找的数组
* @description : 查询给定字符串“abcba”,处理得到第一个不重复字母,c
*/
function queryFirstUniqueItem(target) {
var items = {};
var last = [];
for (var i = 0, l = target.length; i < l; i++) {
var item = target[i];
if (!items.hasOwnProperty(item)) {
last.push(i);
items[item] = i;
} else {
var index = last.indexOf(items[item]);
if (index >= 0) {
last.splice(index, 1);
}
}
}
var ret = '';
if (last.length) {
ret = target[last[0]];
}
return ret
}
function queryFirstUniqueItemTest() {
var a = "abdcbadegceg";
var ret = queryFirstUniqueItem(a);
console.log('ret: ', ret);
console.log('ret: ', !ret);
}
// 给一个数组如:[[“a”,”b”,”c”],[“d”,”e”],…..]得到[ad,ae,bd,be,cd,ce]
function mapConcat(target) {
var res = target.reduce(function (pre, next) {
var ret = [];
pre.forEach(function (preItem) {
next.forEach(function (nextItem) {
if (preItem !== nextItem) { // 去掉aa这种情况
ret.push(preItem + nextItem);
}
})
})
return ret;
})
return res;
}
function mapConcatTest() {
// var target = [['a', 'b', 'c'], ['d', 'e'], ['f', 'g', 'h'], ['j', 'm']];
var target = [['a', 'b', 'c'], ['a', 'b']];
console.log('\nmapConcatTest:\n', mapConcat(target));
}
// mapConcatTest();
// 求整数数组的最大子串和,如 [-23, 17, -7, 11, -2, 1, -34] 的最大子串和是21,对应子串是 [17, -7, 11]
function maxSeqSum(arr) {
if (!Array.isArray(arr)) {
return null;
}
const len = arr.length;
let maxSum = arr[0];
let seqSum = arr[0];
for (let i = 1; i < len; i++) {
if (seqSum <= 0) {
seqSum = arr[i];
} else {
seqSum += arr[i];
}
if (seqSum > maxSum) {
maxSum = seqSum;
}
}
return maxSum;
}
function maxSeqSumTest() {
console.log(maxSeqSum([-23, 17, -7, 11, -2, 1, -34]));
console.log(maxSeqSum([1, -2, 3, 10, -4, 7, 2, -5]));
console.log(maxSeqSum([-1]));
}
// maxSeqSumTest();