56. Merge Intervals\113. Path Sum II\211. Add and Search Word - Data structure design

56. Merge Intervals

题目描述

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

代码实现

这道题目就是排序的运用,可以使用复杂度O(n^2)的暴力破解的方法,但是更加推荐的是先排序再比较,这样复杂度就是O(n log n)

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:
    static bool MyCompare(Interval &a, Interval &b) {
        return a.start < b.start;
    }
    vector<Interval> merge(vector<Interval>& intervals) {
        sort(intervals.begin(), intervals.end(), MyCompare);
        vector<Interval> res;
        for(int i = 0, n = intervals.size(); i < n; i++) {
            int lst = i + 1;
            int left = intervals[i].start, right = intervals[i].end;
            while(lst < n && right >= intervals[lst].start) {
                if(right < intervals[lst].end) right = intervals[lst].end;
                lst++; i++;
            }
            res.push_back(Interval(left, right));    
        }
        return res;
    }
};

113. Path Sum II

题目描述

Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1

return

[
   [5,4,11,2],
   [5,8,4,5]
]

代码实现

使用简单的DFS即可解决这个问题。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void PathSum(vector<vector<int>> &res, TreeNode *root, int sum, vector<int> tmp) {
        if(root == NULL)  return;
        tmp.push_back(root->val);
        if(!root->left && !root->right) { if(sum == root->val)  res.push_back(tmp); }
        else { 
            if(root->left)  PathSum(res, root->left, sum - root->val, tmp);
            if(root->right)  PathSum(res, root->right, sum - root->val, tmp);
        }    
    }

    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>> res;
        vector<int> tmp;
        PathSum(res, root, sum, tmp);
        return res;
    }
};

211. Add and Search Word - Data structure design

题目描述

Design a data structure that supports the following two operations:

void addWord(word)
bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

For example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

Note:
You may assume that all words are consist of lowercase letters a-z.

代码实现

最开始使用数据结构是set,然后超时了。

class WordDictionary {
private:
    set<string> database;
public:
    /** Initialize your data structure here. */
    WordDictionary() {

    }

    /** Adds a word into the data structure. */
    void addWord(string word) {
        database.insert(word);    
    }

    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    bool search(string word) {
        int wlen = word.size();
        for(set<string>::iterator it = database.begin(); it != database.end(); it++) {
            string tmp = *it;
            if(word[0] != '.' && tmp[0] > word[0]) return false;
            if(tmp.size() != wlen) continue;
            int i = 0;
            for(i = 0; i < wlen; i++) 
                if(word[i] != '.' && tmp[i] != word[i]) break;
            if(i == wlen) return true;
        }
        return false;
    }
};

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * bool param_2 = obj.search(word);
 */

还在继续改

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值