Largest Number At Least Twice of Others

class Solution {
public:
    int dominantIndex(vector<int>& nums) {
        int max = 0, tmp = 0,index = 0;
        for (int i = 0; i<nums.size(); i++) {
            if(nums[i]>=max) {
                tmp = max;
                max = nums[i];
                index = i;
            }else if (nums[i]>tmp){
                tmp = nums[i];
            }
            if (i==nums.size()-1 && tmp*2<=max) return index;
        }
        return -1;
    }
};
2018/1/20 posted in  leetcode

Reverse Linked List

迭代法:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre = NULL;
        while (head) {
            ListNode* next = head -> next;
            head -> next = pre;
            pre = head;
            head = next;
        } 
        return pre;
    }
};

递归法:

class Solution {
public:   
    ListNode* reverseList(ListNode* head) {
        if (!head || !(head -> next)) return head;
        ListNode* node = reverseList(head -> next);
        head -> next -> next = head;
        head -> next = NULL;
        return node; 
    }
}; 
2018/1/19 posted in  leetcode

First Missing Positive

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        unordered_map<int, int> map;
        int min = INT_MAX, max = 0, res = 1;
        for (int i = 0; i<nums.size(); i++) {
            if (nums[i]<=min&&nums[i]>=0) {
                min = nums[i];
            }else if (nums[i]<0) min = 0;
            if (nums[i]>=max) max = nums[i];
            map[nums[i]]++;
        }
        for (int j=min; j<=max; j++) {
            if (j==0) continue;
            if (min>=2) {
                return 1;
            }
            if (map[j]==0) {
                res = j;
                break;
            }
            if (j==max) {
                res = max+1;
            }
        }
        return res;
    }
};
2018/1/18 posted in  leetcode

Find the Duplicate Number

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        unordered_map<int, int> map;
        int res = 0;
        for (int i = 0; i<nums.size(); i++) {
            if (map[nums[i]]==0) {
                map[nums[i]]++;
            }else{
                res = nums[i];
            }
        }
        return res;
    }
};
2018/1/18 posted in  leetcode

Set Mismatch

class Solution {
public:
    vector<int> findErrorNums(vector<int>& nums) {
        int len = nums.size();
        vector<int> res;
        vector<int> a(len+1);   //因为数组从0开始,所以除去0,在后面加上1位

        for (int i = 0; i<len; i++) {
            if (a[nums[i]]==0) {
                a[nums[i]]+=1;
            }else{
                res.push_back(nums[i]);
            }
        }

        for (int j = 1; j<=len; j++) {
            if (a[j]==0) {
                res.push_back(j);
            }
        }

        return res;
    }
};
2018/1/18 posted in  leetcode

Find Anagram Mappings

class Solution {
public:
    vector<int> anagramMappings(vector<int>& A, vector<int>& B) {
        unordered_map<int, int> map;
        vector<int> res;
        for (int i = 0; i<B.size(); i++) {
            map[B[i]]=i;
        }
        for (int j = 0; j<A.size(); j++) {
            res.push_back(map[A[j]]);
        }
        return res;
    }
};
2018/1/18 posted in  leetcode