常见算法示例
冒泡排序
for(int i =0;i<arr.length-1;i++) { for(int j=0;j<arr.length-i-1;j++) {//-1为了防止溢出 if(arr[j]>arr[j+1]) { int temp = arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } }
二分查找法
给定数组是按从小到大排序好的
//先排序 bubbleSort(arr); int left = 0; int right = arr.length - 1; int mid; while(left <= right){ mid = (left + right) % 2 == 0 ? (left + right) / 2 : (left + right) / 2 + 1; if(arr[mid] > val){ right = mid ; }else if (arr[mid] < val){ left = mid ; }else{ //找到该值 ** System.out.println(mid);** return; } } }
单链表反转
public class SingleLinkedList{ private static class Node{ private int val; private Node next; } //递归反转1-2-3-4 public Node reverse1(Node node){ if(node.next ==null || node== null){ return node; } Node temp= node.next; Node newNode = reverse1(node.next); node.next = null; temp.next = node; return newNode; } //遍历1-2-3-4 public Node reverse2(Node head){ Node prev = null; Node next = null; while (head != null){ next = head.next; head.next = prev; //在这里完成4->3->2->1 prev = head; head = next; } return prev; } }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Spring Boot 与 MVC 的区别,这些终于搞明白了!
作者:潜龙勿用 Spring 框架就像一个家族,有众多衍生产品例如 boot、security、jpa等等。但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 aop ,解决了面向横切面的编程,然后在此两者的基础上实现了其他延伸产品的高级功能。 Spring MVC是基于 Servlet 的一个 MVC 框架 主要解决 WEB 开发的问题,因为 Spring 的配置非常复杂,各种XML、 JavaConfig、hin处理起来比较繁琐。于是为了简化开发者的使用,从而创造性地推出了Spring boot,约定优于配置,简化了spring的配置流程。说得更简便一些:Spring 最初利用“工厂模式”(DI)和“代理模式”(AOP)解耦应用组件。大家觉得挺好用,于是按照这种模式搞了一个 MVC框架(一些用Spring 解耦的组件),用开发 web 应用( SpringMVC )。然后有发现每次开发都写很多样板代码,为了简化工作流程,于是开发出了一些“懒人整合包”(starter),这套就是 Spring Boot。 Spring MVC的功能 Spring MVC...
- 下一篇
算法:二叉树代码示例
二叉树特点 左节点值小于根节点值,右节点值大于根节点 public class BinaryTree { private Node root; @Data private static class Node { private int data; private Node left; private Node right; public Node(int data){ this.data = data; } } public Node find(int key){ Node current = root; while(current != null){ if(current.data > key){ current = current.left; } if(current.data < key){ current = current.right; } if(current.data == key){ return current; } } return null; } public boolean insert(int data){ Node newNode = new N...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Hadoop3单机部署,实现最简伪集群
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果