array

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

↘锁芯ラ 提交于 2020-02-05 17:06:31
书上题目 描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分。 思路 java实现 import java . util . Arrays ; //https://blog.csdn.net/qq_25343557/article/details/79221649 public class reOrderArray_shu { public static void ReorderArray ( int [ ] arr ) { int left = 0 ; int right = arr . length - 1 ; while ( left < right ) { //向后移动left,知道left指向偶数 while ( left < right && isEven ( arr [ left ] ) ) left ++ ; //向前移动right,知道right指向偶数 while ( left < right && ! isEven ( arr [ right ] ) ) right -- ; //交换 if ( left < right ) { int temp = arr [ left ] ; arr [ left ] = arr [ right ] ; arr [ right ] = temp ;

js数据类型判断和数组判断

℡╲_俬逩灬. 提交于 2020-02-05 10:54:08
js六大数据类型:number、string、object、Boolean、null、undefined string: 由单引号或双引号来说明,如"string" number:什么整数啊浮点数啊都叫数字,你懂的~ Boolean: 就是true和false啦 undefined:未定义,就是你创建一个变量后却没给它赋值~ null: 故名思久,null就是没有,什么也不表示 object: 这个我也很难解释的说。就是除了上面五种之外的类型 --------------------上面的都是浮云,下面的才是神马------------------------------ 数据类型判断之 typeof typeof可以解决大部分的数据类型判断,是一个一元运算,放在一个运算值之前,其返回值为一个字符串,该字符串说明运算数的类型,所以判断某个是否为String类型,可以直接 if(typeof(你的值) == "string"){} 以下是各种数据类型返回结果: var a="string"; console.log(a); //string var a=1; console.log(a); //number var a=false; console.log(a); //boolean var a; console.log(typeof a); //undfined var a =

js数据类型判断和数组判断

一世执手 提交于 2020-02-05 10:43:49
js六大数据类型:number、string、object、Boolean、null、undefined string: 由单引号或双引号来说明,如"string" number:什么整数啊浮点数啊都叫数字,你懂的~ Boolean: 就是true和false啦 undefined:未定义,就是你创建一个变量后却没给它赋值~ null: 故名思久,null就是没有,什么也不表示 object: 这个我也很难解释的说。就是除了上面五种之外的类型 --------------------上面的都是浮云,下面的才是神马------------------------------ 数据类型判断之 typeof typeof可以解决大部分的数据类型判断,是一个一元运算,放在一个运算值之前,其返回值为一个字符串,该字符串说明运算数的类型,所以判断某个是否为String类型,可以直接 if(typeof(你的值) == "string"){} 以下是各种数据类型返回结果: var a="string"; console.log(a); //string var a=1; console.log(a); //number var a=false; console.log(a); //boolean var a; console.log(typeof a); //undfined var a =

经典算法(面试必问)

房东的猫 提交于 2020-02-05 07:33:24
入门算法(面试经典问题) 1.快速排序(快排) 时间复杂度O(nlogn) 最坏时间复杂度O(n^2) 基本思想: 选择一个基准数作为参照物,由2边向中进行检索,先从右边检索比基准数小的,再从左边检索比基准数大的,然后交换2个元素,再进行检索! package quicksortdemo ; public class Demo1QuickSort { public static void main ( String [ ] args ) { int [ ] array = new int [ ] { 3 , 4 , 1 , 6 , 2 } ; forEach ( "未排好的:" , array ) ; quickSort ( array , 0 , array . length - 1 ) ; forEach ( "排好序的:" , array ) ; } /** * @param arr 数组 * @param left 左边索引位置 * @param right 右边索引位置 */ private static void quickSort ( int [ ] arr , int left , int right ) { // 左边索引不能大于右边索引 否则直接return if ( left > right ) { return ; } // 定义基准数 最左边下标的位置

《剑指offer练习笔记_01》

好久不见. 提交于 2020-02-05 01:43:50
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 1.暴力法 依次遍历数组中的每一个数,若找到,则返回true;查找失败,则返回false。 //暴力法 public class Solution { public boolean Find ( int target , int [ ] [ ] array ) { for ( int i = 0 ; i < array . length ; i ++ ) { for ( int j = 0 ; j < array [ 0 ] . length ; j ++ ) { if ( array [ i ] [ j ] == target ) { return true ; } } } return false ; } } 时间复杂度: O(n 2 ) 2. 从边角开始查找 有左上、左下、右上、右下四个角。a是最大值,l是最小值。 (1)左上/右下 以左上为例,比较 a 与 target,若: a > target, 则target不存在,返回false。 a < target, 则继续遍历查找。 a = target, 则查找成功,返回true。 右下同理,此方法相较于暴力法并无太多的改进

FZU 2037

六月ゝ 毕业季﹏ 提交于 2020-02-04 12:28:20
                          Maximum Value Problem Let’s start with a very classical problem. Given an array a[1…n] of positive numbers, if the value of each element in the array is distinct, how to find the maximum element in this array? You may write down the following pseudo code to solve this problem: function find_max(a[1…n]) max=0; for each v from a if(max<v) max=v; return max; However, our problem would not be so easy. As we know, the sentence ‘max=v’ would be executed when and only when a larger element is found while we traverse the array. You may easily count the number of execution of

Codeforces Round #616 (Div. 2) C. Mind Control

只谈情不闲聊 提交于 2020-02-04 02:47:14
题目链接: https://codeforces.com/contest/1291 You and your n−1 friends have found an array of integers a1,a2,…,an. You have decided to share it in the following way: All n of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the m-th position in the line. Before the process starts, you may choose up to k different people in the line, and persuade them to always take

C. Mind Control

六眼飞鱼酱① 提交于 2020-02-04 01:50:20
链接: https://codeforces.com/contest/1291/problem/C You and your n−1n−1 friends have found an array of integers a1,a2,…,ana1,a2,…,an. You have decided to share it in the following way: All nn of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the mm-th position in the line. Before the process starts, you may choose up to kk different people in the line, and

数组_array

南笙酒味 提交于 2020-02-04 01:18:26
数组 :存储同一种数据类型的多个元素的容器。 定义格式:   A:数据类型[] 数组名;   B:数据类型 数组名[]; 举例:   A:int[] a; 定义一个int类型的数组a变量   B:int a[]; 定义一个int类型的a数组变量 注意: 效果可以认为是一样的,都是定义一个int数组,但是念法上有些小区别。    推荐使用第一种。 如何对数组进行初始化呢?   A:何谓初始化呢? 就是为数组开辟内存空间,并为每个数组元素赋予值   B:有几种方式呢?   a:动态初始化 只指定长度,由系统给出初始化值   b:静态初始化 给出初始化值,由系统决定长度 动态初始化的格式:   数据类型[] 数组名 = new 数据类型[数组长度]; 举例:   int[] arr = new int[3]; 如何获取数组中的元素呢? 通过:   数组名[索引]   索引其实就是每个元素的编号,从0开始,最大索引是数组的长度-1。 class ArrayDemo1 { public static void main(String[] args) { //定义一个数组 //int[] a; //可能尚未初始化变量a //System.out.println(a); int[] arr = new int[3]; /* 左边: int:说明数组中的元素的数据类型是int类型 []

Codeforces——Array Sharpening

▼魔方 西西 提交于 2020-02-03 19:36:24
Codeforces——Array Sharpening You’re given an array a1,…,an of n non-negative integers. Let’s call it sharpened if and only if there exists an integer 1≤k≤n such that a1<a2<…ak+1>…>an. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: The arrays [4], [0,1], [12,10,8] and [3,11,15,9,7,4] are sharpened; The arrays [2,8,2,8,6,5], [0,1,1,0] and [2,5,6,9,8,8] are not sharpened. You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any i (1≤i≤n) such