array

日常积累 web.js

ぃ、小莉子 提交于 2019-12-28 01:12:10
var V = { }; var AP = ap = Array.prototype; var OP = op = Object.prototype; var objEqual = V.objEqual = function(objA, objB){ if (typeof arguments[0] != typeof arguments[1]) return false; //数组 if (arguments[0] instanceof Array){ if (arguments[0].length != arguments[1].length) return false; var allElementsEqual = true; for (var i = 0; i < arguments[0].length; ++i){ if (typeof arguments[0][i] != typeof arguments[1][i]){ return false; } if (typeof arguments[0][i] == 'number' && typeof arguments[1][i] == 'number'){ allElementsEqual = (arguments[0][i] == arguments[1][i]); } else{ allElementsEqual =

【STL记录】Containers--Arrays

杀马特。学长 韩版系。学妹 提交于 2019-12-27 18:25:45
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Array是container class array<>的一个实例,是一个固定长度的元素序列。 在使用array之前,需要include头文件: #include <array> 一、初始化 // all elements of x have value 0 (int()) array<int,4> x = {}; //use an initializer list to initialize array array<int,5> coll = { 42, 377, 611, 21, 44 }; // one element with value 42, followed by 9 elements with value 0 array<int,10> c2 = { 42 }; 二、Array Operations 1.Class array<> 的构造函数 Table 1. Constructors of Class array<> Operation Effect array<Elem, N> c 默认构造函数:使用默认初始化元素创建一个array array<Elem, N> c(c2) 复制构造函数:通过复制创建一个相同的array(所有元素都被复制) array<Elem, N> c = c2

你真的会写二分查找吗

北战南征 提交于 2019-12-27 16:42:07
1 二分查找   二分查找是一个基础的算法,也是面试中常考的一个知识点。二分查找就是将查找的键和子数组的中间键作比较,如果被查找的键小于中间键,就在左子数组继续查找;如果大于中间键,就在右子数组中查找,否则中间键就是要找的元素。 (图片来自《算法-第4版》) /** * 二分查找,找到该值在数组中的下标,否则为-1 */ static int binarySerach(int[] array, int key) { int left = 0; int right = array.length - 1; // 这里必须是 <= while (left <= right) { int mid = (left + right) / 2; if (array[mid] == key) { return mid; } else if (array[mid] < key) { left = mid + 1; } else { right = mid - 1; } } return -1; }   每次移动left和right指针的时候,需要在mid的基础上+1或者-1, 防止出现死循环, 程序也就能够正确的运行。   注意:代码中的判断条件必须是while (left <= right),否则的话判断条件不完整,比如:array[3] = {1, 3, 5};待查找的键为5,此时在(low <

《机器学习实战》实现时遇到的问题

只谈情不闲聊 提交于 2019-12-27 11:28:21
《 机器学习 实战》第二章k-近邻 算法 ,自己实现时遇到的问题,以及解决方法。做个记录。 报错:only 2 non-keyword arguments accepted。 问题所在:粗心少写了两个中括号 本来是array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]),结果少写了最外面的两个中括号 from numpy import * import operator def createDataSet(): group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) labels = ['A','A','B','B'] return group,labels def classify0(inX,dataSet,labels,k): dataSetSize = dataSet.shape[0] diffMat = tile(inX,(dataSetSize,1)) - dataSet sqDiffMat = diffMat**2 sqDistances = sqDiffMat.sum(axis = 1) distances = sqDistances**0.5 sortedDistIndicies = distances.argsort() classCount = {} for i in range(k):

Codeforces Round #371 (Div. 2) B. Filya and Homework 水题

百般思念 提交于 2019-12-27 06:52:38
B. Filya and Homework 题目连接: http://codeforces.com/contest/714/problem/B Description Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer x and change

Educational Codeforces Round 42 (Rated for Div. 2)

て烟熏妆下的殇ゞ 提交于 2019-12-27 06:20:28
                                        A. Equator Polycarp has created his own training plan to prepare for the programming contests. He will train for n n days, all days are numbered from 1 1 to n n, beginning from the first. On the i i-th day Polycarp will necessarily solve a i ai problems. One evening Polycarp plans to celebrate the equator. He will celebrate it on the first evening of such a day that from the beginning of the training and to this day inclusive he will solve half or more of all the problems. Determine the index of day when Polycarp will celebrate the equator. Input The

剑指Offer 和为S的两个数字

巧了我就是萌 提交于 2019-12-27 06:08:09
题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。 输出描述: 对应每个测试案例,输出两个数,小的先输出。思路:头尾指针,向中间走。第一组数据肯定是积最小的。 1 class Solution { 2 public: 3 vector<int> FindNumbersWithSum(vector<int> array,int sum) { 4 5 int i=0,j=array.size()-1; 6 vector<int> res; 7 while(i<j) 8 { 9 if(array[i]+array[j]<sum) 10 i++; 11 else if(array[i]+array[j]>sum) 12 j--; 13 else 14 { 15 res.push_back(array[i]); 16 res.push_back(array[j]); 17 return res; 18 } 19 20 } 21 return res; 22 } 23 }; 来源: https://www.cnblogs.com/SeekHit/p/5846298.html

Numpy模块

五迷三道 提交于 2019-12-27 05:22:08
Numpy模块: 通过numpy创建数组 数组(ndarry)的属性: import numpy as np np.ndim(数组的维度:返回值为int) np.shape(数组的尺寸,返回值是tuple) np.size(数组的大小,返回值是int) np.dtype(描述数组的类型) np.itemsize(表示数组每个元素的大小) 通过Numpy创建数组: 创建一维数组: array=np.array([1,2,3,4]) print(array) [1 2 3 4] 创建二维数组: arry=np.array([[1,2,3,4],[3,4,5,6]]) print(arry) [[1 2 3 4] [3 4 5 6] 通过arange创建数组: array2=np.arange(1,10,1) print(array2) [1 2 3 4 5 6 7 8 9] 通过等差数列创建数组: array=np.linspace(1,2,5) print(array) [ 1. 1.25 1.5 1.75 2. ] 通过等比数列创建数组: array=np.logspace(2,4,10) print(array) [ 100. 166.81005372 278.25594022 464.15888336 774.26368268 1291.54966501 2154

LeetCode Wiggle Sort II

我的未来我决定 提交于 2019-12-27 05:16:07
Given an unsorted array nums , reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... . Example 1: Input: nums = [1, 5, 1, 1, 6, 4] Output: One possible answer is [1, 4, 1, 5, 1, 6] . Example 2: Input: nums = [1, 3, 2, 2, 3, 1] Output: One possible answer is [2, 3, 1, 3, 1, 2] . Note: You may assume all input has valid answer. Follow Up: Can you do it in O(n) time and/or in-place with O(1) extra space? 这个题目,感觉好像很简单,就是把给定的数组进行排序,排序完成后,取一个最小元素然后再取一个最大元素好像就可以。但是经过测试用例的测试发现,有的数组包括很多大小重复的数字,这样的话,无论如何排序,始终有一个元素会出现等于的情况,就好比例子中的第二个,所以,排序好像是不行的。 最好的方法是将数组排序后分成两个数组,一个数组中装最大的数字partMax[]

后缀数组

旧巷老猫 提交于 2019-12-27 04:24:18
追随蔡大神的脚步,开始后缀数组的学习。 http://www.cnblogs.com/EC-Ecstasy/ //时间不够不定时不定期完善 一、后缀数组的定义 把一个字符串的后缀全部搞出来,比如“aabaaaab”的后缀就是"aabaaaab”,“abaaaab”,“baaaab”,“aaaab”,“aaab”,“aab”,“ab”,“b”,分别编号为1,2,3,4,5,6,7,8。 然后就有两个数组,一个是rank[],一个是sa[]。rank[i]表示第i个后缀排在第几名,sa[i]表示排第i名是哪个后缀。显然这两个数组为逆运算。(sa[rank[i]]=i,rank[sa[i]]=i) 基排倍增写法。 每次倍增,分两个关键字。 模版1(远古写法) var s:ansistring; n,tot:longint; c,x,y,rank,sa:array[0..1000]of longint; procedure first; var i:longint; begin readln(s); n:=length(s); for i:=1 to n do x[i]:=ord(s[i]); fillchar(c,sizeof(c),0); for i:=1 to n do inc(c[x[i]]); for i:=1 to 128 do inc(c[i],c[i-1]); for i: