ARTS-02

Algorithm

leetcode 1: Two Sum

    public int[] twoSum(int[] nums, int target) {
        if (nums == null || nums.length < 2) return new int[] {-1, -1};
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++) {
            int key = target - nums[i];
            if (map.containsKey(key)) return new int[] {map.get(key), i};
            else map.put(nums[i], i);
        }
        return new int[] {-1, -1};
    }

leetcode 15: 3Sum

    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> list = new ArrayList<>();
        for(int i = 0; i < nums.length - 2; i++) {
            if(i == 0 || nums[i] != nums[i-1]) {
                int lo = i + 1;
                int hi = nums.length - 1;
                int value = 0 - nums[i];
                while(lo < hi) {
                    if(nums[lo] + nums[hi] == value) {
                        list.add(Arrays.asList(nums[i], nums[lo], nums[hi]));
                        while(lo < hi && nums[lo] == nums[lo+1]) lo++;
                        while(lo < hi && nums[hi] == nums[hi-1]) hi--;
                        lo++;
                        hi--;
                    } else if(nums[lo] + nums[hi] < value) {
                        // while(lo < hi &&  nums[lo] == nums[lo+1]) lo++;
                        lo++;
                    } else {
                        // while(lo < hi && nums[hi] == nums[hi-1]) hi--;
                        hi--;
                    }
                }
            }
        }
        return list;
    }

Comment out the two loop is not necessary to add, not only does not improve the performance of the program, the program would give the judge more than perform some statements.

leetcode 16: 3Sum Closest

public int threeSumClosest(int[] nums, int target) {
    Arrays.sort(nums);
    int result = nums[0] + nums[1] + nums[nums.length - 1];
    for(int i = 0; i < nums.length - 2; i++) {
        int lo = i + 1;
        int hi = nums.length - 1;
        while(lo < hi) {
            int value = nums[i] + nums[lo] + nums[hi];
            if(value == target) return target;
            else if(value < target) lo++;
            else hi--;
            if(Math.abs(value - target) < Math.abs(result - target)) result = value;
        }
    }
    return result;
}

leetcode 18: 4Sum

This question and 3Sum very similar, so you can use the same ideas to answer this question. In order to facilitate the de-emphasis, let's order.

public List<List<Integer>> fourSum(int[] nums, int target) {
    Arrays.sort(nums);
    List<List<Integer>> list = new ArrayList<>();
    for(int i = 0; i < nums.length - 3; i++) {
        if(i > 0 && nums[i] == nums[i-1]) continue;
        for(int j = i+1; j < nums.length - 2; j++) {
            // if(nums[j] == nums[j-1]) continue;
            if((j - 1) != i && nums[j] == nums[j-1]) continue;
            int lo = j + 1;
            int hi = nums.length - 1;
            while(lo < hi) {
                int value = nums[i] + nums[j] + nums[lo] + nums[hi];
                if(value == target) {
                    list.add(Arrays.asList(nums[i], nums[j], nums[lo], nums[hi]));
                    while(lo < hi && nums[lo] == nums[lo+1]) lo++;
                    while(lo < hi && nums[hi] == nums[hi-1]) hi--;
                    lo++;
                    hi--;
                } else if(value < target) {
                    lo++;
                } else {
                    hi--;
                } 
            }
        }
    }
    return list;
}

Note the comment portion can not be used if(nums[j] == nums[j-1]) continue;for jfiltration. Because the first element and the second element can be the same.

When we're done brushing 3Sum and 4Sum , we will have a faint feeling, for any integer k (k> = 2), we can answer kSum. Indeed, for kSum, we can break down the problem into two sub-problems:

  1. Solving 2Sum
  2. The kSum reduced to (k-1) Sum
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        return kSum(nums, target, 4);
    }
    
    public List<List<Integer>> kSum(int[] nums, int target, int k) {
        Arrays.sort(nums);
        return kSum(nums, target, k, 0);
    }
    
    private List<List<Integer>> kSum(int[] nums, int target, int k, int fromIndex) {
        List<List<Integer>> list = new ArrayList<>();
        if(fromIndex + k > nums.length) return list;
        if(k == 2) {
            int lo = fromIndex;
            int hi = nums.length - 1;
            while(lo < hi) {
                int value = nums[lo] + nums[hi];
                if(value == target) {
                    List<Integer> t = new LinkedList<>();
                    t.add(nums[lo]);
                    t.add(nums[hi]);
                    list.add(t);
                    while(lo < hi && nums[lo] == nums[lo+1]) lo++;
                    while(lo < hi && nums[hi] == nums[hi-1]) hi--;
                    lo++;
                    hi--;
                } else if(value < target) {
                    lo++;
                } else {
                    hi--;
                }
            }
            return list;
        }
        
        for(int i = fromIndex; i <= nums.length - k; i++) {
            List<List<Integer>> temp = kSum(nums, target-nums[i], k-1, i+1);
            if(!temp.isEmpty()) {
                for(List<Integer> t : temp) {
                    t.add(0, nums[i]);
                }
                list.addAll(temp);
            }
            while(i <= nums.length - k && nums[i] == nums[i+1]) i++;
        }
        return list;
    }
}

Review

This week reading to be medium to an article, the article is titled "Great Developers Never Stop Learning" . In the article, the author discusses the 7 ways IT professionals to maintain continuous learning:

  1. The Art of Reading
  2. From Avid Reader to Avid Writer
  3. Listen Up
  4. Take Online Courses
  5. Practice Makes Perfect
  6. Tap Into Your Colleagues Network
  7. Socialise

Tip

MySQL frequently used show processlistcommands to troubleshoot the problem.

1567347324431

show processlistCheck that the information threads. Only the root user can see all the threads, other users can only see their own start thread, unless the user is given PROCESS privilege.

show processlistMySQL database query is information_schema system in processlist table. show processlistEquivalent to:

select * from information_schema.processlist;

Following is a brief meaning of each field:

The above mentioned id : identification thread, also information_schema.processlist table's primary key.

The User : the user starts this thread.

Host : record the ip address and port of the client.

db : is currently executing commands on which the database. If not use database, it is NULL.

The Command : command executing thread, and the corresponding State.

Time : How long has the current state.

State : state of the thread, is more complex.

Info : general record is currently executing a command. The default display only 100 characters.

Share

Entropy and the second law of thermodynamics is closely related to simple terms, entropy is to describe the degree of disorder of things, the greater the entropy, the higher the degree of disorder. We know the second law of thermodynamics describes an irreversible process, and this is a process of entropy increase.

So, in an isolated system, things are always moving in the direction of a more disorderly development, which is the principle of entropy increase. Therefore, the universe will eventually embark on a thorough disorder, we can call it the "eternal death."

Perhaps, is the meaning of human existence in the universe step by step toward complete disorder in the process of trying to keep themselves organized.

Guess you like

Origin www.cnblogs.com/thomas-hzp/p/11444262.html