array

去重方法小结

假如想象 提交于 2019-12-04 03:56:56
1.最基本的去重方法 思路:定义一个新数组,并存放原数组的第一个元素,然后将元素组一一和新数组的元素对比,若不同则存放在新数组中。 function unique(arr){   var res = [arr[0]];   for(var i=1;i<arr.length;i++){     var repeat = false;     for(var j=0;j<res.length;j++){       if(arr[i] == res[j]){         repeat = true;         break;       }     }     if(!repeat){       res.push(arr[i]);     }   }   return res; } 2.先排序在去重 思路:先将原数组排序,在与相邻的进行比较,如果不同则存入新数组 function unique(arr){   var arr2 = arr.sort();   var res = [arr2[0]];   for(var i=1;i<arr2.length;i++){     if(arr2[i] !== res[res.length-1]){       res.push(arr2[i]);     }   }   return res; } 3.利用对象的属性去重(推荐)

js 数组

别等时光非礼了梦想. 提交于 2019-12-04 03:56:42
数组: 定义: 数组是使用单独的变量名来存储一系列的值。 定义方式: 一维数组 : 空数组: var 变量名 = new Array(); var 变量名 = []; var 变量名 = new Array(数组的长度); 有数值的数组: var 变量名 = new Array("1","2"); var 变量名 = [1,2]; 注意: 注意: var arr1 = [3]; // arr1.length == 1, arr1[0] == 3 var arr2 = new Array(3); // arr2.length == 3, arr2[0] == undefined 往数组中添加元素: 1.通过下标,逐个添加,或者循环添加 2.push方法 :attr.push()。 遍历数组 : for循环: var aa = new Array("1","2","3"); for(var i = 0;i<aa.length;i++){ alert(aa[i]); for-in: var aa = new Array("a","b","c"); for(var b in aa){ alert(aa[b]); } 练习: 求偶数: var aa =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; for(var i = 1; i<

了解 JavaScript 中的内置对象

此生再无相见时 提交于 2019-12-04 02:22:34
Number JavaScript Number 对象是 一个数值包装器。您可以将其与 new 关键词结合使用,并将其设置为一个稍后要在 JavaScript 代码中使用的变量: var myNumber = new Number(numeric value); 或者,您可以通过将一个变量设置为一个数值来创建一个 Number 对象。然后,该变量将 能够访问该对象可用的属性和方法。 除了存储数值, Number 对象包含各种属性和 方法,用于操作或检索关于数字的信息。 Number 对象可用的所有属性 都是只读常量,这意味着它们的值始终保持 不变,不能更改。有 4 个属性包含在 Number 对象里: MAX_VALUE MIN_VALUE NEGATIVE_INFINITY POSITIVE_INFINITY MAX_VALUE 属性返回 1.7976931348623157e+308 值,它是 JavaScript 能够处理的最大数字: document.write(Number.MAX_VALUE); // Result is: 1.7976931348623157e+308 另外,使用 MIN_VALUE 返回 5e-324 值,这是 JavaScript 中最小的数字: document.write(Number.MIN_VALUE); // Result is: 5e

leetcode解题之532. K-diff Pairs in an Array Java版

二次信任 提交于 2019-12-04 00:46:29
532. K-diff Pairs in an Array Given an array of integers and an integer k , you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k . Example 1: Input: [3, 1, 4, 1, 5], k = 2 Output: 2 Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5). Although we have two 1s in the input, we should only return the number of unique pairs. Example 2: Input: [1, 2, 3, 4, 5], k = 1 Output: 4 Explanation: There are four 1-diff pairs in the array, (1,

16python的map函数,filter函数,reduce函数

蹲街弑〆低调 提交于 2019-12-03 15:57:18
map num_l = [1,6,8,9] def map_test(func,array): ret = [] for i in array: res = func(i) ret.append(res) return ret def jia(x): return x+1 #内置函数 print(map_test(lambda x:x+1,num_l)) print(map_test(jia,num_l)) #只能迭代一次 # res = map(lambda x:x+1,num_l) # for i in res: # print(i) # print(list(res)) filter #filter函数 movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb'] print(filter(lambda n:not n.endswith('sb'),movie_people)) res=filter(lambda n:not n.endswith('sb'),movie_people) print(list(res)) print(list(filter(lambda n:not n.endswith('sb'),movie_people))) reduce # def reduce_test(func

ES6 之reduce的高级技巧

浪尽此生 提交于 2019-12-03 15:35:09
reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。reduce() 方法接受四个参数:初始值(或者上一次回调函数的返回值),当前元素值,当前索引,调用 reduce() 的数组。 reduce() 的几个强大用法: 数组求和 var total = [ 0, 1, 2, 3 ].reduce(( acc, cur ) => { return acc + cur }, 0); console.log(total) // 6 二维数组转为一维数组 var array = [[1, 2], [3, 4], [5, 6]].reduce(( acc, cur ) => { return acc.concat(cur) }, []); console.log(array) // [ 0, 1, 3, 4, 5, 6 ] 计算数组中每个元素出现的次数 1. 方法一 let names = ['tom', 'jim', 'jack', 'tom', 'jack']; const countNames = names.reduce((allNames, name) => { if (name in allNames) { allNames[name] ++; } else { allNames[name] = 1; } return

33-Search in Rotated Sorted Array

别说谁变了你拦得住时间么 提交于 2019-12-03 15:05:26
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2] ). You are given a target value to search. If found in the array return its index, otherwise return -1 . You may assume no duplicate exists in the array. Your algorithm's runtime complexity must be in the order of O (log n ). Example 1: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Example 2: Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1 我的解: Runtime: 4 ms , faster than 80.23% of C++ online submissions for Search in Rotated Sorted Array

[LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二

久未见 提交于 2019-12-03 12:27:53
Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even. You may return any answer array that satisfies this condition. Example 1: Input: [4,2,5,7] Output: [4,5,2,7] Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted. Note: 2 <= A.length <= 20000 A.length % 2 == 0 0 <= A[i] <= 1000 Github 同步地址: https://github.com/grandyang/leetcode/issues/922 类似题目: Sort Array By Parity 参考资料: https://leetcode.com/problems/sort-array-by

cifar-10数据集读取

梦想的初衷 提交于 2019-12-03 12:20:39
数据集官网:https://www.cs.toronto.edu/~kriz/cifar.html python2读取方式: def unpickle(file): import cPickle with open(file, 'rb') as fo: dict = cPickle.load(fo) return dict python3读取方式: def unpickle(file): import pickle with open(file, 'rb') as fo: dict = pickle.load(fo, encoding='bytes') return dict Loaded in this way, each of the batch files contains a dictionary with the following elements: data -- a 10000x3072 numpy array of uint8s. Each row of the array stores a 32x32 colour image. The first 1024 entries contain the red channel values, the next 1024 the green, and the final 1024 the blue. The image