array

C++, Array

余生颓废 提交于 2020-02-11 08:28:59
0. 1. Array Its size is fixed; The size must be known at compile-time; 2. Examples 来源: https://www.cnblogs.com/sarah-zhang/p/12293754.html

1.数组中重复的数字

时光怂恿深爱的人放手 提交于 2020-02-11 06:39:55
题目一 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。 独立思考:哈希法 首先想到用长度为n的辅助数组的方法,时间空间复杂度都是O(n),代码如下: // Parameters: // numbers: an array of integers // length: the length of array numbers // duplication: (Output) the duplicated number in the array number // Return value: true if the input is valid, and there are some duplications in the array number // otherwise false bool duplicate(int numbers[], int length, int* duplication) { //空指针检测 if (numbers == NULL || duplication == NULL) return false; //异常输入检测 for(int

基于Java实现的选择排序算法

微笑、不失礼 提交于 2020-02-11 06:11:46
选择排序和冒泡排序同样是基础排序算法,现在也做个学习积累。 简述 选择排序算法较为稳定,基本上都是O(n 2 )的时间复杂度,规模越小排序越快,不需要占用额外空间。其实选择排序原理很简单,就是在未排序序列中找到最小(大)的元素然后放到数组前面,然后再从剩下的未排序序列中找到最小(大)的元素放在上一次找到最小(大)元素的后面,以此类推完成排序。 动图演示 看下动图上的演示,就能够找出排序规律,非常之简明易懂。 (算法动图来源于参考资料,详细请往下翻阅) 代码实现 1 /** 2 * 选择排序 3 * @param array 待排序数组 4 * @return 已排序数组 5 */ 6 public static int[] selectionSort(int[] array) { 7 int len = array.length; 8 // 如果数组长度为0或者1,都是不用排序直接返回 9 if(len == 0 || len == 1) { 10 return array; 11 } 12 for(int i = 0; i < len - 1; i++) { 13 int minIdx = i; 14 for(int j = i + 1; j < len; j++) { 15 // 找到最小的数 16 if(array[minIdx] > array[j]) { 17 //

[leetcode] Burst Balloons

╄→尐↘猪︶ㄣ 提交于 2020-02-11 05:13:45
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent. Find the maximum coins you can collect by bursting the balloons wisely. Note: (1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them. (2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100 Example: Given [3, 1, 5, 8] Return 167 nums =

排序算法之选择排序

依然范特西╮ 提交于 2020-02-11 03:34:32
一. 算法描述 选择排序:在一个长度为N的无序数组中,在第一趟遍历N个数据,找出其中最小的数值与第一个元素交换,第二趟遍历剩下的N-1个数据,找出其中最小的数值与第二个元素交换......第N-1趟遍历剩下的2个数据,找出其中最小的数值与第N-1个元素交换,至此选择排序完成。 二. 算法分析 平均时间复杂度:O(n2) 空间复杂度:O(1) (用于交换和记录索引) 稳定性:不稳定 (比如序列【5, 5, 3】第一趟就将第一个[5]与[3]交换,导致第一个5挪动到第二个5后面) 三. 算法实现 1 #include<stdio.h> 2 int main() 3 { 4 int array[] = {3, 1, 5, 2, 6 ,4}; 5 int count = sizeof(array) / sizeof(array[0]); 6 for (int i = 0; i < count - 1; i++) { 7 int minIndex = i; 8 for (int j = minIndex + 1; j < count; j++) { 9 if (array[minIndex] > array[j]) { 10 minIndex = j; 11 } 12 } 13 if (minIndex != i) { 14 int temp = 0; 15 temp = array

javascript中对象的深度克隆

夙愿已清 提交于 2020-02-10 23:41:42
本文目录: 零、寒暄 一、js中的对象 二、克隆的概念 三、浅克隆的表现 四、深克隆的实现 五、总结 零、寒暄 又是一个月多月没有更新博客了,这段时间回学校处理下论文的事情,实习的生活也暂时告一段落(在公司上班,才发现学校里面的生活简直如天堂一般,相信很多已经毕业的小伙伴肯定被我说中了,说中了请给本文点个赞,哈哈!)。希望接下来自己的更新进度能加快,马上又是一年校招时,被虐也好、大牛虐别人也罢,总之祝福各位今年要找工作的小伙伴们好运。那么,今天就聊一下一个常见的笔试、面试题,js中对象的深度克隆。翻了下这个题目,在很多地方出现过,已经算一个老的题目了,但是每年的校招中总会考到,其实想想,这个题目考查的知识点还是蛮多的,尤其是对基础知识的考查。好了,闲话不多说,开始正题。 一、js中的对象 谈到对象的克隆,必定要说一下对象的概念。 js中的数据类型分为两大类:原始类型和对象类型。(1)原始类型包括:数值、字符串、布尔值、null、undefined(后两个是特殊的原始值,这里不做详细的说明,我的上一篇博客有谈到过一些)(2)对象类型包括:对象即是属性的集合,当然这里又两个特殊的对象----函数(js中的一等对象)、数组(键值的有序集合)。 好了既然对象分为这两类,这两种类型在复制克隆的时候是有很大区别的。原始类型存储的是对象的实际数据,而对象类型存储的是对象的引用地址

理解numpy中ndarray的内存布局和设计哲学

核能气质少年 提交于 2020-02-10 22:31:51
目录 ndarray是什么 ndarray的设计哲学 ndarray的内存布局 为什么可以这样设计 小结 参考 博客: 博客园 | CSDN | blog 本文的主要目的在于理解 numpy.ndarray 的内存结构及其背后的设计哲学。 ndarray是什么 NumPy provides an N-dimensional array type, the ndarray , which describes a collection of “items” of the same type . The items can be indexed using for example N integers. —— from https://docs.scipy.org/doc/numpy-1.17.0/reference/arrays.html ndarray是numpy中的 多维数组 ,数组中的元素具有 相同的类型 ,且可以被 索引 。 如下所示: >>> import numpy as np >>> a = np.array([[0,1,2,3],[4,5,6,7],[8,9,10,11]]) >>> a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> type(a) <class 'numpy.ndarray'> >

C. Anu Has a Function

∥☆過路亽.° 提交于 2020-02-10 12:58:29
链接: https://codeforces.com/contest/1300/problem/C Anu has created her own function ff: f(x,y)=(x|y)−yf(x,y)=(x|y)−y where || denotes the bitwise OR operation . For example, f(11,6)=(11|6)−6=15−6=9f(11,6)=(11|6)−6=15−6=9. It can be proved that for any nonnegative numbers xx and yy value of f(x,y)f(x,y) is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array [a1,a2,…,an][a1,a2,…,an] is defined as f(f(…f(f(a1,a2),a3),…an−1)

剑指offer-调整数组顺序使奇数位于偶数前面

痴心易碎 提交于 2020-02-10 07:31:51
题目:调整数组顺序使奇数位于偶数前面 题目描述:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。 思路:这道题应该是考察排序的,相对位置不变意味着排序算法要稳定 但是如果允许额外开辟空间的话,只需新建一个数组,将原数组的奇数放到新数组前面,偶数放到后面,便可以达到时间复杂度O(n),空间复杂度O(N) 那么,先来开个数组,暴力解下: 1 public class Solution { 2 public void reOrderArray(int [] array) { 3 int oddStart=0; 4 int count=0; 5 int []a=new int[array.length]; 6 for(int i=0;i<array.length;i++){ 7 if((array[i]&1)==1) count++; 8 } 9 for(int i=0;i<array.length;i++){ 10 if((array[i]&1)==1)a[oddStart++]=array[i]; 11 else{ 12 a[count++]=array[i]; 13 } 14 } 15 for(int i=0;i<array.length;i++){ 16

【php复习之】php创建数组的几种方式

回眸只為那壹抹淺笑 提交于 2020-02-09 17:47:35
1、array()函数 1.1无key值    $arr=array(1,2,3,4); 1.2键值对    $arr=array(    'name'=>'myj',    'age'=>'18',    'phone'=>'1888888888'  ); 1.3空数组   $arr=array(); 2、compact()函数 compact函数可以把变量转换为数组。 $a = 'aaa'; $b = 'bbb'; $c = 'ccc'; $arr3 = compact('a','b','c'); 输出: {"a":"aaa","b":"bbb","c":"ccc"} 3、array_combine()函数 array_combine()函数可以将两个数组合并成一个新数组,其中的一个数组是键名,另一个数组的值为键值。 $arr_key = array('a','b','c','d'); $arr_val = array('1','2','3','4'); echo var_dump(array_combine($arr_key,$arr_val)); 输出: 'a' => string '1' (length=1) 'b' => string '2' (length=1) 'c' => string '3' (length=1) 'd' => string '4'