array

python 备忘_矩阵相关

时光怂恿深爱的人放手 提交于 2020-03-10 05:06:45
1. python中矩阵合并、拼接、组合 >> > a = np . array ( [ 1 , 2 , 3 ] ) >> > b = np . array ( [ 2 , 3 , 4 ] ) >> > np . stack ( ( a , b ) ) array ( [ [ 1 , 2 , 3 ] , [ 2 , 3 , 4 ] ] ) >> > >> > np . stack ( ( a , b ) , axis = - 1 ) array ( [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 4 ] ] ) 2. array[n-1,:] 取数组的第n行 array[:,n-1] 取数组的第n列 array[:,0:3] 取数组的1到3列 np.ones((n, 1)) 生成一个n行1列的矩阵,数组元素全是1,用在T@point_cloud(np.hstack来造矩阵,满足矩阵相乘条件) 3. np.array和np.matrix的差别, np.array([1,2,3])这种不是矩阵, np.array([[1, 2, 3]]) 和 np.matrix(1, 2, 3) 才是矩阵,有shape np.array和np.matrix的差别 来源: CSDN 作者: Ka. 链接: https://blog.csdn.net/guaiderzhu1314

【剑指Offer-代码的完整性】面试题21:调整数组顺序使奇数位于偶数前面

百般思念 提交于 2020-03-09 13:09:15
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。 注:这个题在书中没有要求奇数和奇数,偶数和偶数之间的相对位置不变。 思路1 遍历整个数组,当遇到偶数时,将该偶数后面的数前移一个位置并把该偶数放到数组末尾。这个算法时间复杂度为O(n^2),但可以保证奇数和奇数,偶数和偶数之间的相对位置不变。代码如下: class Solution { public: void reOrderArray(vector<int> &array) { int len = array.size(); if(len==0) return; int evenNums = 0; //数组中偶数个数 for(int i=0; i<len; i++) if(array[i]%2==0) evenNums++; int i=0; while(i<len && evenNums){ //注意循环条件 if(array[i]%2==0){ int t = array[i]; for(int j=i; j<len-1; j++) array[j] = array[j+1]; array[len-1] = t; evenNums--; } else i++; //只有当当前值为奇数时才+1 } }

Scala学习笔记03_数组

﹥>﹥吖頭↗ 提交于 2020-03-09 02:02:31
Array Array,长度不可改变的数组,Scala数组的底层实际上是Java数组,如字符串数组在底层就是Java的String[],整数数组在底层就是Java的Int[]。 1 // 数组初始化后,长度就固定下来了,而且元素全部根据其类型初始化 2 scala> val a = new Array [Int]( 10 ) 3 a: Array [Int] = Array ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) 4 scala> a( 0 )= 1 5 scala> a 6 res7: Array [Int] = Array ( 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) 7 scala> a( 0 ) 8 res8: Int = 1 9 scala> val a = new Array [ String ]( 10 ) 10 a: Array [ String ] = Array ( null , null , null , null , null , null , null , null , null , null ) 11 scala> a( 1 )= "leo" 12 scala> a 13 res10: Array [ String ] = Array ( null , leo, null

js 判断是否是数组 及原生toString()方法判断数据类型

谁说胖子不能爱 提交于 2020-03-08 23:22:10
Array.isArray() let a = [1,2,3] Array.isArray(a);//true此方法为 ES5新增方法 ,兼容Es5之外不支持的问题 if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype.toString.call(arg) === '[object Array]'; }; } Object.prototype.toString.call()判断数据类型 1.判断基本类型: Object.prototype.toString.call(null);//”[object Null]” Object.prototype.toString.call(undefined);//”[object Undefined]” Object.prototype.toString.call(“abc”);//”[object String]” Object.prototype.toString.call(123);//”[object Number]” Object.prototype.toString.call(true);//”[object Boolean]” 2.判断原生引用类型: 函数类型 Function fn(){console.log(“test”);}

42.数组中只出现一次的数字

℡╲_俬逩灬. 提交于 2020-03-08 16:13:04
题目描述:   一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。 思路分析:   该题想考的是位操作,我们分析如果数组中只有一个数字出现一次,其他数字都出现了两次,那么我们可以将数组中的元素异或最后的得到的值就是出现一次的数,因为相同数异或为0,所以很自然的最后的结果就是出现一次的数。那么我们首要任务就是将数组分成两个各自只包含一个只出现一次的数的数组。我们将数组中的元素进行异或,那么最终的结果一定是两个只出现一次数的异或结果。我们找到这个结果二进制中最低位的1的位置,就能在数组中将这两个数分开。 代码: public class Solution { public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) { int myxor=0; int flag=1; for(int i=0;i<array.length;i++){ myxor=myxor^array[i]; } while((myxor & flag)==0){ flag<<=1; //找到第一个1的位置 } for(int j=0;j<array.length;j++){ if((array[j]&flag)==0){ num1[0]=num1[0]^array[j]; }else{ num2

Two Arrays(dp)

不打扰是莪最后的温柔 提交于 2020-03-08 12:56:24
问题 Two Arrays You are given two integers n and m. Calculate the number of pairs of arrays (a,b) such that: the length of both arrays is equal to m; each element of each array is an integer between 1 and n (inclusive); ai≤bi for any index i from 1 to m; array a is sorted in non-descending order; array b is sorted in non-ascending order. As the result can be very large, you should print it modulo 1e9+7. input The only line contains two integers n and m (1≤n≤1000, 1≤m≤10). Output Print one integer – the number of arrays a and b satisfying the conditions described above modulo 109+7. Examples

C/C++中获取数组的长度

試著忘記壹切 提交于 2020-03-07 08:46:46
C/C++中没有提供直接获取数组长度的函数。 对于存放字符串的字符数组,可用strlen函数获取长度 。 如:char a[]="hello world";int count = strlen(a); 对于其他类型的数组,可使用sizeof(array)/sizeof(array[0]),计算长度。 在C中,可在使用时把它定义成宏, #define GET_ARRAY_LENGTH(array,len){len=(sizeof(array)/sizeof(array[0]));} 在C++中,可使用模板技术,定义这个函数, 需要将数组参数作为引用类型传递,否则数组名称会退化为一个指针 ,这样就无法计算数组的大小sizeof(a)。 template<class T> int GetArrayLen(T& a) { return sizeof(a)/sizeof(a[0]); } 对于存放字符串的字符数组,也可以使用这种方法来计算长度,但是计算后的值需要减去1, 即sizeof(a)/sizeof(a[0])-1,因为字符串默认带有结束符号'\0' 使用示例如下: C语言: #include<stdio.h> #define GET_ARRAY_LENGTH(array,len){len=(sizeof(array)/sizeof(array[0]));} int main() {

26. Remove Duplicates from Sorted Array

杀马特。学长 韩版系。学妹 提交于 2020-03-07 08:18:54
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array nums = [1,1,2] , Your function should return length = 2 , with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length. 代码: 题目理解没什么难点,就是对排好序的数,去除重复的,还剩下几个。 但题目要求内存不能增加,就是说不能新建数据结构,但可以删除现有的list :) 于是乎: #-*- utf-8 -*- #Remove Duplicates from Sorted Array class Solution(object): def

26. Remove Duplicates from Sorted Array

寵の児 提交于 2020-03-07 08:18:42
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array nums = [1,1,2] , Your function should return length = 2 , with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length. AC代码: class Solution(object): def removeDuplicates(self, nums): i, pre = 0, None while i < len(nums): if nums[i] != pre: pre = nums[i] i += 1 else: del nums[i] return len(nums)

PHP学习笔记之数组(一)

只谈情不闲聊 提交于 2020-03-07 07:33:52
最近正在学习PHP,就把学习到的知识和心得写出来,以防学完就忘,也请各位博友指点! 其实PHP中的数组和JavaScript中的数组很相似,就是一系列键值对的集合。 一、如何定义数组:在PHP中创建数组主要有两种方式,下面就让我们来看看如何创建一个数组 (1)直接给每个元素赋值的方法创建数组。 格式为:$arrayname[key]=value; 其中arrayname为数组的名字,key为数组的元素的键,value为元素的值。键可以是0,1,2,3这一类数字,也可以是字符串。如下所示: 1 <?php 2 //用1,2,3的数值作为数组的键 3 echo '<p>数组$array1的键值为:</p>'; 4 $array1[1]='a'; 5 $array1[2]='b'; 6 $array1[3]='c'; 7 print_r($array1); 8 9 //如果省略键的方式,则数组默认的键为从0开始递增的数值10 echo '<p>数组$array2的键值为:</p>';11 $array2[]='a';12 $array2[]='b';13 $array2[]='c';14 print_r($array2);15 16 //以字符串作为数组的键17 echo '<p>数组$array3的键值为:</p>';18 $array3['one']='a';19 $array3[