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

Length of Last Word

![](media/15162389890253/15162389958774.jpg)
class Solution {
public:
    int lengthOfLastWord(string s) {
        int len = s.size();
        int cout = 0;
        for (int i=len-1; i>=0; i--) {
            if(cout>0&&s[i]==' ') break;

            if (s[i]==' ') {
                continue;
            }else{
                cout++;
            }

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

 Longest Univalue Path

/**
 * 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:
    int dfs(TreeNode* node, int& lup) {
        int l = node->left ? dfs(node->left, lup) : 0;
        int r = node->right ? dfs(node->right, lup) : 0;
        int resl = node->left && node->left->val == node->val ? l + 1 : 0;
        int resr = node->right && node->right->val == node->val ? r + 1 : 0;
        lup = max(lup, resl + resr);
        return max(resl, resr);
    }

    int longestUnivaluePath(TreeNode* root) {
        int lup = 0;
        if (root) dfs(root, lup);
        return lup;
    }
};
2017/12/22 posted in  leetcode

Isomorphic Strings

一开始写的代码,超时版本,用的集合。

bool isIsomorphic(string s, string t) {
    unordered_map<char, set<int>> Smap;
    unordered_map<char, vector<int>> Tmap;
    for (int i = 0; i<s.size(); i++) {
        Smap[s[i]].insert(i);
        Tmap[t[i]].push_back(i);
    }
    if (Smap.size()!=Tmap.size()) {
        return 0;
    }else{
        for (int j = 0; j<s.size(); j++) {
            vector<int> a = Tmap[t[j]];
            set<int> b = Smap[s[j]];
            int len = b.size();
            for (auto num : a) {
                b.insert(num);
                if (len!=b.size()) {
                    return 0;
                }
            }
        }
    }
    
    return 1;
}

修改后的版本。

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int m1[256] = {0}, m2[256] = {0}, n = s.size();
        for (int i = 0; i < n; ++i) {
            if (m1[s[i]] != m2[t[i]]) return false;  //判断两个字符串i位置的字符上次出现的位置是否一样。
            m1[s[i]] = i + 1;   //记录上次该字符出现的位置
            m2[t[i]] = i + 1;   //记录上次该字符出现的位置
        }
        return true;
    }
};
2017/12/20 posted in  leetcode