博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode merge list and add two number,ismatch
阅读量:6086 次
发布时间:2019-06-20

本文共 3482 字,大约阅读时间需要 11 分钟。

preview:'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char *s, const char *p)Some examples:isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → trueclass Solution {public:    bool isMatch(string s, string p) {        if (p.empty())    return s.empty();                if ('*' == p[1])            // x* matches empty string or at least one character: x* -> xx*            // *s is to ensure s is non-empty            return (isMatch(s, p.substr(2)) || !s.empty() && (s[0] == p[0] || '.' == p[0]) && isMatch(s.substr(1), p));        else            return !s.empty() && (s[0] == p[0] || '.' == p[0]) && isMatch(s.substr(1), p.substr(1));    }};class Solution {public:    bool isMatch(string s, string p) {        /**         * f[i][j]: if s[0..i-1] matches p[0..j-1]         * if p[j - 1] != '*'         *      f[i][j] = f[i - 1][j - 1] && s[i - 1] == p[j - 1]         * if p[j - 1] == '*', denote p[j - 2] with x         *      f[i][j] is true iff any of the following is true         *      1) "x*" repeats 0 time and matches empty: f[i][j - 2]         *      2) "x*" repeats >= 1 times and matches "x*x": s[i - 1] == x && f[i - 1][j]         * '.' matches any single character         */        int m = s.size(), n = p.size();        vector
> f(m + 1, vector
(n + 1, false)); f[0][0] = true; for (int i = 1; i <= m; i++) f[i][0] = false; // p[0.., j - 3, j - 2, j - 1] matches empty iff p[j - 1] is '*' and p[0..j - 3] matches empty for (int j = 1; j <= n; j++) f[0][j] = j > 1 && '*' == p[j - 1] && f[0][j - 2]; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) if (p[j - 1] != '*') f[i][j] = f[i - 1][j - 1] && (s[i - 1] == p[j - 1] || '.' == p[j - 1]); else // p[0] cannot be '*' so no need to check "j > 1" here f[i][j] = f[i][j - 2] || (s[i - 1] == p[j - 2] || '.' == p[j - 2]) && f[i - 1][j]; return f[m][n]; }};class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if(l1 == NULL) return l2; if(l2 == NULL) return l1; if(l1->val < l2->val) { l1->next = mergeTwoLists(l1->next, l2); return l1; } else { l2->next = mergeTwoLists(l2->next, l1); return l2; } }};This solution is not a tail-recursive, the stack will overflow while the list is too long :)http://en.wikipedia.org/wiki/Tail_callListNode * addtwonumber(ListNode *l1,ListNode *l2){ ListNode prenode(0),*p = &prenode; int extra = 0; while (l1 || l2|| extra) { int sum =(l1?l1->val:0) +(l2?l2->val:0) + extra; extra = sum / 10; p->next =new ListNode(sum %10); l1 = l1 ? l1->next : l1; l2 = l2 ? l2->next : l2; } return prenode.next;} ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode myList(INT_MIN); ListNode *p = &myList; while (l1 && l2) { if (l1->val < l2->val) { p->next=l1; l1=l1->next; }else { p->next=l2; l2 = l2->next; } p = p->next; } p->next = l1 ?l1:l2; return p->next;}

  

转载于:https://www.cnblogs.com/fanchaostudy/p/7144034.html

你可能感兴趣的文章
以太坊系列之六: p2p模块--以太坊源码学习
查看>>
使用scikit-learn解决文本多分类问题(附python演练)
查看>>
2018 年最值得关注的 JavaScript 趋势
查看>>
什么是区块链?超级账本 Brian Behlendorf 从五个方面教你认识
查看>>
Linux中的帮助功能
查看>>
针对Android的Pegasus恶意软件版本和针对iOS的有什么不同?
查看>>
全局探色器
查看>>
Hive Export和Import介绍及操作示例
查看>>
http://mongoexplorer.com/ 一个不错的 mongodb 客户端工具。。。
查看>>
Xcode 4.3 使用xcodebuild命令编译项目环境设置
查看>>
上传jar包到nexus私服
查看>>
Why Namespace? - 每天5分钟玩转 OpenStack(102)
查看>>
Project:如何分析项目中的资源分配情况
查看>>
HDU 4803 Poor Warehouse Keeper (贪心+避开精度)
查看>>
小错误汇总
查看>>
Spring源码系列 — Envoriment组件
查看>>
windbg java.pdb,PDB符号文件与Windows下利用Windbg 分析dump
查看>>
php输出语句中怎么计算,下列选项中,可以作为PHP的输出语句的是( )。
查看>>
mycat mysql毫秒,mycat 配置mysql读写分离+高可用切换不过去
查看>>
php评论用什么存储,redis存储用户评论
查看>>