about Redis Key lock

Mon, Apr 10, 2023 5-minute read

介绍下Redis Key lock

乐观锁也不够用了的话,就考虑Redis Key lock。

线程A先在Redis里设置该商品的key(比如goods_1)为1,如果线程B也查到1,直到线程A释放掉这把锁,线程B才能再拿到这把锁。

可能多个线程同时进入到 if 如果为空或者为None那么代表获取到锁,于是同时获取到了锁,这就使得分布式锁失效了。

保证原子性

锁过期时间和续租

互斥性:任意时刻只能有一个客户端拥有锁,不能同时多个客户端获取 死锁:获取锁的客户端因为某些原因而宕机,而未能释放锁,其他客户端无法获取此锁,需要有机制来避免该类问题的发生 比如代码异常,导致无法运行到release 当前服务器网络出问题,比如该release时,突然访问不了Redis了 服务器断电

3. Two Sum

leet

<a id="markdown-bash" name="bash"></a>
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.
Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Two Sum II - Input Array Is Sorted 167

Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.

Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
<a id="markdown-return-the-indices-of-the-two-numbers%2C-index1-and-index2%2C-added-by-one-as-an-integer-array-%5Bindex1%2C-index2%5D-of-length-2." name="return-the-indices-of-the-two-numbers%2C-index1-and-index2%2C-added-by-one-as-an-integer-array-%5Bindex1%2C-index2%5D-of-length-2."></a>

The tests are generated such that there is exactly one solution. You may not use the same element twice.

Your solution must use only constant extra space.

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].

Container With Most Water 11

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
<a id="markdown-you-are-given-an-integer-array-height-of-length-n.-there-are-n-vertical-lines-drawn-such-that-the-two-endpoints-of-the-ith-line-are-i%2C-0-and-i%2C-height%5Bi%5D." name="you-are-given-an-integer-array-height-of-length-n.-there-are-n-vertical-lines-drawn-such-that-the-two-endpoints-of-the-ith-line-are-i%2C-0-and-i%2C-height%5Bi%5D."></a>

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

谁小移谁

无论是移动短板或者长板,我们都只关注移动后的新短板会不会变长,而每次移动的木板都只有三种情况,比原短板短,比原短板长,与原短板相等;如向内移动长板,对于新的木板:1.比原短板短,则新短板更短。2.与原短板相等或者比原短板长,则新短板不变。所以,向内移动长板,一定不能使新短板变长

15. 3Sum

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

 

Example 1:

Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]

对于重复元素:跳过,避免出现重复解 左指针 L=i+1L=i+1,右指针 R=n-1R=n−1,当 L<R 时,执行循环**

  • 当 nums[i]+nums[L]+nums[R]==0nums[i]+nums[L]+nums[R]==0,执行循环,判断左界和右界是否和下一位置重复,去除重复解。并同时将 L,RL,R 移到下一位置,寻找新的解
  • 和大于 00,说明 nums[R]nums[R] 太大,R 左移
  • 和小于 00,说明 nums[L]nums[L] 太小,L 右移

1. sort: -4 -1 -1 -1 0 1 2 k i j

sum = -3 < 0, i++

2. 去重: -4 -1 -1 -1 0 1 2 k i j sum = -2 < 0, i++

3. 移动: -4 -1 -1 -1 0 1 2 k i j sum = -1 < 0, i++

4. 第二轮:

-4 -1 -1 -1 0 1 2

. k i j

sum = 0 == 0, 加入 res , i++, j–

4

-4 -1 -1 -1 0 1 2

. k i j // 去重

sum = 0 == 0, 加入 res

5,去重,第三轮

-4 -1 -1 -1 0 1 2

. k i j // 去重

sum = 0 == 0, 加入 res

 public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        
        for (int i = 0; i <= nums.length - 3; i++) {
            // 不能大于 0
            if (nums[i] > 0) {
                return res;
            }
             // 去重  nums[i] == nums[i + 1] 就排斥-1, -1 , 2,或者 0, 0, 0
             if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int k = i + 1, j = nums.length - 1;
            while (k < j) {
                int sum = nums[i] + nums[k] + nums[j];
                if (sum > 0) {
                    j--;
                } else if (sum < 0) {
                    k++;
                } else {
                    res.add(Arrays.asList(nums[i], nums[k], nums[j]));
                    // 去重
                    while (k <a j && nums[j] == nums[j - 1]) j--;
<a id="markdown-while-k-%3C-j-%26%26-nums%5Bj%5D-%3D%3D-nums%5Bj---1%5D-j--%3B" name="while-k-%3C-j-%26%26-nums%5Bj%5D-%3D%3D-nums%5Bj---1%5D-j--%3B"></a>
                     while (k < j && nums[k] == nums[k + 1]) k++;
                    j--;
                    k++;
                }
            }
             
        }
        return res;
    }

3Sum Closest 16

Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.

Return the sum of the three integers.

You may assume that each input would have exactly one solution.

 

Example 1:

Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

 public int threeSumClosest(int[] nums, int target) {
        
       
        Arrays.sort(nums);
         int res = nums[0] + nums[1] + nums[nums.length - 1];
        for (int i = 0; i <= nums.length - 3; i++) {
            int k = i + 1, j = nums.length - 1;
            
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            while (k < j) {
                int sum = nums[i] +nums[k] +nums[j];
                if (Math.abs(sum - target) < Math.abs(res - target)) {
                    res = sum;
                }
                    
                 if (sum== target) {
                    
                  
                     return sum;
                } else if (sum > target) {
                    j--;
                } else {
                    k++;
                }
            }
        }
          return res;
           
    }

int res = nums[0] + nums[1] + nums[nums.length - 1], 刚开始我设置为 int res = Integer.MAX_VALUE

但是!!!! input 为负的时候,Math.abs(res - target))这里 res 因为是正的 max_value - (-1) 会变成 min_value, 这时候 res 就一直是 max_value, 不会变成 sum




来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/MPnaiL

感谢:她们都有题库在 github 上直接有,都是大神, 都富有详细解法,对新人特别适合了解解题思路