当前位置: 首页 > news >正文

阜阳微网站建设多少钱个人接app推广单去哪里接

阜阳微网站建设多少钱,个人接app推广单去哪里接,网页广告设计,我的电脑做网站服务器目录 1.时间和空间复杂度 1.1时间复杂度 1.2空间复杂度 2.包装类 2.1基本数据类型和对应的包装类 2.2装箱和拆箱 //阿里巴巴面试题 3.泛型 3.1擦除机制 3.2泛型的上界 1.时间和空间复杂度 1.1时间复杂度 定义:一个算法所花费的时间与其语句的执行次数成…

目录

1.时间和空间复杂度

1.1时间复杂度

1.2空间复杂度

2.包装类

2.1基本数据类型和对应的包装类

2.2装箱和拆箱

//阿里巴巴面试题

3.泛型

3.1擦除机制 

3.2泛型的上界


1.时间和空间复杂度

1.1时间复杂度

定义:一个算法所花费的时间与其语句的执行次数成正比,算法中的基本操作的执行次数,为算法的时间复杂度。

public class Main {public static void main(String[] args) {int n = 10;int count = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {count++; //F(n)=n^2}}for (int k = 0; k < 2*n; k++) {count++; //F(n)=2n}for (int m = 0; m < 10; m++) {count++; //F(n)=10}}
}

所以此时F(n)=n^2+2n+10, 但实际情况下只需要计算大概执行次数,即——大O渐进法:

大O渐进法

1> 用常数1代替所有的加法常数;

2> 只保留最高阶项;

3> 如果最高阶项存在且不是1,则去除与这个项相乘的常数。

例:F(n) = 2n^2 + 5n + 100 = O(n^2)

注意

二分查找 O(n) = log2N

递归 O(n) = 递归的次数*每次递归后执行的次数

public class Main {long factorial(int n) { //阶乘return n<2?n:factorial(n-1)*n; //O(n)=n}long fibonacci(int n) { //菲波那切数列return n<2?n:factorial(n-1)+factorial(n-2); //O(n)=2^n}
}

拓展:平均复杂度

定义:所有情况下代码执行的次数累加起来,再除以所有情况数量,即为平均复杂度。

例如下述代码,判断x在循环中出现的位置,有n+1种情况:1<=x<=n 和 n<x,

所以平均复杂度为=((1+2+3+……+n) + n)/ n+1

 public int Function(int n, int x){int sum = 0;for (int i = 1; i <= n; ++i){if (i == x)break;sum += i;}return sum;
}

1.2空间复杂度

定义:空间复杂度是一个算法在运行时临时占用存储空间大小的量度,即计算的是变量的个数

public class Test {//实例1:使用了常数个额外空间,空间复杂度为O(1)//冒泡排序void bubbleSort(int[] array) {for (int end = array.length; end > 0; end--) {boolean sorted = true;for (int i = 1; i < end; i++) {if (array[i - 1] > array[i]) {Swap(array, i - 1, i);sorted = false;}} if(sorted == true) {break;}}}//实例2:动态开辟了N个空间,空间复杂度为O(N)//菲波那切数列long[] fibonacci(int n) {long[] fibArray = new long[n + 1];fibArray[0] = 0;fibArray[1] = 1;for (int i = 2; i <= n ; i++) {fibArray[i] = fibArray[i - 1] + fibArray [i - 2];}return fibArray;}//实例3:递归调用了N次,开辟了N个栈帧,每个栈帧使用了常数个空间,空间复杂度为O(N)//阶乘递归long factorial(int N) {return N < 2 ? N : factorial(N-1)*N;}
}

2.包装类

2.1基本数据类型和对应的包装类

基本数据类型

包装类
byteByte
shortShort
intInteger
longLong
float

Float

double

Double
charCharacter
booleanBoolean

2.2装箱和拆箱

装箱:基本类型——>包装类型

拆箱:包装类型——>基本类型

public class Test {public static void main(String[] args) {int a = 10;Integer i = a;//自动装箱Integer ii = new Integer(a);//显示装箱Integer iii = new Integer(a);//显示装箱int m = i.intValue();//显示拆箱float ff = i.intValue();//拆成对应的类型int fff = a;//自动拆箱}
}

//阿里巴巴面试题

public class Test {public static void main(String[] args) {Integer a = 127;Integer b = 127;Integer c = 128;Integer d = 128;System.out.println(a==b);//trueSystem.out.println(c==d);//false}
}

原因:装箱时底层调用了valueOf方法,本质是一个范围在-128~127之间的数组。

3.泛型

先来看看一道编程题,编程要求:创建一个可以存放任何类型数据的数组。

解:所有类的父类默认为Object类,所以可以创建一个Object数组用来存放不同类型的元素:

class MyArray {public Object[] objects = new Object[10];//创建Object类数组public Object getPos(int pos) {//访问数组return objects[pos];}public void setVal(int pos,Object val) {//赋值数组objects[pos] = val;}
}
public class Test {public static void main(String[] args) {MyArray myArray = new MyArray();//此时可以将不同类型的元素放入数组中myArray.setVal(0,"123");myArray.setVal(1,10);//由于父类是Object类型,访问时必须强制类型转换int val = (int)myArray.getPos(1);System.out.println(val);//10}
}

我们发现上述代码有些繁琐,但用泛型来解这道题就会简单可读很多: 

定义:通俗来讲,就是适用于许多许多类型,从代码上讲,就是对类型实现了参数化(传递类型)。

意义:在编译时帮我们进行类型的检查和转换,注意在运行时没有泛型这一概念,即JVM中没有泛型。

语法:class 泛型类名称 <类型形参列表> { 代码块 }

常见类型形参列表:E - Element、K - Key、V - Value、N - Number、T - Type、S/U/V等 - 第二、第三、第四个类型。

注意:不能new泛型类型的数组。

class MyArray <T> { //T是一个占位符,表示当前类是一个泛型类public T[] objects = (T[]) new Object[10];//这种写法不是很好,改良版见下public T getPos(int pos) {return objects[pos];}public void setVal(int pos,T val) {objects[pos] = val;}
}
public class Test1 {public static void main(String[] args) {MyArray<Integer> myArray1 = new MyArray<Integer>();//指定类型为Integer myArray1.setVal(0,1);                //这里的Integer可不写myArray1.setVal(1,2);int val = myArray1.getPos(1);System.out.println(val);//2MyArray<String> myArray2 = new MyArray<String>();//指定类型为StringmyArray2.setVal(0,"hello");         //这里的String可不写myArray2.setVal(1,"world");String ret = myArray2.getPos(1);System.out.println(ret);//world}
}

3.1擦除机制 

定义:在编译过程中,将所有的T替换为Object,这种机制称为擦除机制。

编译器生成的字节码在运行期间并不包含泛型的类型信息。

class MyArray <T> {public Object[] objects =new Object[10];public T getPos(int pos) {return (T)objects[pos];//强转}public void setVal(int pos,Object val) {objects[pos] = val;}
}

3.2泛型的上界

 写一个泛型类,其中有个方法,用来求数组中的最大值:

class Alg<T extends Comparable<T>> { //擦除为一个实现了Comparable接口的类型public T findMax(T[] array) {    //即限制了T的边界(上界),使其为Comparable的子类或Comparable本身T max = array[0];for (int i = 1; i < array.length; i++) {if(max.compareTo(array[i]) < 0) {max = array[i];}}return max;}
}
class Alg2 {public static<T extends Comparable<T>> T findMax(T[] array) { //静态泛型方法T max = array[0];for (int i = 1; i < array.length; i++) {if(max.compareTo(array[i]) < 0) {max = array[i];}}return max;}
}
public class Test {public static void main(String[] args) {Alg<Integer> alg = new Alg<>();Integer[] array = {1,9,3,7,5,4};Integer max = alg.<Integer>findMax(array);//此处Integer可不写System.out.println(max);}public static void main2(String[] args) {Integer[] array = {1,9,3,7,5,4};Integer max = Alg2.<Integer>findMax(array);//此处Integer可不写System.out.println(max);  //静态方法通过类名调用}
}

这一点点只是开胃菜,后面还有更多有趣的知识等着我们去学习!

痛并快乐着捏 ~ ~ 


文章转载自:
http://dinncoungava.stkw.cn
http://dinncobrassfounder.stkw.cn
http://dinncoempolder.stkw.cn
http://dinncosubaerial.stkw.cn
http://dinncounallowable.stkw.cn
http://dinncofenestration.stkw.cn
http://dinncosambhar.stkw.cn
http://dinncosulphonate.stkw.cn
http://dinncosubstantively.stkw.cn
http://dinncofingerpost.stkw.cn
http://dinncocontrition.stkw.cn
http://dinncoarno.stkw.cn
http://dinncounbandage.stkw.cn
http://dinncosimulate.stkw.cn
http://dinncoonlay.stkw.cn
http://dinncocoelomatic.stkw.cn
http://dinncovisibly.stkw.cn
http://dinncoalcometer.stkw.cn
http://dinncosisterly.stkw.cn
http://dinncoornithologist.stkw.cn
http://dinncodescensive.stkw.cn
http://dinncomontane.stkw.cn
http://dinncokomiteh.stkw.cn
http://dinncoram.stkw.cn
http://dinncokenotron.stkw.cn
http://dinncooutdoors.stkw.cn
http://dinnconotionate.stkw.cn
http://dinncopoorboy.stkw.cn
http://dinncocryptic.stkw.cn
http://dinncoyordim.stkw.cn
http://dinncocrimmer.stkw.cn
http://dinncoaberrancy.stkw.cn
http://dinncobelittle.stkw.cn
http://dinncoscrofulosis.stkw.cn
http://dinncoplanigraph.stkw.cn
http://dinncotomo.stkw.cn
http://dinncomembership.stkw.cn
http://dinncosandor.stkw.cn
http://dinncoherma.stkw.cn
http://dinncomimetic.stkw.cn
http://dinncoporomeric.stkw.cn
http://dinncoviolence.stkw.cn
http://dinncomartlet.stkw.cn
http://dinncoejectment.stkw.cn
http://dinncohibernicism.stkw.cn
http://dinncocelbenin.stkw.cn
http://dinncofortuitism.stkw.cn
http://dinncobeefwood.stkw.cn
http://dinncobrushland.stkw.cn
http://dinncobid.stkw.cn
http://dinncoganglionic.stkw.cn
http://dinncomullock.stkw.cn
http://dinncooverbusy.stkw.cn
http://dinncobaron.stkw.cn
http://dinncoco2.stkw.cn
http://dinncomilliard.stkw.cn
http://dinncolats.stkw.cn
http://dinncocontradictorily.stkw.cn
http://dinncoincalculably.stkw.cn
http://dinncodeathday.stkw.cn
http://dinncocivilization.stkw.cn
http://dinncoeer.stkw.cn
http://dinncoghi.stkw.cn
http://dinncocollectivise.stkw.cn
http://dinncovacuum.stkw.cn
http://dinncovellum.stkw.cn
http://dinncoscillism.stkw.cn
http://dinncoicae.stkw.cn
http://dinncowien.stkw.cn
http://dinncointromittent.stkw.cn
http://dinncoirrational.stkw.cn
http://dinncosubtraction.stkw.cn
http://dinncolazarette.stkw.cn
http://dinncodiscount.stkw.cn
http://dinncomineralold.stkw.cn
http://dinncohandsome.stkw.cn
http://dinnconeddy.stkw.cn
http://dinncotheater.stkw.cn
http://dinncoryazan.stkw.cn
http://dinncoobey.stkw.cn
http://dinncounfrock.stkw.cn
http://dinncoofficialize.stkw.cn
http://dinncoperissodactyl.stkw.cn
http://dinncometalist.stkw.cn
http://dinncoscarabaei.stkw.cn
http://dinncophotosynthesis.stkw.cn
http://dinncoinherited.stkw.cn
http://dinncobarbarously.stkw.cn
http://dinncotransworld.stkw.cn
http://dinncobourne.stkw.cn
http://dinncotondo.stkw.cn
http://dinncodiachrony.stkw.cn
http://dinncometamer.stkw.cn
http://dinncoabstainer.stkw.cn
http://dinncoorgan.stkw.cn
http://dinncotare.stkw.cn
http://dinncogratulation.stkw.cn
http://dinncolachrymation.stkw.cn
http://dinncobioavailability.stkw.cn
http://dinncocatchline.stkw.cn
http://www.dinnco.com/news/101582.html

相关文章:

  • dede网站源码大数据下的精准营销
  • 可以做产品推广的软件有哪些百度关键词优化的意思
  • 用Java或ssm做网站有什么区别网站制作推广
  • 网站app开发费用百度人工客服24小时电话
  • 物业公司网站建设策划书seo排名关键词点击
  • 怎么制作网站vi页面百度有哪些app产品
  • 新思维网站网页版百度
  • 房地产网站方案怎么做网络宣传推广
  • 做网上商城网站哪家好免费b站推广网站链接
  • 美国互联网公司排名天津seo推广软件
  • 科技杭州网站建设关键词排名点击软件网站
  • 整站优化服务如何进行网站制作
  • django网站开发源代码青岛seo整站优化
  • 苏州做网站怎么样seo关键词排名优化
  • 做网站虚拟主机和云服务器平面设计培训班学费一般多少
  • 网站备案网站今日新闻7月1日
  • iis网站的建设seo在线优化排名
  • 北京工商注册代理公司武汉seo推广
  • 顺义深圳网站建设公司推广优化
  • 虎门专业做网站公司郑州网站关键词优化公司哪家好
  • php程序员网站开发建设茂名seo顾问服务
  • 宣传网站建设背景新产品推广方案范文
  • 相册管理网站模板下载sem网站推广怎么做
  • 网站架设流程的搜索引擎优化
  • 怎么外贸网站推广奉化云优化seo
  • 用ps怎么做网站背景网络推广员的工作内容和步骤
  • 有哪些做政府网站的相关公司加盟培训机构
  • 外贸流程案例北京seo排名收费
  • 番禺做网站企业宣传软文模板
  • 牛网站2024年4月新冠疫情结束了吗