文章目录
-
- Google面试题一
-
- LeetCode 14. Longest Common Prefix
- LeetCode 20. Valid Parentheses
- LeetCode 43. Multiply Strings
- LeetCode 48. Rotate Image
- LeetCode 31. Next Permutation
- LeetCode 23. Merge k Sorted Lists
- LeetCode 33. Search in Rotated Sorted Array
- LeetCode 11. Container With Most Water
- LeetCode 41. First Missing Positive
- LeetCode 25. Reverse Nodes in k-Group
Google面试题一
LeetCode 14. Longest Common Prefix
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.size()==0)return "";
int minlenth = strs[0].size();
for(int i = 1; i < strs.size(); i++)
{
minlenth = min(minlenth, int(strs[i].size()));
}
for(int j = 1; j <= minlenth; j++)
{
char s = strs[0][j-1];
for(int k = 1; k < strs.size(); k++)
{
if(s != strs[k][j-1])
return strs[0].substr(0, j-1);
}
}
return strs[0].substr(0, minlenth);
}
};
LeetCode 20. Valid Parentheses
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for(auto c : s)
{
if(c == ')')
{
if(stk.empty() || stk.top() != '(')return false;
stk.pop();
}
else if(c ==']')
{
if(stk.empty() || stk.top() != '[')return false;
stk.pop();
}
else if(c == '}')
{
if(stk.empty() || stk.top() != '{')return false;
stk.pop();
}
else stk.push(c);
}
return stk.empty();
}
};
LeetCode 43. Multiply Strings
class Solution {
public:
string multiply(string num1, string num2) {
vector<int> product(num1.size() + num2.size(), 0);
for(int i = 0; i < num1.size(); i++)
for(int j = 0; j < num2.size(); j++)
product[num1.size() - i + num2.size() - j - 2] += (num1[i] - '0')*(num2[j] - '0');
int t = 0;
for(int i = 0; i < product.size(); i++)
{
int &x = product[i];
t += x;
x = t % 10;
t /= 10;
}
string res;
int k = product.size() - 1;
while(!product[k] && k > 0)k--;
for(int i = k; i >= 0; i--)res += to_string(product[i]);
return res;
}
};
LeetCode 48. Rotate Image
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
for(int i = 0; i < matrix.size(); i++)
for(int j = 0; j < i; j++)
swap(matrix[i][j], matrix[j][i]);
for(int i = 0; i < matrix.size(); i++)
for(int j = 0, k = matrix[i].size() - 1; j < k; j++, k--)
swap(matrix[i][j], matrix[i][k]);
}
};
LeetCode 31. Next Permutation
class Solution {
public:
void nextPermutation(vector<int>& nums) {
for(int i = nums.size() - 1; i > 0; i--)
if(nums[i-1] < nums[i])
{
int j = i;
while(j + 1 < nums.size() && nums[j + 1] > nums[i-1])j++;
swap(nums[i-1], nums[j]);
reverse(nums.begin() + i, nums.end());
return;
}
reverse(nums.begin(), nums.end());
}
};
LeetCode 23. Merge k Sorted Lists
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<pair<int, ListNode*>> heap;
for(auto list : lists)
{
if(list)heap.push({
-list->val, list});
}
ListNode * dummy = new ListNode(-1);
auto cur = dummy;
while(heap.size())
{
auto t = heap.top();
heap.pop();
if(t.second->next)heap.push({
-t.second->next->val, t.second->next});
cur->next = t.second;
cur = cur->next;
}
cur->next = NULL;
return dummy->next;
}
};
LeetCode 33. Search in Rotated Sorted Array
class Solution {
public:
int search(vector<int>& nums, int target) {
if(nums.size() < 5)
{
for(int i = 0; i < nums.size(); i++)
if(nums[i] == target)
return i;
return -1;
}
int left = 0, right = nums.size() - 1;
if(nums[0] > nums.back())
{
int l = 0, r = nums.size() - 1;
while(l < r)
{
int mid = l + r + 1 >> 1;
if(nums[mid] >= nums[0])l = mid;
else r = mid - 1;
}
if(target >= nums[0])left = 0, right = l;
else left = l + 1, right = nums.size() - 1;
}
while(left < right)
{
int mid = left + right >> 1;
if(nums[mid] < target)left = mid + 1;
else right = mid;
}
if(nums[left] == target)return left;
return -1;
}
};
LeetCode 11. Container With Most Water
class Solution {
public:
int maxArea(vector<int>& height) {
int res = 0;
for(int l = 0, r = height.size() - 1; l < r;)
{
int left = height[l], right = height[r];
res = max(res, (r-l)*min(left, right));
if(left < right)l++;
else r--;
}
return res;
}
};
LeetCode 41. First Missing Positive
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
for(int i = 0; i < nums.size(); i++)
while(nums[i] >= 1 && nums[i] <= nums.size() && nums[nums[i]-1] != nums[i])
swap(nums[i], nums[nums[i] - 1]);
for(int i = 0; i < nums.size(); i++)
if(nums[i] != i + 1)
return i + 1;
return nums.size() + 1;
}
};
LeetCode 25. Reverse Nodes in k-Group
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode * dummy = new ListNode(-1);
dummy->next = head;
auto cur = dummy;
while(cur)
{
int s = 0;
for(auto i = cur->next; i;i = i->next)s++;
if(s < k)break;
s = 0;
auto a = cur->next, b = a->next;
while(s < k - 1)
{
s++;
auto c = b->next;
b->next = a;
a = b, b = c;
}
auto p = cur->next;
cur->next->next = b;
cur->next = a;
cur = p;
}
return dummy->next;
}
};