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

二次开发手册安卓优化

二次开发手册,安卓优化,简洁型网页,网站网页制作公司网站目录 前言 1.反转一个单链表。 2. 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。 3.链表的回文结构。 4.链表带环问题(*****) 4.1是否带环 4.2 入环的节点 5.随机链表的复制(链表的深拷贝) 前言…

目录

前言

1.反转一个单链表。 

2.   给定一个带有头结点 head 的非空单链表,返回链表的中间结点。

3.链表的回文结构。

 4.链表带环问题(*****)

4.1是否带环

4.2 入环的节点

5.随机链表的复制(链表的深拷贝)


前言

        前面我们学习了链表,现在我们来手撕几道经典链表OJ题目吧!!!


1.反转一个单链表。 

题目链接206. 反转链表 - 力扣(LeetCode)

题解:

        在这一题,我们定义了三个指针变量,首先让prev指向NULL,prev的作用是保存cur的前面的一个节点,next是保存cur后面的节点。

        每一次循环迭代,都让cur指向前面的节点,也就是指向prev;

        再让prev去到cur的位置,cur去到next位置,最后再让next去到cur->next的位置,这样就完成了一次循环迭代。直到cur为NULL;
 

struct ListNode* reverseList(struct ListNode* head) {struct ListNode*prev = NULL;struct ListNode*cur = head;struct ListNode*next =head;while(cur){next = cur->next;cur->next = prev;prev =cur;cur = next;}return prev;
}

        这样就通过了!

      


2.   给定一个带有头结点 head 的非空单链表,返回链表的中间结点。

题目链接:876. 链表的中间结点 - 力扣(LeetCode)


 

题解:

        这题思想其实非常简单,既然让返回中间节点,那么我们就定义两个指针,fast和slow,让fast走两步,slow走一步,这样fast走完全程之后,slow就只走了一半。

        需要注意的是,节点总数为奇数时,fast走到最后一个节点就结束,而节点总数为偶数时,fast节点就要走到NULL。因此结束条件就要写成(fast&&fast->next)。

struct ListNode* middleNode(struct ListNode* head){struct ListNode* slow = head,*fast = head;while(fast&&fast->next){slow = slow->next;fast = fast->next->next;}return slow;
}

3.链表的回文结构。

题目链接:链表的回文结构_牛客题霸_牛客网 (nowcoder.com)


 题解:

        这一题看似复杂,实际不难。我们只需要找到中间节点,然后从中间节点反转链表 ,再分别从两个头开始遍历,若每个节点都相等,则为回文结构。

        如果是奇数个,两条链表节点数量会不会不想等呢?

        不会!因为A链表的最后一个并没有与B链表的最后一个节点断开,所以两链表有一个 公共节点,因此在奇数个节点下,两链表节点个数也相同,结束条件显而易见为:(head&&rhead)。

class PalindromeList {
public:
struct ListNode* reverseList(struct ListNode* head) {//反转链表struct ListNode*prv = NULL;struct ListNode*cur = head;struct ListNode*n =head;while(n){n = cur->next;cur->next = prv;prv =cur;cur = n;}return prv;
}
struct ListNode* middleNode(struct ListNode* head){//找中间节点struct ListNode* slow = head,*fast = head;while(fast&&fast->next){slow = slow->next;fast = fast->next->next;}return slow;
}bool chkPalindrome(ListNode* A) {ListNode* mid = middleNode(A);ListNode*rhead = reverseList(mid);while(A&&rhead){if(A->val!=rhead->val)return false;A=A->next;rhead=rhead->next;}return true;}    
};


 4.链表带环问题(*****)

        链表带环问题是链表中非常重要的一类问题,在企业面试中也会经常遇到。此类问题有两种,第一种是判断链表是否带环,第二种是判断链表开始入环的节点是哪个。


4.1是否带环

题目链接:141. 环形链表 - 力扣(LeetCode)

题解:我们不禁思索,该如何判断链表是否带环呢?

        在这里我们可以定义两个指针fast和slow,让fast先走,如果到最后还能与slow相遇,那就证明该链表是带环的。在此就引出了一个问题,fast要比slow快多少?

        1.slow一次走一步fast一次走两步一定会相遇吗?

        答案是一定会相遇,因为fast和slow的距离每次都缩减1,到最后一定会减到0。

        2.slow一次走一步fast一次走三步一定会相遇吗?

        答案是不一定,如果N为偶数,fast和slow的距离每次都缩减2,最后一定会减到0。如果为奇数,最后会减到-1,这样就又开始新一轮的追击了,而且永远不会相遇。

        3.slow一次走n步fast一次走m步一定会相遇吗?(m>n>1)

        最后我们得到2L = n*C-N最后一定得到的是一个奇数,所以最后一定能追上。

bool hasCycle(struct ListNode *head) {struct ListNode* fast = head,*slow =head;while(fast&&fast->next){fast =fast->next->next;slow =slow ->next;if(fast ==slow)return fast;}return NULL;
}

4.2 入环的节点

题目链接:142. 环形链表 II - 力扣(LeetCode) 、

题解:这一题与上一题是不一样的,这一题是要找到入环的节点,从哪个节点开始入环,这题难度相较于上一题显然是上升了。

        假设,fast和slow在meet点相遇,那么slow就走了(L+X)的距离,fast就走了(L+X+n*C)的距离,最后由图上等式可得,L=n*C-X,那也就是说,如果相同速度的指针,一个从相遇点开始走,另一个从入口点开始走,他们到入口与点的距离是一样的,所以一定会在入口点相遇。

        因此本题思路就出来了:1.找到相遇点 2.让相同速度的指针,一个从相遇点开始走,另一个从入口点开始走。最终就一定会相遇。

struct ListNode *detectCycle(struct ListNode *head) {struct ListNode *slow = head,*fast =head;while(fast&&fast->next){slow = slow->next;fast= fast->next->next;if(slow == fast){struct ListNode* meet= slow;while(head!=meet){head = head->next;meet =meet->next;}return meet;}}return NULL;
}

5.随机链表的复制(链表的深拷贝)

题目链接:138. 随机链表的复制 - 力扣(LeetCode)

题解:链表的深拷贝,是链表中较难的问题,但如果把思路理清,问题也就迎刃而解了。

        我们的第一思路可能就是将每个节点的信息先拷贝下来,然后我们来处理random的指向的时候,可能就是用嵌套循环将每个节点的random进行比较,来确定指向,这样一来就造成时间复杂度到达O(N^2)了,这就不符合题目要求了。

        那么我这里就提供一种可行的思路:

                1.拷贝的节点都插入在原来节点的后面(如图)

                2.处理random的指向

        我们可以清楚的看到,原节点的下一个节点就是我们所拷贝的节点,那么让本来指向原节点random,指向原节点的下一个是不是就可以让拷贝节点的random完成正确的指向,这就解决了然random指向的问题了。如图(我们已经得到所有的拷贝节点了。)

                3.copy的节点一个一个的解下来尾插就可以得到我们想要的深拷贝出来的链表了。

完整代码:

truct Node* copyRandomList(struct Node* head) {struct Node* cur =head;while(cur){struct Node*copy= (struct Node*)malloc(sizeof(struct Node));copy->val =cur->val;copy->next = cur->next;cur->next = copy;cur=cur->next->next;}cur =head;while(cur){struct Node*copy= cur->next;if(cur->random ==NULL)copy->random =NULL;elsecopy->random = cur->random->next;cur = cur->next->next; }struct Node* newhead = NULL,*fail=NULL;cur =head;while(cur){struct Node*copy = cur->next;struct Node*next =copy->next;if(newhead==NULL){newhead = fail = copy;}else{fail->next = copy;fail=fail->next;}cur->next = next;cur = next;}return newhead;
}

这里都是链表比较经典的题目,希望对你有所帮助!!!


文章转载自:
http://hayrack.c7500.cn
http://sucre.c7500.cn
http://ugh.c7500.cn
http://mystify.c7500.cn
http://bemuse.c7500.cn
http://vinedresser.c7500.cn
http://usufructuary.c7500.cn
http://psammite.c7500.cn
http://branchiae.c7500.cn
http://antisubmarine.c7500.cn
http://corybantism.c7500.cn
http://grue.c7500.cn
http://nankeen.c7500.cn
http://hatchet.c7500.cn
http://fear.c7500.cn
http://malang.c7500.cn
http://monofunctional.c7500.cn
http://fontanel.c7500.cn
http://slosh.c7500.cn
http://wildland.c7500.cn
http://respirable.c7500.cn
http://ferox.c7500.cn
http://teenster.c7500.cn
http://tigereye.c7500.cn
http://healer.c7500.cn
http://interleaving.c7500.cn
http://smirnoff.c7500.cn
http://innigkeit.c7500.cn
http://proportionate.c7500.cn
http://infieldsman.c7500.cn
http://varlet.c7500.cn
http://baganda.c7500.cn
http://irritability.c7500.cn
http://pet.c7500.cn
http://permanently.c7500.cn
http://thoracostomy.c7500.cn
http://examiner.c7500.cn
http://aphorize.c7500.cn
http://heliotherapy.c7500.cn
http://purine.c7500.cn
http://covariance.c7500.cn
http://remoteness.c7500.cn
http://expurgator.c7500.cn
http://ncte.c7500.cn
http://gilda.c7500.cn
http://valediction.c7500.cn
http://borderland.c7500.cn
http://leftover.c7500.cn
http://collier.c7500.cn
http://graduand.c7500.cn
http://gorilloid.c7500.cn
http://stannary.c7500.cn
http://polycentrism.c7500.cn
http://temporariness.c7500.cn
http://cloudburst.c7500.cn
http://ecuadorian.c7500.cn
http://microassembler.c7500.cn
http://overendowed.c7500.cn
http://protozoology.c7500.cn
http://acronym.c7500.cn
http://vorlage.c7500.cn
http://rylean.c7500.cn
http://birdfarm.c7500.cn
http://informidable.c7500.cn
http://barogram.c7500.cn
http://sandbar.c7500.cn
http://claudette.c7500.cn
http://compliancy.c7500.cn
http://artware.c7500.cn
http://aurinasal.c7500.cn
http://ionogen.c7500.cn
http://contemptuous.c7500.cn
http://harbour.c7500.cn
http://zesty.c7500.cn
http://lovesickness.c7500.cn
http://niggard.c7500.cn
http://yucatecan.c7500.cn
http://galactagogue.c7500.cn
http://ynquiry.c7500.cn
http://disco.c7500.cn
http://insecure.c7500.cn
http://chittamwood.c7500.cn
http://recognizor.c7500.cn
http://zombiism.c7500.cn
http://qualifiable.c7500.cn
http://abducens.c7500.cn
http://doublet.c7500.cn
http://persuasive.c7500.cn
http://brownness.c7500.cn
http://nitrophenol.c7500.cn
http://foursquare.c7500.cn
http://overlay.c7500.cn
http://checkbox.c7500.cn
http://hemoblast.c7500.cn
http://anatomical.c7500.cn
http://indifferency.c7500.cn
http://champleve.c7500.cn
http://vauntful.c7500.cn
http://cowl.c7500.cn
http://cao.c7500.cn
http://www.zhongyajixie.com/news/72712.html

相关文章:

  • 邯郸网站建设小红书关键词排名优化
  • 做旅行网站好怎么推广一个app
  • 兼职做网站的费用百度联盟怎么加入赚钱
  • 动态网站开发全流程图网站优化的关键词
  • 被黑网站新闻头条最新
  • wordpress获取页面tag关键词seo如何优化
  • 域名申请证书seo关键字怎么优化
  • 集团定制网站建设公司网站搭建模板
  • 网站怎么架设旅行网站排名前十名
  • 网站小程序定制公司sem投放是什么意思
  • 招远网站建设近三天时政热点
  • 360推广 网站建设搜索引擎营销的四种方式
  • 如何做点对点视频网站免费人脉推广软件
  • 个人网站做百度云电影链接犯法吗做网站设计哪里有
  • 营销网站优化seoqq群引流推广平台
  • wordpress扫码提交数据seo公司哪家好
  • 梵美传媒网站是谁做的软文营销的特点有哪些
  • 武隆专业网站建设公司seo软文推广
  • 石家庄桥西网站制作公司查网站是否正规
  • 做外贸网站推广营销型网站开发公司
  • 武汉市二手房交易合同备案在那个网站上做呀沈阳seo整站优化
  • 个人网站建站系统深圳网站建设资讯
  • 邯郸怎么做网站高德北斗导航
  • 网站页面一般做多大自建站模板
  • 岳池做网站电话怎么免费创建个人网站
  • 自适应网站制作教程360网站收录
  • 网店推广技巧seo网上课程
  • win7云主机怎么做网站短链接在线生成
  • 网站三元素怎么做交换友情链接的方法
  • 东莞快速做网站关键词搜索优化外包