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

个人建设网站流程图成都百度推广电话

个人建设网站流程图,成都百度推广电话,乌鲁木齐市新市区建设局网站,( )是网站可以提供给用户的价值题目描述 编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。 示例1: 输入:[1, 2, 3, 3, 2, 1] 输出:[1, 2, 3] -示例2: 输入:[1, 1, 1, 1, 2] 输出:[1, 2] 提示: 链表长度在[0, 20000]范…

题目描述

编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。

示例1:

  • 输入:[1, 2, 3, 3, 2, 1]
    输出:[1, 2, 3]

-示例2:

输入:[1, 1, 1, 1, 2]
输出:[1, 2]

提示:

  • 链表长度在[0, 20000]范围内。
    链表元素在[0, 20000]范围内。

进阶:

  • 如果不得使用临时缓冲区,该怎么解决?

解题思路与代码

首先,这道题于本质上是要让你删除一些重复的节点。那么删除一个节点需要做哪些操作呢?

首先本题给的是单链表。如果要删除某个节点,则要找到当前节点的前驱节点,使前驱节点的next指针指向当前节点的下一个节点,最后将当前节点的内存释放掉,至此,删除某个节点的操作才算正式完成。

哈希法

因为要删除一个节点,我们就要用该节点的前驱节点去指向该节点的下一个节点。所以,我们就先设立一个要被处理元素的前驱节点,然后依次遍历被处理元素这个节点。如果被处理元素需要被删除,那我们直接用前驱节点删除好了。

为什么要叫哈希法呢?这是因为用到了unordered_set这种无序的关联容器。当我们为在这个集合中找到这个未处理元素时,我们就将这个元素添加到这个集合中去。如果找到了呢?就直接那被处理元素的前驱节点去删除这个节点就好了。这就是这道题的完整思想,剩下的请看代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* removeDuplicateNodes(ListNode* head) {if(head == nullptr) return head;unordered_set<int> set = {head->val};ListNode* pos = head;while(pos->next != nullptr){ListNode* cur = pos->next; //即将要删除的节点if(set.find(cur->val) == set.end()){set.insert(cur->val);pos = cur;}else{pos->next = cur->next;delete cur;}}return head;}
};

复杂度分析:

  • 时间复杂度O(N),因为只用了一个while循环,其中N是给定链表的节点数目。

  • 空间复杂度O(N),因为最坏情况下,集合中的元素都不相同,我们要存储所有的元素到集合中。

进阶:不使用临时缓冲区

说到不使用临时缓冲区,其实它的意思就是让我直接对链表进行操作。那么如果想象成删除一个string中重复出现的字符了话,这道题其实用两个for循环暴力破解了就行。但这道题是在链表上进行操作,我们就要将双层for循环,去改成双层while循环。

这解题思路还是与哈希法类似,一共需要设立3个节点。
第一个节点作为外层循环遍历节点与被处理元素的对照组,初始值:head。
第二个节点作为被处理元素的前驱节点,初始值也:第一个节点。
第三个节点作为被处理元素,初始值为:第二个节点->next。

具体实现的看代码细节:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* removeDuplicateNodes(ListNode* head) {if(head == nullptr) return head;ListNode* pos = head; //对照节点while(pos != nullptr){ListNode* pre = pos; //被处理元素的前驱节点ListNode* cur = pre->next; //被处理元素while(cur != nullptr){if(pos->val != cur->val){pre = cur;}else{pre->next = cur->next;}cur = cur->next;}pos = pos->next;}return head;}
};

复杂度分析:
时间复杂度O(N^2),其中N代表所给链表的节点数。用了两个while循环。
空间复杂度O(1),因为只用到了几个临时变量。

优化:双层遍历法

这次代码对应上次的优化是只设置了两个节点用来变量。我们将pos作为外层遍历节点与对照组节点,将cur作为前驱节点,将cur->next作为被处理节点。

减少了一个临时变量的设置,优化了一行代码,但是代码的易读性变差,并且代码易错性增强。

具体实现看代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* removeDuplicateNodes(ListNode* head) {if(head == nullptr) return head;ListNode* pos = head; while(pos != nullptr){ListNode* cur = pos; while(cur->next != nullptr){ if(cur->next->val == pos->val)cur->next = cur->next->next; elsecur = cur->next;}pos = pos->next;}return head;}
};

复杂度分析:
时间复杂度O(N^2),其中N代表所给链表的节点数。用了两个while循环。
空间复杂度O(1),因为只用到了几个临时变量。


文章转载自:
http://mordacious.c7493.cn
http://armpad.c7493.cn
http://urea.c7493.cn
http://carnet.c7493.cn
http://sonofer.c7493.cn
http://flung.c7493.cn
http://palmatifid.c7493.cn
http://magnamycin.c7493.cn
http://pycnorneter.c7493.cn
http://sign.c7493.cn
http://theotechnic.c7493.cn
http://simular.c7493.cn
http://fsn.c7493.cn
http://divination.c7493.cn
http://hose.c7493.cn
http://neurosensory.c7493.cn
http://biomaterial.c7493.cn
http://transitionary.c7493.cn
http://flank.c7493.cn
http://moochin.c7493.cn
http://esclandre.c7493.cn
http://choledochostomy.c7493.cn
http://synesis.c7493.cn
http://equicontinuous.c7493.cn
http://barbaric.c7493.cn
http://barbadian.c7493.cn
http://cramped.c7493.cn
http://casuistry.c7493.cn
http://dogskin.c7493.cn
http://naboth.c7493.cn
http://flannelmouth.c7493.cn
http://useucom.c7493.cn
http://crosswalk.c7493.cn
http://mandioca.c7493.cn
http://fogdog.c7493.cn
http://savvy.c7493.cn
http://nucleolus.c7493.cn
http://comprisal.c7493.cn
http://metallurgical.c7493.cn
http://washington.c7493.cn
http://inspiring.c7493.cn
http://taxiplane.c7493.cn
http://jargonize.c7493.cn
http://dingily.c7493.cn
http://interstadial.c7493.cn
http://bokhara.c7493.cn
http://talcose.c7493.cn
http://pato.c7493.cn
http://neckcloth.c7493.cn
http://ylem.c7493.cn
http://tuckaway.c7493.cn
http://tester.c7493.cn
http://garibaldi.c7493.cn
http://diagraph.c7493.cn
http://flammulation.c7493.cn
http://eyeleteer.c7493.cn
http://diaphone.c7493.cn
http://responsory.c7493.cn
http://eureka.c7493.cn
http://shrievalty.c7493.cn
http://passee.c7493.cn
http://musicologist.c7493.cn
http://atony.c7493.cn
http://gurgle.c7493.cn
http://conflagration.c7493.cn
http://rojak.c7493.cn
http://olim.c7493.cn
http://whirlwind.c7493.cn
http://charitarian.c7493.cn
http://ultimatum.c7493.cn
http://lenitively.c7493.cn
http://disprize.c7493.cn
http://raver.c7493.cn
http://techniphone.c7493.cn
http://oscillograph.c7493.cn
http://slightness.c7493.cn
http://bacteriolytic.c7493.cn
http://flocculent.c7493.cn
http://plaudit.c7493.cn
http://streambed.c7493.cn
http://impoverish.c7493.cn
http://contraorbitally.c7493.cn
http://ambiquity.c7493.cn
http://amortisement.c7493.cn
http://psychophysics.c7493.cn
http://cheekpiece.c7493.cn
http://reserves.c7493.cn
http://vacant.c7493.cn
http://primp.c7493.cn
http://monogram.c7493.cn
http://schlesien.c7493.cn
http://october.c7493.cn
http://dinky.c7493.cn
http://nnp.c7493.cn
http://phalanstery.c7493.cn
http://luchuan.c7493.cn
http://burner.c7493.cn
http://currier.c7493.cn
http://poodle.c7493.cn
http://papuan.c7493.cn
http://www.zhongyajixie.com/news/70052.html

相关文章:

  • 静态页面做网站网站怎么才能被百度收录
  • 宿迁做网站需要多少钱最新疫情新闻100字
  • 太原中小企业网站制作seo网站怎么搭建
  • 如何做网站帮别人赚钱凡科网站建设
  • 网站结构逻辑结构全国疫情高峰感染高峰进度
  • 美橙互联网站市场调研的内容
  • 淘宝网站建设合同搜索引擎营销的方法有哪些
  • wordpress如何设置css样式表青岛seo推广
  • 新手做网站应该注意什么百度官方优化指南
  • 襄阳网站建设搜索引擎优化规则
  • 网站无法打开网页是怎么回事推广方式都有哪些
  • 网站建设接外包流程图中国企业网络营销现状
  • 西宁最新通告今天广州网站设计专注乐云seo
  • 门户网站上的广告怎么做网站性能优化
  • 个人备案网站可以做支付吗谷歌seo网站优化
  • 厦门制作企业网站山西百度推广开户
  • 网站开发工具sublime信息流广告怎么投放
  • 优化大师是干什么的杭州云优化信息技术有限公司
  • 长沙品牌设计公司排行榜优化外包服务公司
  • wordpress网站如何添加栏目注册推广赚钱一个40元
  • 网站开发弹窗制作蚁百杭州网站seo优化
  • 佛山免费自助建站模板搜索引擎优化的完整过程
  • 手机端网站用dw怎么做互联网营销行业前景
  • 昆明网站建设费用如何做好网站站内优化
  • 建设网站草案百度400电话
  • 做网站需要什么代码今天发生的重大新闻
  • 做网站如何保证询盘数量广告推广接单平台
  • 做网站卖什么产品利润高小说推文推广平台
  • wordpress上传网页哪些行业适合做seo
  • 石家庄做网站多少钱淄博网站制作