array

Day8 - B - Non-Secret Cypher CodeForces - 190D

主宰稳场 提交于 2020-01-22 14:48:02
Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their native land, the berlanders need to know exactly how many more flatland soldiers are left in the enemy's reserve. Fortunately, the scouts captured an enemy in the morning, who had a secret encrypted message with the information the berlanders needed so much. The captured enemy had an array of positive integers. Berland intelligence have long been aware of the flatland code: to convey the message, which contained a number m , the enemies use an array of integers a . The number of its subarrays, in

OJ题-设计循环队列(力扣)

泪湿孤枕 提交于 2020-01-22 09:59:38
题目: 思路: 不是每次都进行数字搬移(直到后面没空间了再一次性搬移到前面) //数组中实现对列 class MyCircularQueue { private int [ ] array ; //存储空间 private int size ; //当前数据个数 private int front ; //指向队首下标 private int rear ; //指向队尾下一个可用空间 /** Initialize your data structure here. Set the size of the queue to be k. */ public MyCircularQueue ( int k ) { //容量 array = new int [ k ] ; size = 0 ; front = 0 ; rear = 0 ; } /** Insert an element into the circular queue. Return true if the operation is successful. */ public boolean enQueue ( int value ) { //插入成功返回true if ( size == array . length ) { //满了不成功 return false ; } array [ rear ] = value ;

算法导论——求最大子数组问题

倖福魔咒の 提交于 2020-01-22 08:48:05
利用分治算法求最大子数组问题 #include<iostream> #include<stdio.h> #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable:4996) int const N = 100; using namespace std; struct ARRAY { int high; int low; int sum; }; int A[N]; ARRAY FIND_MAX_SUBARRAY(int* A, int low, int high); ARRAY FIND_MAX_CROSSING_SUBARRAY(int* A, int low, int mid, int high); int main() { int i; int n=0; for (i = 1; i <= N-1; i++) { cin >> A[i]; n++; char c;//= cin.get(); c = getchar(); if (c != ' ') break; } printf("\n"); ARRAY array; array= FIND_MAX_SUBARRAY(A, 1, n); for (i = array.low; i <= array.high; i++) { printf("%d ", A[i]); }

数组的连续最大子段和

限于喜欢 提交于 2020-01-22 08:37:33
   问题描述 :输入是一个大小为n的整型数组,要求输出数组的任何连续子数组中的最大值。例如:输入的数组为array[10] = {31,-41,59,26,-53,58,97,-93,-23,84};输出最大连续子数组和为array[2...6]:187    算法1: 对所有满足0<=i<=j<=n的(i,j)整数对进行迭代,对每个整数对,程序都要计算array[i...j]的总和,并检验该总和是否大于迄今为止的最大总和。 算法1的伪代码描述如下: 1 maxsofar = 0 2 for(i=0;i<n;++j) 3 for(j=i;j<n;++j) 4 tmepsum = 0 5 for(k=i;k<=j;++k) 6 tempsum += array[k] 7 maxsofar = max(maxsofar,tempmax) 这段代码简洁明了,便于理解,但是程序执行的速度很慢,时间复杂度为O(n^3)。    算法2: 对于算法1有一个明显的方法可以使其运行起来快得多。使得时间复杂度控制住平方O(n^2)。 第一个平方算法注意到,array[i...j]的总和与前面计算出的总和(array[i...j-1])密切相关,利用这一点可以达到算法2。 算法2_1的伪代码描述如下: 1 maxsofar = 0 2 for(i=0;i<n;++i) 3 tempsum = 0;

numpy的基础知识

最后都变了- 提交于 2020-01-22 05:52:53
numpy属性 > import numpy as np > array = np . array ( [ [ 1 , 2 , 3 ] , [ 2 , 3 , 4 ] ] ) > print ( array ) > print ( array . ndim ) > print ( array . shape ) #行列 > print ( array . size ) #共有多少元素 > numpy创建array > import numpy as np > a = np . array ( [ 2 , 23 , 4 ] , dtype = np . int ) #定义整数,也可定义浮点数 > b = np . zeros ( ( 3 , 4 ) ) #定义一个3行4列的零矩阵 > c = np . ones ( ( 3 , 4 ) ) > d = np . arange ( 10 , 20 , 2 ) #定义10到20间隔为2的数列 > a = np . arange ( 12 ) . reshape ( ( 3 , 4 ) ) > a = np . linspace ( 1 , 10 , 5 ) #生成1-10均分为5段的数列 > numpy的基础运算 #加减乘除 > c = a + b > print ( b < 3 ) #判断b中哪些元素<3 > c = a * b

【算法】选择排序

一曲冷凌霜 提交于 2020-01-21 19:36:51
选择排序是一种较为简单直观的算法,简单暴力易看懂(代价就是效率较低)。 其原理就是不断遍历数组,每遍历一轮都选择出一个最值放置到前方使其有序排列,然后再遍历剩余的无序元素,依此类推,直至所有元素都有序排列。 现有一数组 int[] array = {3, 5, 6, 1, 8, 7, 4, 9, 2, 10} ,以升序排列为例,其排序过程如下: 代码如下: public static void main(String[] args) { int[] array = {3, 5, 6, 1, 8, 7, 4, 9, 2, 10}; for (int i = 0; i < array.length; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i] > array[j]) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; } } } } 让我们思考一下,上面这种方法的效率会不会偏低了呢? 每一轮其实只需要筛选出一个最小值放到前面即可,根本没必要在中间过程中经过多次多余的交换。 我们只需要声明一个变量来记住最小值的索引,然后在每一轮遍历的最后根据最小值的索引值将这个最小值放置到前面正确的位置即可。 来看看改进过的代码: for (int i

【javascript 面试笔试】1、几道笔试题

被刻印的时光 ゝ 提交于 2020-01-20 21:11:14
今天想起来几道javascript的面试题,大家做做看看,有别的思路可以在下面写出来,大家交流一下 (1) 将多维数组转化成一个一位数组,例如[1,[2,3],[4,5,[6,7]]]转化成[1,2,3,4,5,6,7],答案在下面,主要就是利用递归 function test(array){ var newArray = []; for (var i=0,length = array.length - 1; i <= length; i++) { if(array[i] instanceof Array){//判断是不是数组 newArray = newArray.concat(test(array[i]));//递归调用 }else{ newArray.push(array[i]); } }; return newArray; } (2) 求数组的最大维数,即一共嵌套了多少层,例如[1,2,[3,[4,5,[6]],[7,[8]]]] 一共嵌套了4层,还是利用递归,主要代码在下面 function getDim(array){ var dim = []; for (var i =0,length = array.length - 1; i <= length; i++) { if(array[i] instanceof Array){ dim[i] = getDim(array

215. Kth Largest Element in an Array

送分小仙女□ 提交于 2020-01-20 16:36:18
https://www.jianshu.com/p/b30955885e6d Find the k th largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Approach 1: Priority Queue class Solution { public int findKthLargest ( int [ ] nums , int k ) { PriorityQueue < Integer > minQ = new PriorityQueue < > ( k ) ; for ( int i : nums ) { if ( minQ . size ( ) < k ) { minQ . add ( i ) ; continue ; } if ( i <= minQ . peek ( ) ) continue ; minQ . remove ( ) ; minQ . add ( i ) ; } return minQ . peek ( ) ; } } Runtime: 8 ms, faster than 46.78% of Java online submissions for

这些Zepto中实用的方法集

爱⌒轻易说出口 提交于 2020-01-19 14:09:22
前言 时间过得可真快,转眼间2017年已去大半有余,你就说吓不吓人,这一年你成长了多少,是否荒度了很多时光,亦或者天天向上,收获满满。今天主要写一些看Zepto基础模块时,比较实用的部分内部方法,在我们日常工作或者学习中也会用的到。 源码仓库 原文链接 1. 将数组铺平(flatten) 面试或者工作中经常遇到要将多维数组铺平成一维数组。例如将 [1, 2, [3], [4], [5]] 最后变成 [1, 2, 3, 4, 5] function flatten(array) { return array.length > 0 ? $.fn.concat.apply([], array) : array } 这里先将 $.fn.concat 理解成原生数组的 concat方法 ,我们会发现,其实他只能铺平一层。例如 [1, 2, [3], [4, [5]]] => [1, 2, 3, 4, [5]] 那怎样才能将多层嵌套的数组完全铺平为一层呢?这里介绍两种方式。 方式1 let flatten = (array) => { return array.reduce((result, val) => { return result.concat(Array.isArray(val) ? flatten(val) : val) }, []) } 测试 let testArr1 = [1,