array

[Javascript] Compare a Generator to Using Array Map and Filter

人盡茶涼 提交于 2020-01-08 19:16:11
Generators offer flexible alternatives to working with arrays and how you want to iterate through the data. While most scenarios are covered by the methods included on Arrays such as "map" and "filter", generators are great for covering complex scenarios when writing all your logic in map and filter functions might become difficult. let names = ["John", "Mindy", "Sally"] let result = names.filter(name => name.includes("y")).map(name => name.toLocaleLowerCase()) console.log(result) // ["mindy", "sally"] // -- Generator -- function* format(array: string[]) { for (let name of array) { if (name

Codewar-010 Rotate Array

*爱你&永不变心* 提交于 2020-01-08 16:19:15
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Create a method named "rotate" that returns a given array with the elements inside the array rotated n spaces. If n is greater than 0 it should rotate the array to the right. If n is less than 0 it should rotate the array to the left. If n is 0, then it should return the array unchanged. Example: Object[] data = new Object[]{1, 2, 3, 4, 5}; rotate(data, 1) => {5, 1, 2, 3, 4} rotate(data, 2) => {4, 5, 1, 2, 3} rotate(data, 3) => {3, 4, 5, 1, 2} rotate(data, 4) => {2, 3, 4, 5, 1} rotate(data, 5) => {1, 2, 3, 4, 5} rotate(data, 0) => {1, 2, 3, 4, 5} rotate(data

Codewar-006: A disguised sequence (I)

痞子三分冷 提交于 2020-01-08 16:18:09
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 来源: https://www.codewars.com/kata/563f0c54a22b9345bf000053/train/java 参考: http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String,%20int) Given u0 = 1, u1 = 2 and the relation 6unun+1-5unun+2+un+1un+2 = 0 calculate un for any integer n >= 0 . Examples fct(n) returns un : fct(17) -> 131072, fct(21) -> 2097152 Remark: You can take two points of view to do this kata: the first one purely algorithmic from the definition of un the second one - not at all mandatory, but as a complement - is to get a bit your head around and

数组中查找指定元素

北慕城南 提交于 2020-01-08 14:22:20
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> findIndex(es6) function isBigEnough(element) { return element >= 15; } [12, 5, 8, 130, 44].findIndex(isBigEnough); // 3 findIndex 会找出第一个大于 15 的元素的下标,所以最后返回 3。 实现findIndex function findIndex(array, predicate, context) { for (var i = 0; i < array.length; i++) { if (predicate.call(context, array[i], i, array)) return i; } return -1; } console.log(findIndex([1, 2, 3, 4], function(item, i, array){ if (item == 3) return true; })) // 2 findLastIndex function findLastIndex(array, predicate, context) { var length = array.length; for (var i = length - 1; i >= 0; i--)

503. Next Greater Element II

醉酒当歌 提交于 2020-01-08 03:36:12
Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, output -1 for this number. Example 1:
 Input: [1,2,1] Output: [2,-1,2] Explanation: The first 1's next greater number is 2; The number 2 can't find next greater number; The second 1's next greater number needs to search circularly, which is also 2. //

数组中出现次数超过一半的数【Java】

梦想的初衷 提交于 2020-01-07 21:03:18
题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。 代码: import java.util.HashMap; import java.util.Map; /** * 题目描述 * 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。 * 由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。 */ public class Solution { public static void main(String[] args) { Solution solution = new Solution(); System.out.println(solution.MoreThanHalfNum_Solution(new int[]{1, 2, 3, 2, 2, 2, 5, 4, 2})); } public int MoreThanHalfNum_Solution(int [] array) { if(array == null || array.length == 0){ return 0; } Map<Integer

7、把数组排成最小的数

╄→尐↘猪︶ㄣ 提交于 2020-01-07 17:47:42
题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。 1、解题思路 本题最直观的解法就是求出数组中所有数字的全排列,然后比较所有的排列,最后找到最小的排列,但是时间复杂度为O(n!),所以不是一个好的解法。   换一种思路可以发现,本题实际上希望我们找到一个排序规则,数组根据这个排序规则进行重排之后可以连成一个最小的数字。要确定这样的排序规则,也就是对于两个数字m和n,通过一个规则确定哪个应排在前面。   根据题目要求,我们可以发现,两个数字m和n能拼接成mn和nm,如果mn<nm,那m应该在前;如果nm<mn,那么n应该在前。因此,我们得到的排序规则如下: 若mn>nm,则m大于n 若mn<nm,则m小于n 若mn=nm,则m等于n   根据上述规则,我们需要先把数字转换成字符串再进行比较,因为需要拼接起来。比较完之后按顺序连接成一个字符串即可。 2、代码实现 package offer ; import java . util . Arrays ; import java . util . Comparator ; public class PrintMinNumber { public static void main ( String [

js数组去重

佐手、 提交于 2020-01-07 15:59:54
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> //数组去重 var testArray = [1,2,1,1,1]; //双重for循环 function unique2For(array) { var res = []; for(var i=0, arrayLen = array.length;i<arrayLen; i++) { for(var j=0, resLen = res.length;j<resLen; j++) { if(array[i] === res[j]) { break; } } if(j === resLen) { res.push(array[i]); } } } //indexof function uniqueUseIndexOf(array) { var res = []; for(var i=0,arrlen = array.length;i<arrlen;i++) { var current = array[i]; if(res.indexOf(current) === -1) { res.push(current); } } } //es5 filter function uniqueUseFilter(array) { var res = array.filter(function (item,index

【剑指offer】和为S的两个数字

一世执手 提交于 2020-01-07 08:47:17
题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。 解题思路 两个指针分别指向头和尾 如果当前和小于目标值,则头指针++; 如果当前和大于目标值,则尾指针–; 如果当前和等于目标值,则更新最小和,并把数字存入结果集合中。 清空ArrayList的方法是clear。 因为不知道最小和是正数还是负数,所以最小和的初始值只能设为最大的int。 代码 import java . util . ArrayList ; public class Solution { public ArrayList < Integer > FindNumbersWithSum ( int [ ] array , int sum ) { ArrayList < Integer > res = new ArrayList < Integer > ( ) ; if ( array == null || array . length == 0 ) return res ; int low = 0 , high = array . length - 1 , product = 2147483647 ; while ( low < high ) { if ( array [ low ] + array [ high ] < sum )

An Array of Sequences(2)

为君一笑 提交于 2020-01-06 14:49:42
1. Managing Ordered Sequences with bisect The bisect module offers two main functions --- bisect and insort --- that use the binary serach algorithm to quickly find and insert items in any sorted sequence. Example 2-17. bisect finds insertion points for items in a sorted sequence. import bisect import sys HAYSTACK = [1, 4, 5, 6, 8, 12, 15, 20, 21, 23, 23, 26, 29, 30] NEEDLES = [0, 1, 2, 5, 8, 10, 22, 23, 29, 30, 31] ROW_FMT = '{0:2d} @ {1:2d} {2}{0:<2d}' def demo(bisect_fn): for needle in reversed(NEEDLES): position = bisect_fn(HAYSTACK, needle) offset = position * ' |' print(ROW_FMT.format