当前位置:网站首页>Leetcode algorithm (1)

Leetcode algorithm (1)

2020-11-09 12:58:00 I'm sorry.

This is a leetcode The simplest question !!!

This blogger recommends several simple algorithms to you , Simple to simple , But whether it's easy or not , All change is the same . The blogger will take you from easy to difficult , Update slowly , The accumulation of . If there is a better algorithm , I hope you can share .



Topic requirements 1:

Small A and Small B Playing guessing numbers . Small B Each time from 1, 2, 3 Choose one randomly , Small A Every time 1, 2, 3 Choose a guess . They played the game three times in all , Please return Small A I guessed it right several times ?

Input guess The array is Small A Every guess ,answer The array is Small B Every choice .guess and answer The length of is equal to 3.

Code implementation

public class Solution {
   
      
    public int game(int[] guess, int[] answer) {
   
      
        int count = 0;
        for (int i = 0; i < 3; i++) {
   
      
            if (guess[i] == answer[i]){
   
      
                count+=1;
            }
        }
        return count;
    }

    public static void main(String[] args) {
   
      
        Solution n2 = new Solution();
        System.out.println(n2.game(new int[]{
   
      1,2,3}, new int[]{
   
      1, 2, 2}));
    }
}

Topic requirements 2:

seek 1+2+...+n , It is required that multiplication and division shall not be used 、for、while、if、else、switch、case Wait for keywords and conditional statements (A?B:C).

Code implementation

public class Solution3 {
   
      
    public int sumNums(int n) {
   
      
        // Sum of equal difference sequence 
        return (1+n)*n/2;
    }

    public static void main(String[] args) {
   
      
        Solution3 q = new Solution3();
        System.out.println(q.sumNums(3));
    }
}

Topic requirements 3:

Here's a square matrix mat, Please return the sum of the diagonal elements of the matrix .

Please return the sum of the elements on the main diagonal and the sub diagonal of the matrix and not on the main diagonal .
 Insert picture description here
Output :1 + 5 + 9 + 3 + 7 = 25
Be careful , Elements mat[1][1] = 5 Will only be counted once .


Code implementation

public class Solution4 {
   
      

    int diagonal= 0;
    public int diagonalSum(int[][] mat) {
   
      
        for (int i=0;i<mat.length;i++){
   
      
            for (int j = 0; j < mat[i].length; j++) {
   
      
                if ( i==j || i+j == mat.length-1){
   
      
                    diagonal += mat[i][j];
                }
            }
        }
        return diagonal;
    }

    public static void main(String[] args) {
   
      
        Solution4 s = new Solution4();
        System.out.println(s.diagonalSum(new int[][]{
   
      {
   
      1,2,3},{
   
      4,5,6},{
   
      7,8,9}}));
    }
}

Topic requirements 4:

It's on the table n Money is deducted from the pile , The number of each heap is stored in the array coins in . We can choose any pile at a time , Take one or two of them , Ask for the minimum number of times to deduct money from all efforts .

Code implementation before simplification

public class Solution5 {
   
      

    public int minCount(int[] coins) {
   
      

        int count1 = 0;
        int count2 = 0;

        for (int i = 0; i < coins.length; i++) {
   
      

            if (coins[i] % 2 ==0){
   
      
                count1 += coins[i]/2;
            }
            if (coins[i] % 2 !=0){
   
      
                count2 += coins[i]/2+1;
            }
        }
        return count1+count2;
    }

    public static void main(String[] args) {
   
      
        Solution5 s = new Solution5();
        System.out.println(s.minCount(new int[]{
   
      1,1,1}));
    }
}

Simplified code implementation

public class Solution5 {
   
      

   public int minCount1(int[] coins) {
   
      
        int c = 0;
        for (int i = 0; i < coins.length; i++) {
   
      
            c += coins[i] / 2;
            c += coins[i] % 2;
        }
        return c;
    }
    
     public static void main(String[] args) {
   
      
        Solution5 s = new Solution5();
        System.out.println(s.minCount1(new int[]{
   
      2,5,7}));
    }

版权声明
本文为[I'm sorry.]所创,转载请带上原文链接,感谢