讨论/《高频算法实战》 - 练习:求数组中每个数前一次出现的位置/
《高频算法实战》 - 练习:求数组中每个数前一次出现的位置
共 9 个回复
class Solution {
    public int[] find_left_repeat_numII(int[] nums) {
        int[] res = new int[nums.length];
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<nums.length;i++){
            res[i] = map.getOrDefault(nums[i], -1);
            map.put(nums[i],i);
        }
        return res;
    }
}
2

rust 解法 match

use std::collections::HashMap;

impl Solution {
    pub fn find_left_repeat_num_ii(nums: Vec<i32>) -> Vec<i32> {
        let mut ret: Vec<i32> = Vec::new();
        let mut hash: HashMap<i32,i32> = HashMap::new();
        for i in 0..nums.len() {
            match hash.get(&nums[i]) {
                Some(&x) => {
                    ret.push(x);
                },
                None => {
                    ret.push(-1);
                }
            }
            hash.insert(nums[i], i as i32);
        }
        ret
    }
}

class Solution {
public:
    vector<int> find_left_repeat_numII(vector<int>& nums) {
        unordered_map<int,int> hash;
        vector<int> vec;
        for(int i = 0;i<nums.size();i++){
            auto it = hash.find(nums[i]);
            if(it==hash.end())
                vec.push_back(-1);
            else
                vec.push_back(it->second);
            hash[nums[i]]=i;
        }
        return vec;
    }
};

image.png

vector<int> find_left_repeat_numII(vector<int>& nums) {
		int n = nums.size();
		vector<int> res(n);
		unordered_map<int, int> q;
		for (int i = 0; i < n; i++)
		{
			if (q.count(nums[i]) == 0)
			{
				q[nums[i]] = i;
				res[i] = -1;
			}
			else{
				res[i] = q[nums[i]];
				q[nums[i]] = i;
			}
		}
		return res;
	}
class Solution {
public:
    vector<int> find_left_repeat_numII(vector<int>& nums) {
        unordered_map<int, int> mp;
        vector<int> ret;
        for (int i = 0; i < nums.size(); i++) {
            if (!mp.count(nums[i])) {
                mp[nums[i]] = i;
                ret.emplace_back(-1);
            } else {
                ret.emplace_back(mp[nums[i]]);
                mp[nums[i]] = i;
            }
        }
        return ret;
    }
};
class Solution {
public:
    vector<int> find_left_repeat_numII(vector<int>& nums) {
        unordered_map<int,int> m;
        vector<int> ans(nums.size());
        for(int i = 0; i < nums.size(); ++i)
        {
            if(m.find(nums[i]) == m.end())
                ans[i] = -1;
            else
                ans[i] = m[nums[i]];
            m[nums[i]] = i;
        }
        return ans;
    }
};

4ms 8.2 MB

python

class Solution:
    def find_left_repeat_numII(self, nums: List[int]) -> List[int]:
        hash_set = {}
        res = []
        for idx in range(len(nums)):
            if nums[idx] not in hash_set:
                res.append(-1)
            else:
                res.append(hash_set[nums[idx]])
            hash_set[nums[idx]] = idx
        return res
class Solution {
    public int[] find_left_repeat_numII(int[] nums) {
         HashMap<Integer, Integer>map = new HashMap<>();
        int[] ans = new int[nums.length]; 
        for(int i = 0; i<nums.length; i++){
            if(!map.containsKey(nums[i])){
                map.put(nums[i], i);
                ans[i] = -1;
            }else{
                ans[i] = map.get(nums[i]);
                map.put(nums[i], i);
            }
        }
        return ans;
    }
}

class Solution {
public:
    vector<int> find_left_repeat_numII(vector<int>& nums) {
        vector<int> v(nums.size(), -1);
        bool flag = false;
        for (int i = nums.size() - 1; i > 0; i--) {
            for (int j =  i - 1; j >= 0; j--) {
                if (nums[i] == nums[j]) {
                    v[i] = j;
                    break;
                }
            }
        }
        return v;
    }
};