Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
"""
9ms, beats 55.59%
time O(n), space O(1)
"""
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head)
return head;
int temp = head->val;
ListNode *current = head;
while(current->next){
if(current->next->val == temp){
//delete current.next;
current->next = current->next->next;
}
else{
temp = current->next->val;
current = current->next;
}
}
return head;
}
};
Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
"""
3ms, beats 37.56%
time O(m+n) space 1
还可以从不同方面进行改进,但是思路终归比较单纯
"""
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
if(n>0){
if(m>0){
int temp = 0;
int len = m;
for(int i=0; i<n; i++){
for (int j=temp; j<m+n; j++){
if(nums2[i] < nums1[j]){
nums1.insert(nums1.begin()+j, nums2[i]);
temp = j+1;
len ++;
break;
}
else if(j == len-1){
nums1.insert(nums1.begin()+j+1, nums2[i]);
temp = j+1;
len ++;
break;
}
}
}
nums1.erase(nums1.begin()+m+n, nums1.end());
}
else
nums1 = nums2;
}
}
};
"""
精炼。6 ms,看来不是写的越短跑的越快
"""
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - 1, j = n - 1, tar = m + n - 1;
while (j >= 0) {
nums1[tar--] = i >= 0 && nums1[i] > nums2[j] ? nums1[i--] : nums2[j--];
}
}
};
Same Tree
Given two binary trees, write a function to check if they are the same or not.
"""
BFS和DFS肯定是可以。我们用分治法。递归,3ms, beats 1.35%
time n (T(n)=2*T(n/2)+O(1));space n (recursive calls on the stack)
"""
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if (p == NULL && q == NULL)
return true;
if (p != NULL && q != NULL){
if (p->val != q->val)
return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
return false;
}
};
"""精炼, 3 ms
"""
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
return p == NULL || q == NULL ? p == q : p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
"""recursively,与上面类似。6 ms, beats 12.12%。
time n space n
"""
/**
* 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:
bool isMirror(TreeNode* left, TreeNode* right){
return !left||!right ? left == right: left->val == right->val && isMirror(left->left, right->right) && isMirror(left->right, right->left);
}
bool isSymmetric(TreeNode* root) {
if(root == NULL)
return true;
return isMirror(root->left, root->right);
}
};
"""iteratively。用队列。
"""
Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
"""显然DFS或BFS可以。9 ms, beats 1.76%
time O(n), space O(n)
"""
class Solution {
public:
int maxDepth(TreeNode* root) {
return root == NULL ? 0 : max(maxDepth(root -> left), maxDepth(root -> right)) + 1;
}
};
插播几道medium面试题。
Maximum Product Subarray
"""类似之前的最大和,其实不难,但是难以一遍通过,主要是考虑不够周全。
3 ms, beats 42.53%
time O(n)
"""
class Solution {
public:
int maxProduct(vector<int>& nums) {
int max_so_far = INT_MIN;
int max_end_here = 1;
int min_end_here = 1;
for (int i=0; i<nums.size(); i++){
int temp = max_end_here * nums[i];
max_end_here = max(max(temp, nums[i]), min_end_here * nums[i]);
max_so_far = max(max_so_far, max_end_here);
min_end_here = min(min(min_end_here * nums[i], nums[i]), temp);
}
return max_so_far;
}
};