Leetcode题解(29)

93. Restore IP Addresses

题目

Leetcode题解(29)_第1张图片

分析:多重循环,判断小数点合适的位置

代码如下(copy网上)

 1 class Solution {
 2 public:
 3     vector<string> restoreIpAddresses(string s) {
 4         vector<string> ret;
 5         if(s.size() > 12)
 6             return ret;
 7         for(int i = 0; i < s.size(); i ++)
 8         {// [0, i]
 9             for(int j = i+1; j < s.size(); j ++)
10             {// [i+1, j]
11                 for(int k = j+1; k < s.size()-1; k ++)
12                 {// [j+1, k], [k+1, s.size()-1]
13                     string ip1 = s.substr(0, i+1);
14                     string ip2 = s.substr(i+1, j-i);
15                     string ip3 = s.substr(j+1, k-j);
16                     string ip4 = s.substr(k+1);
17                     if(check(ip1) && check(ip2) && check(ip3) && check(ip4))
18                     {
19                         string ip = ip1 + "." + ip2 + "." + ip3 + "." + ip4;
20                         ret.push_back(ip);
21                     }
22                 }
23             }
24         }
25         return ret;
26     }
27     bool check(string ip)
28     {
29         int value = stoi(ip);
30         if(ip[0] == '0')
31         {
32             return (ip.size() == 1);
33         }
34         else
35         {
36             if(value <= 255)
37                 return true;
38             else
39                 return false;
40         }
41     }
42 };

 ----------------------------------------------------------------------分割线--------------------------------------------------------------------------

94. Binary Tree Inorder Traversal

题目

Leetcode题解(29)_第2张图片

分析:二叉树的中序遍历,题目要求不能使用递归,因此为了实现中序遍历,需要使用一个stack,只是在存储节点的 时候,中间节点利用两个NULL包裹起来存放。详细请看代码中的注释,代码如下:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> inorderTraversal(TreeNode* root) {
13         vector<int> res;
14         if(root == NULL)
15             return res;
16         stack<TreeNode*> myStack;
17         TreeNode *temp;
18         myStack.push(root);
19         while(!myStack.empty())
20         {
21             temp = myStack.top();
22             myStack.pop();
23             
24             if(NULL == temp)//表示访问到中间节点了,需要读取val
25             {
26                 temp = myStack.top();
27                 myStack.pop();
28                 res.push_back(temp->val);
29                 temp = myStack.top();
30                 myStack.pop();
31                 continue;                
32             }
33             //右节点push
34             if(temp->right != NULL)
35                 myStack.push(temp->right);
36             
37             //利用两个NULL把中间节点包裹起来
38             myStack.push(NULL);
39             myStack.push(temp);
40             myStack.push(NULL);
41             
42             //左节点push
43             if(temp->left != NULL)
44             {                
45                 myStack.push(temp->left);
46             }
47         }
48         return res;
49         
50     }
51 };

 -------------------------------------------------------------------------------分割线-------------------------------------------------------------------

95. Unique Binary Search Trees II

题目

Leetcode题解(29)_第3张图片

分析:递归求解,代码如下(copy网上代码)

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<TreeNode *> generateTrees(int n) {
13         vector<TreeNode *> res;
14         if(n==0)
15         return res;
16         return Helper(1, n);
17     }
18     vector<TreeNode *> Helper(int begin, int end)
19     {
20         vector<TreeNode *> ret;
21         if(begin > end)
22             ret.push_back(NULL);
23         else if(begin == end)
24         {
25             TreeNode* node = new TreeNode(begin);
26             ret.push_back(node);
27         }
28         else
29         {
30             for(int i = begin; i <= end; i ++)
31             {//root
32                 vector<TreeNode *> left = Helper(begin, i-1);
33                 vector<TreeNode *> right = Helper(i+1, end);
34                 for(int l = 0; l < left.size(); l ++)
35                 {
36                     for(int r = 0; r < right.size(); r ++)
37                     {
38                         //new tree
39                         TreeNode* root = new TreeNode(i);
40                         root->left = left[l];
41                         root->right = right[r];
42                         ret.push_back(root);
43                     }
44                 }
45             }
46         }
47         return ret;
48     }
49 };

 ----------------------------------------------------------------------------分割线---------------------------------------------------------------------

96. Unique Binary Search Trees

题目

Leetcode题解(29)_第4张图片

分析:这题和95题是一样的,代码如下(copy网上代码):

 1 class Solution {
 2 public:
 3     int numTrees(int n) {
 4         int f[n+1];
 5         memset (f , 0 , sizeof(int)*(n+1));
 6         f[0]=1;
 7         for(int i=1; i<=n; i++)
 8             for(int j=0; j<i; j++)
 9                 f[i] += f[j] * f[i-j-1];
10         
11         return f[n];
12     }
13 };

 

你可能感兴趣的:(Leetcode题解(29))