Day four

算法题:
题目来源:力扣(LeetCode)
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。
说明:不允许修改给定的链表。
进阶:
你是否可以使用 O(1) 空间解决此题?

示例 1:
在这里插入图片描述
输入:head = [3,2,0,-4], pos = 1
输出:返回索引为 1 的链表节点
解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:
在这里插入图片描述
输入:head = [1,2], pos = 0
输出:返回索引为 0 的链表节点
解释:链表中有一个环,其尾部连接到第一个节点。

示例 3:
在这里插入图片描述
输入:head = [1], pos = -1
输出:返回 null
解释:链表中没有环。

提示:
链表中节点的数目范围是 [0, 104]
-105 <= Node.val <= 105
pos 为 -1 或者链表中的一个 有效索引 。

解题思路
这是昨天141的进阶题142,今天是需要找到成环的这个节点。昨天的set方法是可以直接使用的,我就不赘述了。而快慢指针法需要有一定的改进,快慢指针只是帮我们确定了链表是否为环,还需要其他步骤去找到成环的节点。这个思路我是从B站学来的,截图至上:
在这里插入图片描述
由于我们设置的fast走的步数是slow的两倍,所以两个相遇,fast必定走了slow的两倍路程,就能得到上图公式:2(a+b)=a+b+c+b,就会很神奇的发现,这时候如果slow继续走,head从头走,当他们相遇时,刚好是相交节点,所以就能得到下列代码:

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode*slow=head;
        ListNode*fast=head;
        while(fast!=NULL&&fast->next!=NULL){
            slow=slow->next;
            fast=fast->next->next;
            if(fast==slow)
            {
                while(head!=slow)
                {
                    slow=slow->next;
                    head=head->next;
                }
                return head;
            }
        }
        return NULL;
    }
};

算法题:
题目来源:力扣(LeetCode)
给你一个链表和一个特定值 x ,请你对链表进行分隔,使得所有小于 x 的节点都出现在大于或等于 x 的节点之前。
你应当保留两个分区中每个节点的初始相对位置。

示例:
输入:head = 1->4->3->2->5->2, x = 3
输出:1->2->2->4->3->5

解题思路:
就找两个空节点,分别放比x小的,和大于等于x的,然后两个链表链接就是结果了。

class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode less(0);
        ListNode more(0);
        ListNode* pl=&less;
        ListNode* pm=&more;
        if(head==NULL||head->next==NULL)return head;
        while(head)
        {
            if(head->val<x)
            {
                pl->next=head;
                pl=head;
            }else{
                pm->next=head;
                pm=head;
            }
            head=head->next;
        }
        pl->next=more.next;
        pm->next=NULL;
        return less.next;
    }
};

前端

今天看了《Head First HTML与CSS》的前两章,记了些随笔。

1.HTML会告诉浏览器文档的结构:例如,标题放在哪,段落放在哪。

2.浏览器会忽略HTML文档的制表符、回车和大部分空格。解决方法:利用实体。

3.虽说标题h有1-6,但一般都不会超过3

4.注释:

5.文件要分类放好

6.文件名不要有空格!!!

7.“…”表示”父文件夹”

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
马丁路德金的"I have a dream"节选存放在"freedom.txt"中: I have a dream that one day this nation will rise up, live up to the true meaning of its creed: “We hold these truths to be self-evident; that all men are created equal.” I have a dream that one day on the red hills of Georgia the sons of former slaves and the sons of former slave-owners will be able to sit down together at the table of br otherhood. I have a dream that one day even the state of Mississippi, a state sweltering with th e heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice. I have a dream that my four children will one day live in a nation where they will no t be judged by the color if their skin but by the content of their character. I have a dream today. I have a dream that one day down in Alabama with its governor having his lips drippin g with the words of interposition and nullification, one day right down in Alabama li ttle black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers. I have a dream today. I have a dream that one day every valley shall be exalted, every hill and mountain sh all be made low, the rough places will be made plain, and the crooked places will be made straight, and the glory of the Lord shall be revealed, and all flesh shall see i t together. 编程实现词汇表,计算每一个单词出现的次数,大小写不区分,输出到"dic.txt" 文件保存。
最新发布
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Monster_White

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值