首页 文章 精选 留言 我的

精选列表

搜索[java],共10000篇文章
优秀的个人博客,低调大师

leetcode算法题解(Java版)-7-循环链表

一、循环链表 题目描述 Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 思路 不能用多余空间,刚开始没有考虑多个指针什么,一下子想到个歪点子:循环就是重复走,那我可以标记一下每次走过的路,如果遇到标记过的路,那说明就是有回路了。 代码一 /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { if(head==null){ return false; } ListNode p=new ListNode(0); p=head; int u=-987123; while(p.val!=u&&p.next!=null){ p.val=u; p=p.next; } if(p.val==u){ return true; } else{ return false; } } } 思路二 当然标准的是应该用两个指针来“追赶”,前提是这两个指针走的速度不一样,一前一后如果相遇了则说明有回路。 代码二 /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { if(head==null){ return false; } ListNode p=head; ListNode q=head.next; while(p!=q&&q!=null&&p!=null){ q=q.next; if(q!=null){ q=q.next; } p=p.next; } if(p==q&&p!=null){ return true; } else{ return false; } } } 优化过的代码: /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { if(head==null){ return false; } ListNode fastNode=head; ListNode lowNode=head; while(fastNode!=null&&fastNode.next!=null){ fastNode=fastNode.next.next; lowNode=lowNode.next; if(fastNode==lowNode){ return true; } } return false; } } 今天有场考试,到七点半才结束,就刷这么多了。

优秀的个人博客,低调大师

Java统计代码段的执行时间

版权声明:欢迎评论和转载,转载请注明来源。 https://blog.csdn.net/zy332719794/article/details/80180571 通常在进行代码测试和代码优化的时候,会想要知道代码执行时每段代码的执行时间,以便进行代码优化和调整。 下面封装的类是利用代码段标记和执行时间差进行统计。使用时,仅需要在代码段中加入CodeTimer.set("标记");就可以了,打印时调用CodeTimer.print();统计字段有代码段、总时间(纳秒)、执行次数、平均时间。 封装类: /** * 统计代码段执行时间。 * 在需要进行统计的代码段调用CodeTimer.set()方法进行标记。 * 打印时调用CodeTimer.print()方法 */ public class CodeTimer { private static String lastMark = "start"; private static long lastTime = System.nanoTime(); private static final Map<String, Long> timeMap = new LinkedHashMap<String, Long>(); private static final Map<String, Long> timeHappenCount = new LinkedHashMap<String, Long>(); public static void set(int mark) { set("" + mark); }; public static void set(String mark) { long thisTime = System.nanoTime(); String key = "[" + lastMark + "]->[" + mark + "]"; Long lastSummary = timeMap.get(key); if (lastSummary == null) lastSummary = 0L; timeMap.put(key, System.nanoTime() - lastTime + lastSummary); Long lastCount = timeHappenCount.get(key); if (lastCount == null) lastCount = 0L; timeHappenCount.put(key, ++lastCount); lastTime = thisTime; lastMark = mark; }; public static void print() { Integer a = 0; System.out.println( String.format("%25s %18s %18s %18s", "PROCESS", "TOTAL_TIME", "REPEAT_TIMES", "AVG_TIME")); for (Entry<String, Long> entry : timeMap.entrySet()) { System.out.println( String.format("%25s %18s %18s %18s", entry.getKey(), String.format("%,d", entry.getValue()), timeHappenCount.get(entry.getKey()), String.format("%,d", entry.getValue() / timeHappenCount.get(entry.getKey())))); } } } 打印出的效果形如: PROCESS TOTAL_TIME REPEAT_TIMES AVG_TIME [start]->[0] 152,312 1 152,312 [0]->[4] 12,223,365 1 12,223,365 [4]->[10] 101,838 6 16,973 [10]->[8] 1,246,189 34 36,652 [8]->[5] 489,096,299 34 14,385,185 [5]->[6] 122,247,497 34 3,595,514 [6]->[7] 2,686,057,029 34 79,001,677 [7]->[1] 22,334 1 22,334 [1]->[9] 911,191 1 911,191

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册