array

MergeSort,归并排序

假装没事ソ 提交于 2020-03-26 16:06:50
package _Sort.Algorithm /** * https://www.geeksforgeeks.org/merge-sort/ * https://www.cnblogs.com/chengxiao/p/6194356.html * best/worst/average Time complexity are O(nlogn), Space complexity is O(n), stable * # is Divide and Conquer algorithm * Basic idea * 1. find the middle point to divide the array into two halves; * 2. Call MergeSort for first halves; * 3. Call MergeSort for second halves; * 4. Merge two halves; * */ class MergeSort { fun sortArray(array: IntArray): IntArray { //create temp array first, avoid create temp in recursion val tempArray = IntArray(array.size) val left = 0 val

排序算法原理及实现

醉酒当歌 提交于 2020-03-26 01:43:02
插入排序 1 public static void insertSort(Comparable[] array) 2 { 3 int j = 0; 4 5 for (int i = 1; i < array.length; i++) { 6 Comparable temp = array[i]; 7 8 for (j = i; j > 0 && (temp.compareTo(array[j - 1]) < 0); j--) { 9 array[j] = array[j - 1]; 10 } 11 array[j] = temp; 12 } 13 } 归并排序 1 public static void mergeSort(Comparable[] array) 2 { 3 Comparable[] copy = new Comparable[array.length]; 4 mergeSort(array, copy, 0, array.length - 1); 5 } 6 7 8 private static void mergeSort(Comparable[] array, Comparable[] temp, int left, int right) 9 { 10 if (left < right) { 11 int middle = (left + right) >> 1

java各种排序算法及实现

不羁岁月 提交于 2020-03-26 00:18:44
3 月,跳不动了?>>> 先来看看8种排序之间的关系: 下图是各种排序的比较: 1, 直接插入排序 (1)基本思想:在要排序的一组数中,假设前面(n-1) [n>=2] 个数已经是排 好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数 也是排好顺序的。如此反复循环,直到全部排好顺序。 在插入算法中,如果有一个最小的数在数组的最后面,用插入算法就会从最后一个 位置移动到第一个。 (2)实例 package cglib; public class StringNumber { public static void insertSort(int[] a) { if (a == null || a.length < 2) { return; } int length=a.length; //数组长度 int j; //当前值的位置 int i; //指向j前的位置 int key; //当前要进行插入排序的值 //从数组的第二个位置开始遍历值 for(j=1;j<length;j++){ key=a[j]; i=j-1; System.out.println(" 将i="+i); //a[i]比当前值大时,a[i]后移一位,空出i的位置,好让下一次循环的值后移 while(i>=0 && a[i]>key){ System.out.println("进 i="+i); a[i+1]

JavaScript数组去重

不羁岁月 提交于 2020-03-23 16:54:41
数组去重,一般都是在面试的时候才会碰到,一般是要求手写数组去重方法的代码。如果是被提问到,数组去重的方法有哪些?你能答出其中的10种,面试官很有可能对你刮目相看。 在真实的项目中碰到的数组去重,一般都是后台去处理,很少让前端处理数组去重。虽然日常项目用到的概率比较低,但还是需要了解一下,以防面试的时候可能回被问到。 注:写的匆忙,加上这几天有点忙,还没有非常认真核对过,不过思路是没有问题,可能一些小细节出错而已。 数组去重的方法 一、利用ES6 Set去重(ES6中最常用) function unique (arr) { return Array.from(new Set(arr)) } var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}]; console.log(unique(arr)) //[1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {}, {}] 不考虑兼容性,这种去重的方法代码最少。这种方法还无法去掉“{}”空对象,后面的高阶方法会添加去掉重复“{}”的方法。 二、利用for嵌套for

[Javascript] Use an Array of Promises with a For Await Of Loop

妖精的绣舞 提交于 2020-03-22 03:55:34
The "for await of" loop syntax enables you to loop through an Array of Promises and await each of the responses asynchronously. This lesson walks you through creating the array and awaiting each of the results. let promises = [ Promise.resolve(1), Promise.resolve(2), new Promise(resolve => { setTimeout(() => { resolve(3) }, 3000) }) ] async function start() { for await (let promise of promises) { console.log(promise) } } start() It will log out 1 2 At once.. Then after 3 seconds, log 3 来源: https://www.cnblogs.com/Answer1215/p/12543891.html

【LeetCode】18.Array and String — Remove Duplicates from Sorted Array 从已排序的数组中删除重复项

拥有回忆 提交于 2020-03-21 07:30:22
Given a sorted array nums , 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 by modifying the input array in-place with O(1) extra memory. Example 1: Given nums=[1,1,2] Your function should return length =2with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the returned length. Example 2: Given nums=[0,0,1,1,1,2,2,3,3,4], Your function should return length =5, with the first five elements of nums being modified to 0,1,2,3 and 4

8. 基数排序

隐身守侯 提交于 2020-03-20 16:31:32
思想 基数排序,是按照元素的更小元素组成来进行排序。拿常用的数字排序举例,会先对个位进行排序,然后对十位进行排序。 其中,关键是如何对个位进行排序,如何对十位进行排序。 我们知道对于数字的每一位,只有10个数字(0-9),这就是基数。所以故名,基数排序。基数排序所以可以理解为适用于基数是有限的情况下。 所以基数排序中的基数,相当于把计数排序中的计数数组的大小确定下来了。 那么如何对个位、十位进行排序呢?可以用到计数排序。 实现 import java.util.Arrays; public class RadixSort { public static void main(String[] args) { int[] array = { 1200, 292, 121, 72, 233, 44, 12 }; radixSort(array, 10, 4); System.out.println(Arrays.toString(array)); } public static void radixSort(int[] array, int radix, int d){ int[] tempArray = new int[array.length]; int[] count = new int[radix]; int rate = 1; for (int i = 0; i < d; i+

如何将值和键都推送到数组中

本小妞迷上赌 提交于 2020-03-20 14:47:23
3 月,跳不动了?>>> 看看这段代码: $GET = array(); $key = 'one=1'; $rule = explode('=', $key); /* array_push($GET, $rule[0] => $rule[1]); */ 我正在寻找这样的东西,以便: print_r($GET); /* output: $GET[one => 1, two => 2, ...] */ 有这个功能吗? (因为 array_push 不会这样工作) #1楼 您可以使用union运算符( + )组合数组并保留添加的数组的键。 例如: <?php $arr1 = array('foo' => 'bar'); $arr2 = array('baz' => 'bof'); $arr3 = $arr1 + $arr2; print_r($arr3); // prints: // array( // 'foo' => 'bar', // 'baz' => 'bof', // ); 所以你可以做 $_GET += array('one' => 1); 。 有关union运算符与 array_merge 的使用的更多信息,请参阅 http://php.net/manual/en/function.array-merge.php 中的文档。 #2楼 不,没有关联数组的 array_push

numpy.where()的用法

允我心安 提交于 2020-03-20 13:23:55
  1. np.where(condition, x, y)   满足条件(condition),输出x,不满足输出y。 arr = np.arange(10) out = np.where(arr%2 == 1,-1,arr) print(out) 4 >>> [ 0 -1 2 -1 4 -1 6 -1 8 -1]   2. np.where(condition)    只有条件 (condition),没有x和y,则输出满足条件 (即非0) 元素的坐标。这里的坐标以tuple的形式给出,通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标。 a = np.array([1,3,5,7,9]) print(np.where(a>5)) print(a[np.where(a>5)]) >>> (array([3, 4], dtype=int64),) [7 9]   3. 给where()函数传递一个条件数组和两个值或数组,对于条件数组中等价于True的位置,从第一个值或数组中取值进行替换,否则从第二个值或数组中取值进行替换。  x = np.array([1,2,3,4,5,6]) y = np.array([[1,0,3],[4,5,0]]) print(np.where(x%2 == 1,x,-x)) print(np.where(y%2 ==

Python 关于数组矩阵变换函数numpy.nonzero(),numpy.multiply()用法

安稳与你 提交于 2020-03-20 13:23:01
1.numpy.nonzero(condition),返回参数condition(为数组或者矩阵)中非0元素的索引所形成的ndarray数组,同时也可以返回condition中布尔值为True的值索引,其中,数值0为False,其余的都为True。 1 >>>b=np.mat(np.arange(10)).T 2 >>>b 3 matrix([[0], 4 [1], 5 [2], 6 [3], 7 [4], 8 [5], 9 [6], 10 [7], 11 [8], 12 [9]]) 13 >>>np.nonzero(b>2) 14 (array([3, 4, 5, 6, 7, 8, 9], dtype=int64), 15 array([0, 0, 0, 0, 0, 0, 0], dtype=int64)) 16 >>>np.nonzero((b.A>2)*(b.A<8)) 17 (array([3, 4, 5, 6, 7], dtype=int64), array([0, 0, 0, 0, 0], dtype=int64)) 1 >>> x = np.eye(3) 2 >>> x 3 array([[ 1., 0., 0.], 4 [ 0., 1., 0.], 5 [ 0., 0., 1.]]) 6 >>> np.nonzero(x) 7 (array([0, 1, 2]),