array

快速排序

空扰寡人 提交于 2020-01-15 10:25:11
public static void main(String[] args) { int[] array={49,38,65,97,76,13,27,50,78,34,12,64,1}; myQuickSort(array,0,array.length-1); System.out.println("排序之后:"); for(int i=0;i<array.length;i++){ System.out.print(array[i]+" "); } System.out.println(); } /** * 快排,是递归的,一个数组先进行一次轴划分成两个区, * 然后以轴为中心把左边的和右边都认为是一个新的数组再次进行快排 * @param array * @param low * @param hight */ public static void myQuickSort(int[] array,int low,int hight){ if(low<hight){ int axis = myFirstQuickSort(array, low, hight);//进行第一轮排序 myQuickSort(array,axis+1,hight);//右边认为是一个新的数组,进行快排 myQuickSort(array,low,axis-1);//左边的认为是一个新的数组,进行快排 }

js方法封装大全

空扰寡人 提交于 2020-01-15 02:41:11
/** * @version: v2.5.0 * @buildTime: Thu Jul 16 2015 17:36:29 GMT+0800 (中国标准时间) */ ( function ( global , document , S , undefined ) { var location = global . location , ua = navigator . userAgent , documentElement = document . documentElement , head = document . head || document . getElementsByTagName ( "head" ) [ 0 ] , isSupportConsole = global . console && console . log , noop = function ( ) { } , error = function ( msg ) { throw isError ( msg ) ? msg : new Error ( msg ) ; } , /** * 配置对象 * @type {Object} */ Config = { debug : location . search . indexOf ( "debug" ) !== - 1 ? true : false } ,

JavaSE基础

帅比萌擦擦* 提交于 2020-01-14 17:57:16
Random package day_01; import java.util.Random; import java.util.Scanner; public class RandomPractice { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); int index = random.nextInt(101); while(true){ System.out.print("请输入0-100的数字:"); int n = scanner.nextInt(); if(n > index){ System.out.println("输入的数字过大"); } else if(n < index){ System.out.println("输入的数字过小"); } else{ System.out.println("输入正确"); break; } } System.out.println("游戏结束!"); } }    ArrayList package day_01; import java.util.ArrayList; public class ArrayListPractice { public

LeetCode Array Easy 414. Third Maximum Number

断了今生、忘了曾经 提交于 2020-01-14 15:55:03
Description Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). Example 1: Input: [3, 2, 1] Output: 1 Explanation: The third maximum is 1. Example 2: Input: [1, 2] Output: 2 Explanation: The third maximum does not exist, so the maximum (2) is returned instead. Example 3: Input: [2, 2, 3, 1] Output: 1 Explanation: Note that the third maximum here means the third maximum distinct number. Both numbers with value 2 are both considered as second maximum. 问题描述,给定要给非空数组,找出第三大的数

如何将类数组转化为数组

妖精的绣舞 提交于 2020-01-14 09:15:00
首先,什么是类数组(Array Like)? 一个简单的定义,如果一个对象有 length 属性值,则它就是类数组 那常见的类数组有哪些呢? 这在 DOM 中甚为常见,如各种元素检索 API 返回的都是类数组,如 document.getElementsByTagName , document.querySelectorAll 等等。除了 DOM API 中,常见的 function 中的 arguments 也是类数组 那如何把类数组转化为数组呢?这是类数组操作时一个典型的场景,也是一个典型的面试题 以下我们将以 { length: 3 } 来指代类数组,来作为演示 节选自 日文 【Q168】在 js 中如何把类数组转化为数组 。另外这里有更多的 前端面试题 ,欢迎交流 ES6 ES6 中有现成的 API: Array.from ,极为简单 // [undefined, undefined, undefined] Array.from({ length: 3 }) 除了 Array.from 还有更简单的运算符 ... 扩展运算符,不过它只能作用于 iterable 对象,即拥有 Symbol(Symbol.iterator) 属性值 拥有 Symbol(Symbol.iterator) 属性值,意味着可以使用 for of 来循环迭代 // 适用于 iterable 对象 [.

D. Strange Device

落爺英雄遲暮 提交于 2020-01-14 07:58:14
This problem is interactive. We have hidden an array a of n pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of k different elements of the array, it will return the position and value of the m-th among them in the ascending order. Unfortunately, the instruction for the device was lost during delivery. However, you remember k, but don’t remember m. Your task is to find m using queries to this device

剑指offer 调整数组顺序是奇数位于偶数前面

风流意气都作罢 提交于 2020-01-14 05:51:53
1.题目 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。 来源:剑指offer 链接: https://www.nowcoder.com/practice/beb5aa231adc45b2a5dcc5b62c93f593?tpId=13&tqId=11166&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking 2.我的题解 (1)稳定的排序方式有插入排序、冒泡排序、归并排序,可以借鉴排序算法解决本题。 时间复杂度: O(n^2) 空间复杂度: O(1) (2)使用辅助数组,先遍历一遍原数组,提取出奇数顺序放入辅助数组,再遍历一遍原数组,提取出偶数顺序放入辅助数组,最后将辅助数组赋值给原数组。 时间复杂度: O(n) 空间复杂度: O(n) 先复制数据到辅助数组然后更改原数组的值,或者先更改辅助数组的值再复制到原数组,是一样的。 class Solution { public : void reOrderArray ( vector < int > & array ) { vector < int > vec ( array . begin ( ) , array .

CodeForces Good Bye 2019——C. Make Good(异或的应用)

半世苍凉 提交于 2020-01-14 01:16:01
题目传送门:https://codeforces.com/contest/1270/problem/C 题目内容 Let’s call an array a1,a2,…,am of nonnegative integer numbers good if a1+a2+⋯+am=2⋅(a1⊕a2⊕⋯⊕am), where ⊕ denotes the bitwise XOR operation. For example, array [1,2,3,6] is good, as 1+2+3+6=12=2⋅6=2⋅(1⊕2⊕3⊕6). At the same time, array [1,2,1,3] isn’t good, as 1+2+1+3=7≠2⋅1=2⋅(1⊕2⊕1⊕3). You are given an array of length n: a1,a2,…,an. Append at most 3 elements to it to make it good. Appended elements don’t have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you

剑指offer:数组中重复的数字

孤街醉人 提交于 2020-01-13 23:24:16
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。 // Parameters: // numbers: an array of integers // length: the length of array numbers // duplication: (Output) the duplicated number in the array number // Return value: true if the input is valid, and there are some duplications in the array number // otherwise false bool duplicate ( int numbers [ ] , int length , int * duplication ) { } 分析:数组中的数的大小是有限的,在0到n-1的范围之内,因此,我们可以利用一个hash表。下标表示的是数字,然后它的内容表示的是出现了几次。这样的话查找的时间复杂度就是O(1)。当记录好以后再从头遍历,找到第一个值为1的。 #

CareerCup maximum sum in n*n num array

大憨熊 提交于 2020-01-13 01:08:59
Given a n*n array like the following example. A man could only move up down left and right without oblique movement. BTW, the man could only arrive when num[i][j] is a number instead of '*'. At the same time, no num circle could be generated in n*n array. How could we get the maximum num sum? nums = [['*','*','4','*','*','1','*'], ['*','1','2','3','*','*','*'], ['*','1','*','3','*','*','*'], ['*','*','*','*','1','*','*'], ['*','*','1','1','3','1','*'], ['*','*','3','*','88','*','*'], ['*','*','1','1','*','*','1233']]; -------------------------------------------------- Solution: The condition