(Problem) "Algorithms Zero Base 100 Lecture" (Lecture 31) Multidimensional Enumeration - Introduction

Keywords: Java Algorithm leetcode

1. Judgment Subsequence ( leetcode392)

The idea is that we can use violence directly for pattern matching, but if we use violence directly in addition to the kmp algorithm, we do not need to use backtracking, because it is not a continuous substring, but a subsequence, as long as we find the s characters in t and the order is the same, we can AC s.

class Solution {
    public boolean isSubsequence(String s, String t) {
       int i = 0 , j = 0;
        while(i < t.length() && j < s.length())
        {
            if(t.charAt( i ) == s.charAt(j)) j ++;
            i ++;
        }
        return j == s.length();
    }
}

2. Search for a two-dimensional matrix ( leetcode240)

The idea of this question is to start with recursion, that is, if the target target is larger than the current matrix value, search right and bottom recursively, if it is smaller than the current matrix value, search left and right recursively, and set up an array of sentinels to see if it has been traversed. But this will be wrong, and after reading the solution, change your mind.
A good way to do this is to split the search and Z-search:
Binary Search: Each row of the matrix is searched by a corresponding binary search, but I find one problem is that if only a single binary search is performed, an error will occur, such as the following binary search:

public   boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length ,n = matrix[0].length;
        for(int i = 0 ; i < m ; i ++)
        {
            int l = 0,r = n - 1 ;
            if(l == r)
            {
                if(matrix[i][l] == target)return true;
                else continue;
            }
            while(l < r)
            {
                int mid = (l + r) / 2;
                if(target == matrix[i][mid])return true;
                else if(target <= matrix[i][mid])r = mid;
                else l = mid + 1;
            }
        }
        return false;
    }

(Binary search here for arraysIn this case, only -1 matches instead of 3, which results in an error, so a binary search should be performed in both cases. It is also important to note that if there is only one element in the matched array, the binary search condition l < R will not be searched, so a condition must be added before it is judged.)

Correct writing:
    public   boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length ,n = matrix[0].length;
        for(int i = 0 ; i < m ; i ++)
        {
            int l = 0,r = n - 1 ;
            if(l == r)
            {
                if(matrix[i][l] == target)return true;
                else continue;
            }
            while(l < r)
            {
                int mid = (l + r) / 2;
                if(target == matrix[i][mid])return true;
                else if(target <= matrix[i][mid])r = mid;
                else l = mid + 1;
            }
            l = 0;
            r = n - 1 ;
            while(l < r)
            {
                int mid = (l + r + 1) / 2;
                if(target == matrix[i][mid])return true;
                else if(target >= matrix[i][mid])l = mid;
                else r = mid - 1;
            }
        }
        return false;
    }

Z Search
We can search from the upper right corner of the matrix (0, n-1). For example, if we are currently in (x,y), then the matrix to be searched is a sub-matrix bounded by (x,y) and the lower left corner of the original matrix.
If matrix[x,y] == target, the result is complete;
If matrix [x, y] < target, the number to the left of the current row where x is located is less than the target, so x + 1 can be matched to the next row.
If matrix [x, y] > target, the number below the current column where y is located is greater than the target, so Y-1 can be detected in the previous column.

    public   boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length,n = matrix[0].length;
        int i = 0,j = n - 1;
        while( i < m && j >= 0 )
        {
            if(matrix[i][j] == target)return true;
            else if(matrix[i][j] > target)j--;
            else i ++;
        }
        return false;
    }

3. The absolute value of the difference is the number of pairs of K ( leetcode2006)

This is relatively easy, and a double for loop traverses the array to make a judgment.

       public   int countKDifference(int[] nums, int k) {
        int n = nums.length,amount = 0;
        for(int i = 0 ; i < n ; i++)
        {
            for(int j = i + 1; j < n ; j ++)
            {
                if(Math.abs(nums[i] - nums[j]) == k )amount ++;
            }
        }
        return amount;
    }

4. Find a difference ( leetcode389)

The idea at the beginning of this problem was not to count the number of characters in a string, but to find out the characters with different numbers of characters in t and s, so HashMap was thought of to solve it.

    public static char findTheDifference(String s, String t) {
        Map<Character,Integer> map = new HashMap<>();
        Map<Character,Integer> map1 = new HashMap<>();
        //for Loop Storage s Characters and Number
        for(int i = 0 ; i < s.length() ; i ++)
        {
            if(!map.containsKey(s.charAt(i)))
            {
                map.put(s.charAt(i),0);
            }
            else
            {
                int num = map.get(s.charAt(i));
                map.put(s.charAt(i),++num);
            }
        }
        //for loop stores t's characters and number
        for(int i = 0 ; i < t.length() ; i ++)
        {
            if(!map1.containsKey(t.charAt(i)))
            {
                map1.put(t.charAt(i),0);
            }
            else
            {
                int num = map1.get(t.charAt(i));
                map1.put(t.charAt(i),++num);
            }
        }
        //Judges the number of characters in s and t and returns the character that differs
        for(Character c : map1.keySet())
        {
            if(!map1.containsKey(c))return c;
            else
            {
                if(map.get(c) != map1.get(c))return c;
            }
        }
        return 'a';
    }

In fact, a better way is to add the ASCII codes of the s and t strings and subtract them, because only one character is added, so the characters transferred from the subtracted ASCII codes are added characters, which are more flexible.

public char findTheDifference(String s, String t) {
        int amount= 0;
        for (int i = 0; i < s.length(); ++i) {
            amount^= s.charAt(i);
        }
        for (int i = 0; i < t.length(); ++i) {
            amount^= t.charAt(i);
        }
        return (char) amount;
    }

5. Children who have the most polysaccharide fruits ( leetcode1431)

Equivalent to a water topic, first create a List array of boolean values, then for loop to find the maximum value in the array, and then iterate through, adding each value of the array with an additional number of candy, if it is greater than the maximum value, add true to the List array, otherwise add false to the List array.

    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        List<Boolean> li = new ArrayList<>();
        int max = candies[0];
        for(int i = 0 ; i < candies.length ; i++)
        {
            if(candies[i] > max)max = candies[i];
        }
        for(int i = 0 ; i < candies.length ; i++)
        {
            if((candies[i] + extraCandies) >= max)li.add(true);
            else li.add(false);
        }
        return li;
    }

6. The sum of all odd length subarrays ( leetcode1588)

You can use the idea of prefix sum, and each iteration changes the iteration condition by an odd number of times.

    public int sumOddLengthSubarrays(int[] arr) {
        int []a = new int[arr.length + 1];
        a[0] = 0;
        int amount = 0;
        for(int i = 1 ; i < a.length ; i ++)
        {
            a[i] = a[i - 1] + arr[i - 1];
        }
        for(int i = 1 ; i <= arr.length ; i +=2)
        {
            for(int j = i ; j <= arr.length ; j ++)
            {
                amount += a[j] - a[j - i];
                
            }
        }
        return amount;
    }

7. Statistics of the tuples ( leetcode1534)

Triple for loop traversal

    public int countGoodTriplets(int[] arr, int a, int b, int c) {
        int amount = 0;
        int n = arr.length;
        for(int i = 0 ; i < n - 2 ; i ++ )
        {
            for(int j = i + 1; j < n - 1 ; j ++)
            {
                for(int k = j + 1; k < n ; k ++)
                {
                    if(Math.abs(arr[i] - arr[j]) <=a && Math.abs(arr[j] - arr[k]) <=b && Math.abs(arr[i] - arr[k]) <=c)amount ++;
                }
            }
        }
        return amount;
    }

8. Gems and stones ( leetcode771)

Use HashSet to store gem strings and then traverse statistics.

    public int numJewelsInStones(String jewels, String stones) {
        int amount = 0;
        Set<Character> set = new HashSet<>();
        for(int i = 0 ; i < jewels.length() ; i ++)
        {
            set.add(jewels.charAt(i));
        }
        for(int i = 0 ; i < stones.length() ; i ++)
        {
            char c = stones.charAt(i);
            if(set.contains(c))amount++;
        }
        return amount;
    }

9. Create the target array in the order specified ( leetcode1389)

Water topic, a cycle to traverse.

    public int[] createTargetArray(int[] nums, int[] index) {
        List<Integer> li = new ArrayList<>();
        for(int i = 0 ; i < nums.length ; i ++)
        {
            li.add(index[i],nums[i]);
        }
        int []num = new int[li.size()];
        for(int i = 0 ; i< num.length ; i ++)num[i] = li.get(i);
        return num;
    }

10. Longest common prefix ( leetcode14)

Previously written in C++, Violent Traversal Statistics allows you to match one of these strings as a pattern string and then to match to the smallest prefix as a result.

#include<string>
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
	
        if(strs.size()==1)return strs[0];
        int max_len=0,j;
        for(int i=1;i<strs.size();i++)
            {
            for(j=0;j<strs[i].size();j++)
                {
                if(strs[0][j]!=strs[i][j])break;
            }
            	if(i==1&&j!=0)max_len=j;
                else max_len=min(max_len,j);
        }
        string str;
        for(int i=0;i<max_len;i++)
            str+=strs[0][i];
        return str; 
    }
};

11. Statistical Square and Number of Triples

Similar to the previous question, except that the condition of the for loop is changed to n, and then generally only the values of a and B need to be exchanged, but the result of exchanging the values of a and B is the same. The value of c must be greater than the values of a and b, so once traversed, the result is directly + 2 as long as it meets

    public int countTriples(int n) {
                int amount = 0;
        for(int i = 1 ; i <= n - 2 ; i ++ )
        {
            for(int j = i + 1; j <= n - 1 ; j ++)
            {
                for(int k = j + 1; k <= n ; k ++)
                {
                    if( (i*i + j * j) == k * k)amount+=2;
                }
            }
        }
        return amount;
    }

For the first time I blogged, I am writing poorly. Please forgive me and offer my advice.

Posted by g.grillo on Sat, 20 Nov 2021 13:59:36 -0800