array

移除有序数组的重复数字(Remove Duplicates from Sorted Array)

被刻印的时光 ゝ 提交于 2020-01-27 06:23:55
题目 Given a sorted array, 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 in place with constant memory. For example, Given input array A = [1, 1, 2], Your function should return length = 2, and A is now [1, 2] 题目大概意思是说给你要一个有序的数组,把里面的重复的部分删掉使得这个数组的每个元素就出现一次,然后返回移除后的数组长度。要求:不同使用额外的数组空间,只能在这个数组里面完成。 分析 题目比较简单,已知数组有序,只保留一个,其实就是去重处理。可以使用双重指针来做:新数组指针和旧数组指针,旧数组指针用于遍历整个数组,新数组指针从用于存储新的去重后的数据。 代码 /************************************** * Given a sorted array, remove the duplicates in place

doT.js详细介绍

天涯浪子 提交于 2020-01-27 03:36:58
doT.js详细介绍 doT.js特点是快,小,无依赖其他插件。 官网: http://olado.github.io doT.js详细使用介绍 使用方法: {{= }} for interpolation {{ }} for evaluation {{~ }} for array iteration {{? }} for conditionals {{! }} for interpolation with encoding {{# }} for compile-time evaluation/includes and partials {{## #}} for compile-time defines 调用方式: var tmpText = doT.template(模板); tmpText(数据源); 例子一: 1、for interpolation 赋值 格式: {{= }} 数据源:{"name":"Jake","age":31} 区域:<div id="interpolation"></div> 模板: <script id="interpolationtmpl" type="text/x-dot-template"> <div>Hi {{=it.name}}!</div> <div>{{=it.age || ''}}</div> </script> 调用方式: var

剑指Offer刷题记录 1-10

孤者浪人 提交于 2020-01-27 00:44:55
马上要面试了,把前些天在剑指Offer上刷的题再看一遍。 写上注释,算复习了,也方便以后复习。 带 * 号的为忘记怎么做的。 1. 二维数组中的查找 (数组优化) class Solution { public: bool Find(int target, vector<vector<int> > array) { int row_num = array.size(); if (row_num == 0) return false; int col_num = array[0].size(); // 从左下角开始找 int i = row_num - 1, j = 0; while (i >= 0 && j < col_num) { // 对于每个位置有3种情况 if (array[i][j] == target) return true; else if (array[i][j] > target) i--; else j++; } // 越过边界仍未找到 return false; } }; /* class Solution { public: int binarySearch(vector<int> arr, int target) { // 二分查找,找到返回索引,否则返回-1 int left = 0, right = arr.size() - 1; int mid;

数组处理

旧时模样 提交于 2020-01-27 00:17:18
/*判断一个元素是否在数组中*/ contains (arr, val) { return arr.indexOf(val) != -1 ? true : false; } /** * @param {arr} 数组 * @param {fn} 回调函数 * @return {undefined} */ each (arr, fn) { fn = fn || Function; var a = []; var args = Array.prototype.slice.call(arguments, 1); for(var i = 0; i < arr.length; i++) { var res = fn.apply(arr, [arr[i], i].concat(args)); if(res != null) a.push(res); } } /** * @param {arr} 数组 * @param {fn} 回调函数 * @param {thisObj} this指向 * @return {Array} */ map (arr, fn, thisObj) { var scope = thisObj || window; var a = []; for(var i = 0, j = arr.length; i < j; ++i) { var res = fn.call

二维数组的查找(编程题目)

喜你入骨 提交于 2020-01-26 21:22:07
二维数组的查找 题目分析 暴力求解 O ( n l g n ) O(nlgn) O ( n l g n ) python代码如下: 技巧法 O ( n ) O(n) O ( n ) python代码如下 二维二分查找法 O ( l g n ) O(lgn) O ( l g n ) python代码如下 题目描述:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 题目分析   任何一道算法题,在拿到题目之后,都是首先从基础的题干入手,了不起我看不懂任何题干中的隐藏信息,我直接暴力求解,起码让自己有一定的思路,先能解决问题,对于笔试题目的编程,有可能由于测试用例的量级比较小,直接求解了。而且对于一般的问题(排除很明显的动态规划、或者贪心算法暗示,这种题目暴力法很可能达到指数复杂度)。最最最传统的暴力法没有意义,不做讲解,首先抓住题干中的行(列)内有序,大不了这个有序就可以使用二分查找,同时也要认清楚线性有序,有序基本上必然和二分查找分不开了。 暴力求解 O ( n l g n ) O(nlgn) O ( n l g n )   首先来讲解暴力求解,如上分析,这个暴力求解不是指 O ( n 2 ) O(n^2) O ( n 2 )

NumPy数组

家住魔仙堡 提交于 2020-01-26 14:27:27
NumPy的数组类(numpy.array)中提供了很多便捷的方法,在实现深度学习时,我们将使用这些方法。 # 1.导入NumPy import numpy as np 这条语句就是“将numpy作为np导入”的意思。通过写成这样的形式,之后NumPy相关的方法均可通过np来调用。 # 2.生成NumPy数组 A = np.array([1,2,3]) np.array()接收Python列表作为参数,生成NumPy数组。 # 3.NumPy的算术运算 x = np.array([1,2,3]) y = np.array([2,4,6]) print("对应元素加法x+y=",x+y) print("对应元素减法x-y=",x-y) print("对应元素乘法x*y=",x*y) print("对应元素除法x/y=",x/y) 对应测试结果: # 4.NumPy的N维数组 A = np.array([[1,2],[3,4]]) # 创建二维数组A print(A) # 打印二维数组A print(A.shape) # 查看矩阵A的形状 print(A.dtype) # 查看矩阵A中元素的数据类型 对应测试结果: NumPy数组可以生成N维数组,这里只展示了二维数组(矩阵)的生成。 # 5.广播 A = np.array([[1,2],[3,4]]) B = np.array([10

doT.js详细介绍

荒凉一梦 提交于 2020-01-26 05:57:01
doT.js特点是快,小,无依赖其他插件。 官网: http://olado.github.io doT.js详细使用介绍 使用方法: {{= }} for interpolation {{ }} for evaluation {{~ }} for array iteration {{? }} for conditionals {{! }} for interpolation with encoding {{# }} for compile-time evaluation/includes and partials {{## #}} for compile-time defines 调用方式: var tmpText = doT.template(模板); tmpText(数据源); 例子一: 1、for interpolation 赋值 格式: {{= }} 数据源:{"name":"Jake","age":31} 区域:<div id="interpolation"></div> 模板: <script id="interpolationtmpl" type="text/x-dot-template"> <div>Hi {{=it.name}}!</div> <div>{{=it.age || ''}}</div> </script> 调用方式: var dataInter =

《机器学习实战》学习笔记——k近邻算法

ε祈祈猫儿з 提交于 2020-01-26 03:24:38
1.numpy中一些函数的用法学习 shape()用法: shape : tuple of ints The elements of the shape tuple give the lengths of the corresponding array dimensions.。   shape返回一个元组,依次为 各维度的长度。shape[0]:第一维长度 ,shape[1]:第二维长度。    tile()用法: numpy. tile ( A , reps ) Construct an array by repeating A the number of times given by reps. If reps has length d , the result will have dimension of max(d, A.ndim) . (A.ndim表示A矩阵的维度) If A.ndim < d , A is promoted to be d-dimensional by prepending new axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication, or shape (1, 1, 3) for 3-D replication. If this is not the desired

查找旋转数组中的最小数字

天涯浪子 提交于 2020-01-26 00:58:37
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。 解题思路 看到题目的第一反应,肯定不能直接遍历数组,要寻找时间复杂度小于 O(n) 的方法。由于题目限定的是非递减排序数组的旋转,也就意味着数组的前后可以分成两部分,前一部分的数字都比后一部分的数字大,同时,后一部分的第一个数字就是我们要找的最小数字。尝试使用二分法的变形寻找最小数字。 二分法的主要逻辑共有三种情况( mid,high,low 为数组的角标): array[mid]>array[high] : 这种情况下最小数字肯定在 mid 的右边,所以: low = mid +1; array[mid]==array[high] :此时可能出现的情况比较复杂,比如 [1,1,1,0,1,] 或者 [1,0,1,1,1] ,无法判断最小数字在 mid 左还是右,只能一个一个尝试,所以执行: high = high - 1 array[mid]<array[high] 这种情况下最小最在 mid 右边或者正好位于 mid ,执行: high=mid 注意在二分查找的时候注意 low 可以越界到

The Basics of Numpy

会有一股神秘感。 提交于 2020-01-25 23:17:30
在python语言中, Tensorflow 中的 tensor 返回的是 numpy ndarray 对象。 Numpy 的主要对象是齐次多维数组,即一个元素表(通常是数字),所有的元素具有相同类型,可以通过有序整数列表元组 tuple 访问其元素。 In Numpy , dimensions are called axes. The number of axes is rank. Numpy 的数组类为 ndarray ,它还有一个名气甚大的别名 array 。需要注意的是: numpy.array 与python标准库中的 array.array 并不完全相同,后者仅仅处理一维数组而且提供的函数功能较少。 比较重要的一些 ndarray 数组的属性: ndarray.ndim : the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank. ndarray.shape :the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix