leetcode每日刷题计划-简单篇day3

收到swe提前批面试hhh算是ep挂了的后续

努力刷题呀争取今年冲进去!

Num 21 合并两个有序链表 Merge Two Sorted Lists

注意新开的链表用来输出结果的是ListNode *l3=new ListNode(0)这样的写法

还有就是,注意一下可能会返回到NULL,有必要重新写一下

因为是链表,最后就直接补上去就ok了,一个一个加有可能触发NULL

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *l3=new ListNode(0);
        ListNode*temp;
        temp=l3;
        while(l1!=NULL && l2!=NULL)
        {
            if(l1->val<=l2->val)
            {
                l3->next=l1;
                l1=l1->next;
                l3=l3->next;
            }
            else
            {
                l3->next=l2;
                l2=l2->next;
                l3=l3->next;
            }
        }
        if(l1)
            l3->next=l1;
        if(l2)
            l3->next=l2;
        return temp->next;
    }
};
View Code

Num 28 实现strStr Implement strStr()

strStr(string a,string b)

题很简单,一个问题:b字符串为空的时候应该是返回0

class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle=="") return 0;
        int hlen=haystack.length();
        int nlen=needle.length();
        bool pd=false;
        for(int i=0;i<hlen-nlen+1;i++)
        {
            bool p=true;
            if(haystack[i]==needle[0])
            {
                for(int j=1;j<nlen;j++)
                {
                    if(haystack[i+j]!=needle[j])
                    {
                        p=false;
                        break;
                    }
                }
                if(p==true)
                    return i;
            }
        }
        return -1;
    }
};
View Code

 

posted on 2019-04-12 22:56 ltx_zero 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/tingxilin/p/10699191.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值