array

JavaScript工具函数--数组相关

谁说胖子不能爱 提交于 2019-12-05 23:12:19
/** * 数组去重 * @param arr * @returns {*[]} */ function deDuplication ( arr ) { return [ ... new Set ( [ ... arr ] ) ] ; } /** * 数组平铺 * @param arr * @returns {Array} */ function flattenArray ( arr ) { return arr . reduce ( ( prev , curr ) => { if ( ! Array . isArray ( curr ) ) { // 当前元素不是数组 return [ ... prev , curr ] ; } else { // 当前元素是数组 return [ ... prev , ... flattenArray ( curr ) ] ; } } , [ ] ) ; } /** * 数组平铺2 * @param arr */ function flattenArray1 ( arr ) { const res = [ ] ; function flat ( arr ) { for ( let item of arr ) { if ( ! Array . isArray ( item ) ) { res . push ( item ) ; } else {

二维数组中的查找

倖福魔咒の 提交于 2019-12-05 22:39:01
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 最开始题目理解错了,以为二维数组是从上到下,从左到右递增排列的。题目的意思是每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序,数组可能是这样的:[[1,2,8,9],[4,7,10,13]]。 错误代码 class Solution { public: bool Find(int target, vector<vector<int> > array) { int i; if(array.size()==0){ return false; } if(array[0].size()==0){ return false; } for (i = 0; i<array.size(); i++) { if (target<array[i][0]) { if (i == 0) { return false; } int j; for (j = 0; j < array[i - 1].size(); j++) { if (target == array[i - 1][j]) { cout << i - 1 << ", " << j << ", " << array[i - 1

scala 关于Array,List,Tuple的区别

空扰寡人 提交于 2019-12-05 22:17:30
在 Scala 2.7 中, Array 、 List 都不能混合类型,只有 Tuple 可以;而在Scala以上版本中, 3 者的元素都可以混合不同的类型(转化为 Any 类型),只不过是 当使用混合类型时,Array和List会将元素类型转化为Any类型,而Tuple则保留每一个元素的初始类型 ; 关于Array,List,Tuple 关于初始化 1 ) val array= new Array[String](3) // Array(null, null, null) 相当于声明了3个null值的空元素 val array= Array("a","b","c","d") // 相当于 Array.apply("a","b","c","d") 定义一个类型为 Any 的 Array : val aa = Array[Any](1, 2) 或: val aa: Array[Any] = Array(1, 2) 或: val aa: Array[_] = Array(1, 2) 2 ) v al list:List [ Int ] = List(1,3,4,5,6) // 或者 v al list = List(1,3,4,5,6) (:::)实现叠加List,(::)cons:将新元素组合到列表的最前端。 元素合并使用::,集合合并使用:::,示例如下:其中Nil代表空元素

python模块之numpy,pandas

 ̄綄美尐妖づ 提交于 2019-12-05 17:07:20
numpy: 是 Python 的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库简单来说:就是支持一维数组和多维数组的创建和操作,并有丰富的函数库。 直接看例子 一维数组: k=np.array([1,2,3,4]) np.ndim(k) #查看维数 1 np.shape(k) #显示维度的元素个数 (4,) k.size #总共多少个数字 4 二维数组: m=np.array([[1,2,3,4],[0.1,0.2,0.3,0.4]]) np.shape(m) # (2, 4) #两个维度,一个维度4个数字 m array([[1. , 2. , 3. , 4. ], [0.1, 0.2, 0.3, 0.4]]) m.size 8 m[:,0:2] #显示每个维度里面第一和第二个数字 array([[1. , 2. ], [0.1, 0.2]]) 下面看看一个图 来源: https://www.cnblogs.com/mmyy-blog/p/11935245.html

java实现几种常用的排序算法

我与影子孤独终老i 提交于 2019-12-05 15:37:45
1 import java.util.Arrays; 2 import java.util.Random; 3 4 /** 5 * Created by Administrator on 2019/11/25. 6 */ 7 public class Application { 8 public static void main(String[] args) { 9 int[] array=new int[8]; 10 Sort sort=new Sort(); 11 fillArray(array); 12 System.out.println("快速排序前:"+ Arrays.toString(array)); 13 sort.quickSort(array,0,array.length-1); 14 System.out.println("排序后:"+ Arrays.toString(array)); 15 System.out.println("---------------------------------------"); 16 17 fillArray(array); 18 System.out.println("冒泡排序前:"+ Arrays.toString(array)); 19 sort.bubbleSort(array); 20 System.out

ZBD-3 螺旋

牧云@^-^@ 提交于 2019-12-05 14:14:10
二维数组螺旋遍历 #include <iostream> using namespace std; int main() { int m,n; int left=0,right=0,top=0,bottom=0; cin>>m>>n; char array[m][n]; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cin>>array[i][j]; } } right=1; int i=0,j=0; for(int tst=0;tst<m*n;tst++) { if(right==1) { if(array[i][j]=='?'||j==n-1) { if(array[i][j]!='?') cout<<array[i][j]; right=0; bottom=1; i++; continue; } cout<<array[i][j]; array[i][j]='?'; j++; } else if(left==1) { if(array[i][j]=='?'||i==0) { if(array[i][j]!='?') cout<<array[i][j]; left=0; top=1; i--; continue; } cout<<array[i][j]; array[i][j]='?'; j--; } else if(top==1)

Data Structure: All you should know about Hash

大城市里の小女人 提交于 2019-12-05 13:14:07
在我们进入正题之前,我们来讲讲Object里面最常用的两个方法和哈希的关系。在Java的定义当中,如果equals方法返回true的两个对象,其hashCode的返回值是需要相等的。默认的实现是,hashCode返回引用指向的对象的内存地址(10进制)。而equals根据两个hashCode返回的内存地址值(10进制)是否相等来判断两个对象是否相等。 另外还有一个比较容易记混的方法叫toString,对于没有重载这个方法的类,它的默认是返回classname @ heximal 的内存地址(与上面的10进制不同)。 而hash table的基本原理是,对于任何的对象,利用hashcode产生一个独一无二的数,再用这个数放进地址压缩算法返回一个index值。下面讨论的是,如何在压缩算法上做文章。 Why do we need hashing? Assume we store our data in an array, and we want to search for an element. The normal time consume is O(n). Since we need to traversal the array's element one by one. If we got a sorted array, we can use binary search to

Design Journey Into Searches, Sorts, and IComparer

喜你入骨 提交于 2019-12-05 13:12:08
Picture this scenario: you have an array of objects and need to search through the array for an object matching a specific ID. You know the array will be around for a long time in your application, so you need to make the search quick. You know you do not want to implement the search with the following code, because each search could potentially loop through the entire array of objects: SomeClass result = null; foreach(SomeClass c in someCollection) { if(c.ID == "A123") { result = c; break; } } return result; You know the Array class in .NET has a BinarySearch method which could improve the

Binary Search

帅比萌擦擦* 提交于 2019-12-05 11:11:20
原题链接在这里: https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/ 题目: Given an integer array sorted in ascending order, write a function to search target in nums . If target exists, then return its index, otherwise return -1 . However, the array size is unknown to you. You may only access the array using an ArrayReader interface, where ArrayReader.get(k) returns the element of the array at index k (0-indexed). You may assume all integers in the array are less than 10000 , and if you access the array out of bounds, ArrayReader.get will return 2147483647 . Example 1: Input: array

排序算法练习之选择排序

末鹿安然 提交于 2019-12-05 10:57:07
练习练习练习!!! import java.util.Arrays; /** * @Date: 2019-11-24 09:20 * @King: No blood!No bone!No ash!!! */ public class TestSelectionSort { public static void main(String[] args) { /** * 定义数组 */ int [] arr = new int[7]; arr[0] = 31; arr[1] = 13; arr[2] = 21; arr[3] = 33; arr[4] = 10; arr[5] = 25; arr[6] = 11; /** * 输出排序前 */ System.out.println("排序前:"+ Arrays.toString(arr)); /** * 调用两种选择排序方法,并输出结果 */ selectionSortI(arr); System.out.println("无返回值类型排序后:"+Arrays.toString(arr)); selectionSortII(arr); System.out.println("有返回值类型排序后:"+Arrays.toString(arr)); } /** * 无返回值类型的选择查找 */ public static void