56. Merge Intervals\113. Path Sum II\211. Add and Search Word

来源:互联网 发布:淘宝考试题目答案 编辑:程序博客网 时间:2024/06/01 10:06

  • Merge Intervals
    • 题目描述
    • 代码实现
  • Path Sum II
    • 题目描述
    • 代码实现
  • 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") -> falsesearch("bad") -> truesearch(".ad") -> truesearch("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
原创粉丝点击