array

852. Peak Index in a Mountain Array

我怕爱的太早我们不能终老 提交于 2020-01-25 21:56:08
852. 山脉数组的峰顶索引 我们把符合下列属性的数组 A 称作山脉: A.length >= 3 存在 0 < i < A.length - 1 使得 A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] 给定一个确定为山脉的数组,返回任何满足 A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] 的 i 的值。 示例 1: 输入:[0,1,0] 输出:1 示例 2: 输入:[0,2,1,0] 输出:1 提示: 3 <= A.length <= 10000 0 <= A[i] <= 10^6 A 是如上定义的山脉 解法一 //时产复杂度O(n), 空间复杂度O(1) class Solution { public: int peakIndexInMountainArray(vector<int>& A) { int n = A.size(), i; for(i = 1; i < n; i++) { if(A[i - 1] < A[i]) continue; break; } return i - 1; } }; 解法二 //时间复杂度O(logn), 空间复杂度O(1) class Solution { public: int

正则实现整数运算

不羁岁月 提交于 2020-01-25 16:46:58
加 const sum = ( m , n ) => { m = Array ( m + 1 ) . join ( '#' ) ; n = Array ( n + 1 ) . join ( '#' ) ; return m . replace ( /$/ , n ) . length ; } ; sum ( 3 , 2 ) ; // 5 减 const diff = ( m , n ) => { m = Array ( m + 1 ) . join ( '#' ) ; n = Array ( n + 1 ) . join ( '#' ) ; return m . replace ( n , '' ) . length ; } ; diff ( 3 , 2 ) ; // 1 乘 const product = ( m , n ) => { m = Array ( m + 1 ) . join ( '#' ) ; n = Array ( n + 1 ) . join ( '#' ) ; return m . replace ( /\#/g , n ) . length ; } ; product ( 3 , 2 ) ; // 6 除 const division = ( m , n ) => { m = Array ( m + 1 ) . join ( '#' ) ; n =

最长递增子序列

限于喜欢 提交于 2020-01-25 05:16:38
《编程之美》2.16节  这是一个DP(动态规划)问题。 以串1, -1, 2, -3, 4, -5, 6, -7为例,递归子结构为: incr[i] = max{1, LIS[k]+1};对k <i, 且array[k] < array[i]  LIS表示以array[i]为最大元素的最长递增子序列的长度.prev[N]用于存储该元素的前驱元素。 1 def findInc(array): 2 incr = [1] * len(array) 3 prev = [-1] * len(array) #record the sequence's previous element 4 5 for i in range(len(array)): 6 for j in range(0, i): 7 if array[j] < array[i] and incr[i] < incr[j]+1: 8 incr[i] = incr[j] + 1 9 prev[i] = j 10 11 print "The length is " + str(max(incr)) 12 print "The subsequence is " 13 inx = incr.index(max(incr)) 14 result = [] 15 while prev[inx] != -1: 16 result.append

HTML&PHP学习笔记(4)

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-25 04:51:47
创建数组: $products = array('a','b','c'); range(1,10);  //1-10的数字数组 访问数组内容: $products[0]; $products[3] = 'd'; //增加元素 循环访问: for ($i = 0; $i < 3; $i++){ echo $products[$i]." "; } foreach ($products as $current){ echo $current. " "; } 关联索引: $prices = array('a'=>100, 'b'=>10, 'c'=>4); 循环: foreach ($prices as $key => $value) { echo $key." - ".$value."<br />"; } or while ($element = each($price)){ echo $element['key']; echo " - "; echo $element['value']; echo "<br />"; } or while (list($product, $price) = each($prices)){ echo "$product - $price<br />"; } each() 将记录当前元素,使用reset()重置到开始处。 多维数组: $products =

C. Increasing by Modulo

耗尽温柔 提交于 2020-01-25 03:40:43
链接: https://codeforces.com/contest/1169/problem/C Toad Zitz has an array of integers, each integer is between 00 and m−1m−1 inclusive. The integers are a1,a2,…,ana1,a2,…,an. In one operation Zitz can choose an integer kk and kk indices i1,i2,…,iki1,i2,…,ik such that 1≤i1<i2<…<ik≤n1≤i1<i2<…<ik≤n. He should then change aijaij to ((aij+1)modm)((aij+1)modm) for each chosen integer ijij. The integer mm is fixed for all operations and indices. Here xmodyxmody denotes the remainder of the division of xx by yy. Zitz wants to make his array non-decreasing with the minimum number of such operations.

doT.js详细介绍

*爱你&永不变心* 提交于 2020-01-25 02:02:21
官网: 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 = {"name":"Jake","age"

直接插入排序

二次信任 提交于 2020-01-24 19:43:29
直接插入排序 时间复杂度: O(N ^ 2) (最坏情况下 - 无序) O(N) (最好情况下 - 有序) 稳定性 :稳定 实现原理: 简单的说,就是把数组划分为两个部分,一部分已经有序(在最开始的时候,就把第一个元素,看成有序的那部分),一部分未排序; 每次从未排序的部分,取第一个元素,然后遍历有序部分,进行比较,找到位置以后,就将这个元素插进去。 具体做法就是,找到位置以后,不断把已经有序的元素后移,给待插入的元素让位。 移动完以后,再把元素放到那个为它空出来的数组位置。这样,一趟插入排序就完成了。 代码示例: public static void insertSort ( int [ ] array ) { for ( int i = 1 ; i < array . length ; i ++ ) { int tmp = array [ i ] ; int j = i - 1 ; for ( j = i - 1 ; j >= 0 ; j -- ) { if ( array [ j ] > tmp ) { array [ j + 1 ] = array [ j ] ; } else { break ; } } array [ j + 1 ] = tmp ; } } 来源: CSDN 作者: Anonyme(ღ˘⌣˘ღ) 链接: https://blog.csdn.net/qq

26. Remove Duplicates from Sorted Array

那年仲夏 提交于 2020-01-24 19:36:31
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 = 2, with 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,

希尔排序

試著忘記壹切 提交于 2020-01-24 14:54:15
希尔排序 **时间复杂度:**最好 O(N) 最坏 O(n ^ 2) 稳定性: 比较时加了" = ",则不稳定,否则稳定 实现: 组内直接插入排序,最后看成一组,进行插入排序。 思想与直接插入排序类似,不过增量变成了组的差值(这是因为在进行组内排序) 分组-》组内插入排序-》最后看成一组 代码示例: public static void shell ( int [ ] array , int gap ) { for ( int i = gap ; i < array . length ; i ++ ) { int tmp = array [ i ] ; int j = i - gap ; for ( ; j >= 0 ; j -= gap ) { if ( array [ j ] > tmp ) { array [ j + gap ] = array [ j ] ; } else { break ; } } array [ j + gap ] = tmp ; } } public static void shellSort ( int [ ] array ) { //这里先分为5组,再分为3组,之后看成一组 int [ ] drr = { 5 , 3 , 1 } ; for ( int i = 0 ; i < drr . length ; i ++ ) { shellSort (

Understanding Why Sometime an Array Can Excel a Map

…衆ロ難τιáo~ 提交于 2020-01-24 13:42:50
Introduction We have discussed in a previous article , in most common situation if we have to maintain two Arrays working consistantly, we should better use a Map instead. And that's the reason why Map is introduced at the first place. But there are also some cases an Array can excel a Map. The first question is always, why do we care? We already feel comfortable of using a Map. The answer is Array will work more quickly than a Map. If we want to improve the speed, in some situation we can go back to using an Array. Array is fast, but not always we can use it. That is, when we working with