文章目录
- Facebook面试题
- LeetCode 125. Valid Palindrome
- LeetCode 88. Merge Sorted Array
- LeetCode 278. First Bad Version
- LeetCode 98. Validate Binary Search Tree
- LeetCode 173. Binary Search Tree Iterator
- LeetCode 238. Product of Array Except Self
- LeetCode 56. Merge Intervals
- LeetCode 75. Sort Colors
- LeetCode 133. Clone Graph
- LeetCode 76. Minimum Window Substring
Facebook面试题
LeetCode 125. Valid Palindrome
class Solution {
public:
bool check(char c)
{
return c >= '0' && c <= '9' || c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z';
}
bool isPalindrome(string s) {
for(int i = 0, j = s.size()-1; i < j; i++, j--)
{
while(i < j && !check(s[i]))i++;
while(i < j && !check(s[j]))j--;
if(s[i] != s[j] && s[i] != (s[j]^32))return false;
}
return true;
}
};
LeetCode 88. Merge Sorted Array
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int k = m + n - 1;
m--, n--;
while(m >= 0 && n >= 0)
{
if(nums1[m] > nums2[n])nums1[k--] = nums1[m--];
else nums1[k--] = nums2[n--];
}
while(n >= 0)nums1[k--] = nums2[n--];
}
};
LeetCode 278. First Bad Version
bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
int l = 1, r = n;
while(l < r)
{
int mid = (l + 0ll + r) >> 1;
if(isBadVersion(mid))r = mid;
else l = mid + 1;
}
return r;
}
};
LeetCode 98. Validate Binary Search Tree
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(!root)return true;
int minV, maxV;
return dfs(root, minV, maxV);
}
bool dfs(TreeNode *root, int &minV, int &maxV)
{
minV = maxV = root->val;
if(root->left)
{
int nowMinV, nowMaxV;
if(!dfs(root->left, nowMinV, nowMaxV))return false;
if(nowMaxV >= root->val)return false;
minV = nowMinV;
}
if(root->right)
{
int nowMinV, nowMaxV;
if(!dfs(root->right, nowMinV, nowMaxV))return false;
if(nowMinV <= root->val)return false;
maxV = nowMaxV;
}
return true;
}
};
LeetCode 173. Binary Search Tree Iterator
class BSTIterator {
public:
stack<TreeNode*> stk;
BSTIterator(TreeNode* root) {
while(root)
{
stk.push(root);
root = root->left;
}
}
int next() {
auto t = stk.top();
stk.pop();
for(auto p = t->right; p; p = p->left)
{
stk.push(p);
}
return t->val;
}
bool hasNext() {
return !stk.empty();
}
};
LeetCode 238. Product of Array Except Self
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> res(nums.size());
for(int i = 0, t = 1; i < nums.size(); i++)
{
res[i] = t;
t *= nums[i];
}
for(int i = nums.size()-1, t = 1; i >= 0; i--)
{
res[i] *= t;
t *= nums[i];
}
return res;
}
};
LeetCode 56. Merge Intervals
class Solution {
public:
static bool cmp(vector<int> a, vector<int> b)
{
return a[0] < b[0];
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> res;
if(intervals.empty())return res;
sort(intervals.begin(), intervals.end(), cmp);
int st = intervals[0][0], ed = intervals[0][1];
for(int i = 1; i < intervals.size(); i++)
{
if(ed < intervals[i][0])
{
res.push_back({st, ed});
st = intervals[i][0], ed = intervals[i][1];
}
else
{
ed = max(ed, intervals[i][1]);
}
}
res.push_back({st, ed});
return res;
}
};
LeetCode 75. Sort Colors
class Solution {
public:
void sortColors(vector<int>& nums) {
int red = 0, blue = nums.size() - 1;
for(int i = 0; i <= blue;)
{
if(nums[i] == 0)swap(nums[i++], nums[red++]);
else if(nums[i] == 2)swap(nums[i], nums[blue--]);
else i++;
}
}
};
LeetCode 133. Clone Graph
class Solution {
public:
unordered_map<int, Node*> hash;
Node* cloneGraph(Node* node) {
if(!node)return node;
auto root = new Node(node->val, vector<Node*>());
hash[node->val] = root;
dfs(node);
return root;
}
void dfs(Node* node)
{
for(auto &neighbor:node->neighbors)
{
if(!hash.count(neighbor->val))
{
hash[neighbor->val] = new Node(neighbor->val, vector<Node*>());
dfs(neighbor);
}
hash[node->val]->neighbors.push_back(hash[neighbor->val]);
}
}
};
LeetCode 76. Minimum Window Substring
class Solution {
public:
string minWindow(string s, string t) {
string res;
unordered_map<char, int> S, T;
for(auto c : t)T[c]++;
int total = T.size();
int satisfy = 0;
for(int i = 0, j = 0; i < s.size(); i ++)
{
S[s[i]]++;
if(S[s[i]] == T[s[i]])satisfy++;
while(S[s[j]] > T[s[j]])S[s[j++]]--;
if(satisfy == total && (res.empty() || i - j + 1 < res.size()))
{
res = s.substr(j, i-j+1);
}
}
return res;
}
};