zoukankan      html  css  js  c++  java
  • 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
    时间才能证明一切,选好了就尽力去做吧!
  • 相关阅读:
    2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum
    Codeforces Round #412 C. Success Rate (rated, Div. 2, base on VK Cup 2017 Round 3)
    2017 中国大学生程序设计竞赛 女生专场 Building Shops (hdu6024)
    51nod 1084 矩阵取数问题 V2
    Power收集
    红色的幻想乡
    Koishi Loves Segments
    Wood Processing
    整数对
    Room and Moor
  • 原文地址:https://www.cnblogs.com/tingxilin/p/10699191.html
Copyright © 2011-2022 走看看