91. 解码方法
一条包含字母 A-Z
的消息通过以下方式进行了编码:
'A' -> 1
'B' -> 2
...
'Z' -> 26
给定一个只包含数字的非空字符串,请计算解码方法的总数。
示例 1:
输入: "12"
输出: 2
解释: 它可以解码为 "AB"(1 2)或者 "L"(12)。
示例 2:
输入: "226"
输出: 3
解释: 它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。
class Solution {
public:
int numDecodings(string s) {
if (s.empty())return 0;
vector<int>v(s.size() + 1, 0);
v[0] = 1;
v[1] = (s[0] == '0') ? 0 : 1;
for (int i = 2; i <= s.size(); ++i) {
auto t1 = stoi(s.substr(i - 1, 1));
if (t1>0 && t1<10)v[i] += v[i - 1];
auto t2 = stoi(s.substr(i - 2, 2));
if (t2<27 && t2>9)v[i] += v[i - 2];
}
return v.back();
}
};
92. 反转链表 II
反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。
说明:
1 ≤ m ≤ n ≤ 链表长度。
示例:
输入: 1->2->3->4->5->NULL, m = 2, n = 4
输出: 1->4->3->2->5->NULL
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(head==NULL || head->next==NULL || m>=n || m<0 || n<0) return head;
ListNode* h = new ListNode(-1);
h->next = head;
ListNode* pre = h;
ListNode* cur = head;
int i=1;
for(; i<m && cur != NULL; ++i){
pre = cur;
cur = cur->next;
}
ListNode* t1 = pre;
ListNode* t2 = cur;
for(; i<=n && cur != NULL; ++i){
ListNode* tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
t1->next = pre;
t2->next = cur;
return h->next;
}
};
93. 复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
示例:
输入: "25525511135"
输出: ["255.255.11.135", "255.255.111.35"]
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
vector<string> res;
if(!s.empty() && s.size()<4 && s.size()>12) return res;
restoreIpAdd(s, 4, 0, string(""), res);
return res;
}
void restoreIpAdd(string s, int n, int index, string ip, vector<string>& res){
if(n==0){
if(index == s.size()) res.push_back(ip);
return;
}
for(int i=index+1; i <= s.size(); i++){
string tmpS = string(s.begin()+index, s.begin()+i);
if(isNum(tmpS)){
if(ip.empty()) restoreIpAdd(s, n-1, i, tmpS, res);
else{
tmpS = ip+"."+tmpS;
restoreIpAdd(s, n-1, i, tmpS, res);
}
} else break;
}
}
bool isNum(string num){
if (0 <= stoi(num) and stoi(num) <= 255 and to_string(stoi(num)) == num){
return true;
}
return false;
}
};
94. 二叉树的中序遍历
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2]
95. 不同的二叉搜索树 II
给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。
示例:
输入: 3
输出:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
解释:
以上的输出对应以下 5 种不同结构的二叉搜索树:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
/**
* 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:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL) return res;
stack<TreeNode*> st;
while(!st.empty() || root!=NULL){
if(root!=NULL){
st.push(root);
root=root->left;
} else {
root=st.top();
st.pop();
res.push_back(root->val);
root=root->right;
}
}
return res;
}
};
95. 不同的二叉搜索树 II
给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。
示例:
输入: 3 输出: [ [1,null,3,2], [3,2,null,1], [3,1,null,null,2], [2,1,3], [1,null,2,null,3] ] 解释: 以上的输出对应以下 5 种不同结构的二叉搜索树: 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
/**
* 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:
vector<TreeNode*> generateTrees(int n) {
if(n==0) return vector<TreeNode*>();
return fun(1,n);
}
vector<TreeNode*> fun(int l, int r){
vector<TreeNode*> res;
if(l>r) res.push_back(NULL);
for(int k=l; k<=r; k++){
vector<TreeNode*> left=fun(l,k-1);
vector<TreeNode*> right=fun(k+1,r);
for(int i=0; i<left.size(); i++) {
for(int j=0; j<right.size(); j++){
TreeNode* tmp = new TreeNode(k);
tmp->left=left[i];
tmp->right=right[j];
res.push_back(tmp);
}
}
}
return res;
}
};
(以上题目均摘自leetcode)