array

NumPy基础入门(3)花式索引和索引技巧

人走茶凉 提交于 2020-01-11 10:46:20
1.用索引数组建立索引 用索引数组建立索引,如果索引数组a是多维的,则单个索引数组是指的第一维a。 >>> a = np.arange(12)**2 # the first 12 square numbers >>> i = np.array( [ 1,1,3,8,5 ] ) # an array of indices >>> a[i] # the elements of a at the positions i array([ 1, 1, 9, 64, 25]) >>> >>> j = np.array( [ [ 3, 4], [ 9, 7 ] ] ) # a bidimensional array of indices >>> a[j] # the same shape as j array([[ 9, 16], [81, 49]]) 还可以为多个维度提供索引。每个维度的索引数组必须具有相同的形状。 >>> a = np.arange(12).reshape(3,4) >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> i = np.array( [ [0,1], # indices for the first dim of a ... [1,2] ] ) >>> j = np.array( [ [2

数组去重主要的5种方法,

倖福魔咒の 提交于 2020-01-11 04:13:34
数组去重的方法 一、利用ES6 Set去重(ES6中最常用) var arr = [1,1,8,8,12,12,15,15,16,16]; function unique (arr) { return Array.from(new Set(arr)) } console.log(unique(arr)) //[1,8,12,15,16] 不考虑兼容性,这种去重的方法代码最少。这种方法还无法去掉“{}”空对象,后面的高阶方法会添加去掉重复“{}”的方法。 二、利用for嵌套for,然后splice去重(ES5中最常用) var arr = [1, 1, 8, 8, 12, 12, 15, 15, 16, 16]; function unlink(arr) { for (var i = 0; i < arr.length; i++) { // 首次遍历数组 for (var j = i + 1; j < arr.length; j++) { // 再次遍历数组 if (arr[i] == arr[j]) { // 判断连个值是否相等 arr.splice(j, 1); // 相等删除后者 j--; } } } return arr } console.log(unlink(arr)); //NaN和{}没有去重,两个null直接消失了 双层循环,外层循环元素,内层循环时比较值。值相同时

剑指offer-二维数组中的查找

独自空忆成欢 提交于 2020-01-11 03:58:26
题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 时间限制:1秒 空间限制:32768K 热度指数:29783 我的方法:从数组每行第一个元素开始判断是否小于或等于目标值,若是就遍历该行 难点备注:二维数组判断的条件 1 public class Solution { 2 public boolean Find(int target, int [][] array) { 3 4 boolean flag=false; 5 6 if(array==null||array.length==0||(array.length==1&&array[0].length==0)){ 7 return flag; 8 } 9 10 for(int i=0;!flag&i<array.length;i++){ 11 if(target>=array[i][0]){ 12 for(int j=0;j<array[i].length;j++){ 13 if(array[i][j]==target){ 14 flag=true;break; 15 } 16 } 17 } 18 } 19 20 return flag; 21 } 22 } 优秀解法: /* 思路矩阵是有序的

剑指offer(1)

冷暖自知 提交于 2020-01-10 20:57:57
1.二维数组中的查找 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 public class Solution { public boolean Find(int target, int [][] array) { if(array == null||array.length==0) return false; int rowIdx = 0,colIdx = array[0].length-1; while(rowIdx<array.length&&colIdx>=0){ if(array[rowIdx][colIdx] == target) return true; else if(target>array[rowIdx][colIdx]) rowIdx++; else if(target<array[rowIdx][colIdx]) colIdx--; } return false; } }    2.替换空格 请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 public class Solution { public String

数组初始

岁酱吖の 提交于 2020-01-10 16:26:14
空数组 $arr=array(); var_dump($arr); gettype():得到变量的类型 echo gettype($arr);//array(0) {} 通过is_array():检测变量是否为数组 var_dump(is_array($arr));//arraybool(true) 如果数组下标重复,后面的覆盖前面的值 $arr=array( 'a', 0=>'b' ); var_dump($arr); //array(1) { //[0]=> // string(1) "b" //} 如果新添加元素没有指定下标,它的下标为已有下标最大值加1(已有下标不全为负数) $arr=array( 'a','b','c', 5=>'d', 19=>'e', 'f' ); //var_dump($arr); //array(6) { // [0]=> // string(1) "a" // [1]=> // string(1) "b" // [2]=> // string(1) "c" // [5]=> // string(1) "d" // [19]=> // string(1) "e" // [20]=> // string(1) "f" //} 如果已有下标都为负数,那么新添加元素的下标从0开始 $arr=array( -12=>'a', -43=>'b', 'c' )

NumPy 算术函数

六眼飞鱼酱① 提交于 2020-01-10 01:45:56
NumPy 算术函数 NumPy 算术函数包含简单的加减乘除: add() , subtract() , multiply() 和 divide() 。 需要注意的是数组必须具有相同的形状或符合数组广播规则。 实例 import numpy as np a = np . arange ( 9 , dtype = np . float_ ) . reshape ( 3 , 3 ) print ( ' 第一个数组: ' ) print ( a ) print ( ' \n ' ) print ( ' 第二个数组: ' ) b = np . array ( [ 10 , 10 , 10 ] ) print ( b ) print ( ' \n ' ) print ( ' 两个数组相加: ' ) print ( np . add ( a , b ) ) print ( ' \n ' ) print ( ' 两个数组相减: ' ) print ( np . subtract ( a , b ) ) print ( ' \n ' ) print ( ' 两个数组相乘: ' ) print ( np . multiply ( a , b ) ) print ( ' \n ' ) print ( ' 两个数组相除: ' ) print ( np . divide ( a , b ) ) 输出结果为

1146. Snapshot Array

被刻印的时光 ゝ 提交于 2020-01-09 13:12:00
Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0 . void set(index, val) sets the element at the given index to be equal to val . int snap() takes a snapshot of the array and returns the snap_id : the total number of times we called snap() minus 1 . int get(index, snap_id) returns the value at the given index , at the time we took the snapshot with the given snap_id Solution0: class SnapshotArray: def __init__(self, length: int): self.array = [[0] for i in

数组排序

我与影子孤独终老i 提交于 2020-01-09 04:57:12
数组排序方法的实现 JAVA中在运用数组进行排序功能时,一般有四种方法: 快速排序法、冒泡法、选择排序法、插入排序法。 快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现。 冒泡法是运用遍历数组进行比较,通过不断的比较将最小值或者最大值一个一个的遍历出来。 选择排序法是将数组的第一个数据作为最大或者最小的值,然后通过比较循环,输出有序的数组。 插入排序是选择一个数组中的数据,通过不断的插入比较最后进行排序。 <1>利用Arrays带有的排序方法快速排序 import java.util.Arrays; public class Test2{ public static void main(String[] args){ int[] a={5,4,2,4,9,1}; Arrays.sort(a); //进行排序 for(int i: a){ System.out.print(i); } } } <2>冒泡排序算法 public static int[] bubbleSort(int[] args){//冒泡排序算法 for(int i=0;i<args.length-1;i++){ for(int j=i+1;j<args.length;j++){ if (args[i]>args[j]){ int temp=args[i]; args[i]=args[j]

leetcode004 寻找两个有序数组的中位数

独自空忆成欢 提交于 2020-01-09 03:32:28
题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 你可以假设 nums1 和 nums2 不会同时为空。 解法1 依次遍历这两个有序数组的元素,如果长度分别为len1,len2直到找到第(len1+len2)/2的元素 class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int n1 = nums1.length; int n2 = nums2.length; int i = 0; int j = 0; int[] array = new int[(n1 + n2) / 2 + 1]; int index = 0; // 找到第(n1+n2)/2小的数 while(index < array.length) { if (i < n1 & j < n2) { if (nums1[i] < nums2[j]) { array[index++] = nums1[i++]; } else { array[index++] = nums2[j++]; } } else if (i >= n1) { array[index++] = nums2[j++]; }

排序算法

若如初见. 提交于 2020-01-08 21:19:53
排序算法 冒泡排序 选择排序 快速排序 归并排序 堆排序 冒泡排序 public class BubbleSort { //最简洁的冒泡排序写法 //每次比较相邻的两个元素,如果后方元素比前方元素小,则进行交换 //每次循环确定一个最大的的元素,放在最后一位 //平均时间复杂度 N² //空间复杂度 1 //稳定性 稳定 public static void main ( String [ ] args ) { int [ ] number = { 233 , 666 , 1 , 6 , 648 , 328 , 128 , 999 } ; //冒泡排序算法,由小到大排序 for ( int i = 0 ; i < number . length - 1 ; i ++ ) for ( int j = 0 ; j < number . length - 1 - i ; j ++ ) if ( number [ j ] > number [ j + 1 ] ) { int temp = number [ j ] ; number [ j ] = number [ j + 1 ] ; number [ j + 1 ] = temp ; } for ( int array : number ) System . out . print ( array + " " ) ; } } 选择排序