Starting today, brush the algorithm question -2021.12.05

Posted by BleedingSky on Mon, 06 Dec 2021 01:01:22 +0100

Sword finger Offer 04. Search in two-dimensional array

https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/
The excerpt is as follows( https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/solution/mian-shi-ti-04-er-wei-shu-zu-zhong-de-cha-zhao-b-3/):
Method 2: linear search
Because a given two-dimensional array has the characteristics of increasing each row from left to right and each column from top to bottom, when an element is accessed, some elements in the array can be excluded.
Search from the upper right corner of the two-dimensional array. If the current element is equal to the target value, return true. If the current element is greater than the target value, move to the left column. If the current element is less than the target value, move to the next row.
It can be proved that this method will not miss the target value. If the current element is greater than the target value, it means that all the elements below the current element must be greater than the target value, so it is impossible to find the target value by looking down, and it is possible to find the target value by looking left. If the current element is less than the target value, it means that all the elements on the left of the current element must be less than the target value, so The left search cannot find the target value, and the down search may find the target value.

  • The code is as follows:
  class Solution {
      public boolean findNumberIn2DArray(int[][] matrix, int target) {
          if(matrix==null||matrix.length==0||matrix[0].length==0){
              return false;
          }
          int rows = matrix.length;
          int row =0,column = matrix[0].length-1;
          while(row<rows&&column>=0){
              int nums = matrix[row][column];
              if(nums==target){
                  return true;
              }else if(nums>target){
                  column--;
              }else{
                  row++;
              }
          }
          return false;
      }
  }

Sword finger Offer 11. Rotate the minimum number of the array

https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/

  1. Binary search
  • The code is as follows:
  class Solution {
      public int minArray(int[] numbers) {
          int low =0,hight=numbers.length-1;
          while(low<=hight){
              int mid = low + (hight-low)/2;
              if(numbers[mid]<numbers[hight]){
                  hight = mid;
              }else if(numbers[mid]>numbers[hight]){
                  low = mid + 1;
              }else {
                  hight -=1;
              }
          }
          return numbers[low];
      }
  }

Sword finger Offer 50. The first character that appears only once

https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/
1. There are two main types of ideas: the frequency of additional spatial records, such as HashMap (the first one needs to be found in a loop), LinkHashMap is orderly and returns the first element
2. Use 26 letters to cycle through the storage calculation

  • The code is as follows:
  class Solution {
      public char firstUniqChar(String s) {
          int[] count=new int[26];
          char[] chars=s.toCharArray();
          //count
          for (char ch:chars) 
              count[ch-'a']++;
          //Traversal search
          for (char ch:chars) 
              if(count[ch-'a']==1) return ch;
          return ' ';
  }

Traverse whether 26 letters exist in the string array; whether the first index and the last index are consistent; and whether the current index value is the smallest.

  • The code is as follows:
  public char firstUniqChar(String s) {
      char res = ' ';
      int min = Integer.MAX_VALUE;
      for (char c = 'a'; c <= 'z'; c++) {
          int index = s.indexOf(c);
          if (index != -1 && index == s.lastIndexOf(c) && index < min) {
              min = index;
              res = c;
          }
      }
      return res;
  }