28.实现strStr
/*实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
示例 1:
输入: haystack = "hello", needle = "ll"
输出: 2
示例 2:
输入: haystack = "aaaaa", needle = "bba"
输出: -1
说明:
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-strstr
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。*/
//自己写,超时,遍历两个字符串,暴力时间复杂度O(mn)
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle == "") return 0;
if (haystack.size() < needle.size()) return -1;
for (int i = 0; i < haystack.size(); ++i) {
int flag = i;
for (int j = 0; j < needle.size(); ++j) {
if (haystack[flag] == needle[j]) {
flag++;
}
}
int x = flag - i;
if (x == needle.size()) return i;
}
return -1;
}
};
//正确暴力解法
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle == "") return 0;
if (haystack.size() < needle.size()) return -1;
int p = haystack.size();
for (int i = 0; i < haystack.size(); ++i) {
int l = p-i;
if(haystack[i] == needle[0]&&l>=needle.size()){
int start = i;
int z = i;
int count = 0;
for(int j = 0;j<needle.size();++j){
if(haystack[z] == needle[j]){
z++;
count++;
}
}
if(count == needle.size()) return start;
}
}
return -1;
}
};
173.二叉搜索树迭代器
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public:
BSTIterator(TreeNode* root) {
stack<pair<TreeNode*,bool>> zyw;
zyw.emplace(make_pair(root,false));
while(!zyw.empty()){
TreeNode* root = zyw.top().first;
bool visited = zyw.top().second;
zyw.pop();
if(root == nullptr) continue;
if(visited) path.push_back(root);
else{
zyw.emplace(make_pair(root->right,false));
zyw.emplace(make_pair(root,true));
zyw.emplace(make_pair(root->left,false));
}
}
cursor = 0;
}
/** @return the next smallest number */
int next() {
int x = path[cursor]->val;
cursor++;
return x;
}
/** @return whether we have a next smallest number */
bool hasNext() {
if(cursor<path.size()) return true;
return false;
}
public:
vector<TreeNode*> path;
int cursor;
};
/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator* obj = new BSTIterator(root);
* int param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/
面试题0403.特定深度节点链表
/*给定一棵二叉树,设计一个算法,创建含有某一深度上所有节点的链表(比如,若一棵树的深度为 D,则会创建出 D 个链表)。返回一个包含所有深度的链表的数组。
示例:
输入:[1,2,3,4,5,null,7,8]
1
/ \
2 3
/ \ \
4 5 7
/
8
输出:[[1],[2,3],[4,5,7],[8]]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/list-of-depth-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> listOfDepth(TreeNode* tree) {
queue<TreeNode*> zyw;
vector<vector<int>> res;
if(!tree) return {};
zyw.push(tree);
while(!zyw.empty()){
int ss = zyw.size();
vector<int> tmp;
for(int i = 0;i<ss;++i){
TreeNode *pNode = zyw.front();
tmp.push_back(pNode->val);
zyw.pop();
if(pNode->left) zyw.push(pNode->left);
if(pNode->right) zyw.push(pNode->right);
}
res.push_back(tmp);
}
vector<ListNode*> ans;
for(int i = 0;i<res.size();++i){
ListNode* head = new ListNode(-1);
ListNode* p1 = head;
for(int j = 0;j<res[i].size();++j){
if(j != res[i].size()) {
p1->next = new ListNode(res[i][j]);
p1 = p1->next;
}
else{
p1->next = nullptr;
}
}
ans.push_back(head->next);
}
return ans;
}
};
88.合并两个有序数组,暴力自解
/*给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组。
说明:
初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。
你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。
示例:
输入:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
输出: [1,2,2,3,5,6]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-sorted-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。*/
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int k = 0;
int i,j;
int p = m+n;
int res[p];
for(i = 0,j = 0,k=0;i<m&&j<n&&k<m+n;k++){
if(nums1[i]<nums2[j]) {res[k] = nums1[i];++i;}
else {res[k] = nums2[j];++j;}
}
while(i<m) res[k++] = nums1[i++];
while(j<n) res[k++] = nums2[j++];
for(int t = 0;t<p;++t){
nums1[t] = res[t];
}
}
};