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

宁波网站排名优化seo上海公司网站建设公司

宁波网站排名优化seo,上海公司网站建设公司,汕头网站seo外包,国外优秀网站文章目录题目描述思路 代码1. 哈希表法2. 原地算法二刷题目描述 主要有两个考虑点#xff1a; 不能改变原链表新链表赋予 next、random 时#xff0c;复制结点不一定存在 思路 代码 1. 哈希表法 O(n)、O(n)参考了dalao的写法#xff0c;这里哈希表… 文章目录题目描述思路 代码1. 哈希表法2. 原地算法二刷题目描述 主要有两个考虑点 不能改变原链表新链表赋予 next、random 时复制结点不一定存在 思路 代码 1. 哈希表法 O(n)、O(n)参考了dalao的写法这里哈希表用得非常巧妙值得学习思路在哈希表中建立 Node - CopyNode 的联系在此基础上进行 next random 的处理即可。 /* // Definition for a Node. class Node {int val;Node next;Node random;public Node(int val) {this.val val;this.next null;this.random null;} } */ class Solution {public Node copyRandomList(Node head) {if(head null) {return null;}// 哈希表做法Node - CopyNodeMapNode, Node hashmap new HashMap();for(Node temp head; temp ! null; temp temp.next) {hashmap.put(temp, new Node(temp.val));}for(Node temp head; temp ! null; temp temp.next) {// next 的处理hashmap.get(temp).next hashmap.get(temp.next);// random 的处理hashmap.get(temp).random hashmap.get(temp.random);}return hashmap.get(head);} }2. 原地算法 O(n)、O(1)相对于方法1此处不需要占用额外空间注意原链表的恢复、边界结点的处理 class Solution {public Node copyRandomList(Node head) {if(head null) {return null;}// 原地算法// 1. 首先 1 - 2 - 3 变成 1 - 1* - 2 - 2* - 3 - 3*for(Node temp head; temp ! null; temp temp.next.next) {Node copy new Node(temp.val);copy.next temp.next;temp.next copy;}// 2. 进行 random 的处理for(Node temp head; temp ! null; temp temp.next.next) {if(temp.random ! null) {temp.next.random temp.random.next;}}// 3. 分开 1 - 1* - 2 - 2* - 3 - 3* 变成 1* - 2* - 3*Node copyHead head.next;for(Node temp head; temp ! null; temp temp.next) {// 取到原本的正确nextNode nextNode temp.next.next;// * - *if(nextNode ! null) {temp.next.next temp.next.next.next;}// 1 - 2temp.next nextNode;}return copyHead;} }二刷 哈希表 class Solution {public Node copyRandomList(Node head) {MapNode, Node map new HashMap();for(Node temp head; temp ! null; temp temp.next) {map.put(temp, new Node(temp.val));}for(Node temp head; temp ! null; temp temp.next) {map.get(temp).next map.get(temp.next);map.get(temp).random map.get(temp.random);}return map.get(head);} }原地算法二刷来看还是很有含金量的做法思路 class Solution {public Node copyRandomList(Node head) {if(head null) return null;for(Node temp head; temp ! null; temp temp.next.next) {Node newNode new Node(temp.val);newNode.next temp.next;temp.next newNode;}for(Node temp head; temp ! null; temp temp.next.next) {temp.next.random temp.random null ? null : temp.random.next;}Node ans head.next;for(Node temp head; temp ! null; temp temp.next) {Node tempNode temp.next;temp.next temp.next.next;tempNode.next temp.next null ? null : tempNode.next.next;}return ans;} }
http://www.zqtcl.cn/news/171112/

相关文章:

  • 做户外运动的网站seo内部优化方案
  • 哪个行业必须做网站软件工程最好的出路
  • 安徽省质量提升工程建设网站深圳十大国际外贸公司
  • 县城做信息网站qq是哪个公司
  • 设计师作品展示网站做图软件官方网站
  • 企业网站网站建设价格seo短视频网页入口引流
  • 旅游电商网站建设方案模板济南搜点网络科技有限公司
  • 网站模板 带手机端头条推广平台有哪些
  • 有没有专门做衣服的网站小程序加盟代理前景
  • app网站开发报价wordpress怎么加快网站打开速度
  • 路南网站建设可用的ftp网站
  • 台州市建站公司网站免费建设推荐
  • 网站世界排名怎么做柘城县网站建设
  • 网站设计与制作培训班户外运动网站建设策划
  • 保险公司网站网站域名注册哪个好
  • 平台网站建设可行报告工作室注册条件
  • 罗湖附近公司做网站建设哪家便宜做cpa必须要有网站吗
  • 深圳网站开发教程wordpress密码登录插件
  • 农机网站建设目标上海网站建设工资多少
  • 手机做网站过程网站名字做版权需要源代码吗
  • 公司微信网站建设方案现在做外贸前景如何
  • 如何使用微信公众号做网站河南省专业做网站公司
  • 清远市清城区网站建设公司广州黄浦区建设局网站
  • 站长做2个网站网站程序指的是什么
  • 网站建设jw100请拿笔记记下新域名
  • 兰州市住房保障和城乡建设局网站如何有效的推广网站
  • 网站推广做百度还是360北京企业网站建设费用
  • 推荐一个简单的网站制作策划方案免费网站
  • 用vs2012做网站案例企业现在有必要做网站吗
  • 网站建设少用控件wordpress默认分类