Leetcode LeetCode algorithm punch card learning day 5

Seven, plus one

Of a given integer composed of non-null non-negative integer array represented on the basis of the number plus one.
The highest digit is stored in the first position of the array, and each element in the array only stores a single number.
You can assume that in addition to the integer 0 , this integer does not start with a zero.
Insert picture description here
Method: My algorithm. Since I didn't see the official answer, I will only share my code here.

class Solution {
    
    
    public int[] plusOne(int[] digits) {
    
    
        int length=digits.length;
        //最后一位加一
        digits[length-1]++;
        for(int i=length-1;i>0;i--){
    
    
                digits[i-1]=digits[i-1]+digits[i]/10;
                digits[i]=digits[i]%10;
        }
        //单独看最高位,如果最高位>9了,那么就得多一位
        if(digits[0]/10 >0){
    
    
            int[] willback=new int[length+1];
            willback[0]=digits[0]/10;
            willback[1]=digits[0]%10;
            for(int i=2;i<length+1;i++){
    
    
                willback[i]=digits[i-1];
            }
            return willback;
        }
        else{
    
    
            return digits;
        }
       
    }
}

Eight, move zero

Given an array nums , write a function to move all zeros to the end of the array while maintaining the relative order of non-zero elements.
Insert picture description here
Method 1: My code, but it exceeds the time limit.

class Solution {
    
    
    public void moveZeroes(int[] nums) {
    
    
        int index=nums.length-1;
        for(int i=nums.length-1;i>=0;i--){
    
    
            if(nums[i]==0){
    
    
                System.out.println(i);
                for(int j=i;j<index;j++){
    
    
                    nums[j]=nums[j+1];
                    nums[j+1]=0;
                    System.out.println(j);
                }
                index--;
            }
        }

    }
}

Method two: double pointer

class Solution {
    
    
    public void moveZeroes(int[] nums) {
    
    
        //左指针左边全是非零,左指针与右指针的之间全为0
        int length=nums.length;
        int index1=0;
        int index2=0;
        while(index2<length){
    
    
            if(nums[index2]!=0){
    
    
                int temp=nums[index2];
                nums[index2]=nums[index1];
                nums[index1]=temp;
                index1++;
            }
            index2++;
        }
    }
}

Method 3: Look at other people’s in the comment area

class Solution {
    
    
    public void moveZeroes(int[] nums) {
    
    
        int index=0;
        for(int i=0;i<nums.length;i++){
    
    
            if(nums[i]!=0){
    
    
                nums[index]=nums[i];
                index++;
            }
        }
        for(int j=index;j<nums.length;j++){
    
    
            nums[j]=0;
        }
    }
}

Nine: the sum of two numbers

Given an integer array nums and an integer target value target, please find the two integers whose sum is the target value in the array and return their array subscripts.
You can assume that each input will only correspond to one answer. However, the same element in the array cannot be used twice.
You can return the answers in any order.
Insert picture description here
Method 1: Mine, violent enumeration, but there are still two fors, which doesn’t feel good

class Solution {
    
    
    public int[] twoSum(int[] nums, int target) {
    
    
        boolean back=false;
        int[] willback=new int[2];
        for(int i=0;i<nums.length-1;i++){
    
    
            for(int j=i+1;j<nums.length;j++){
    
    
                if(nums[i]+nums[j]==target){
    
    
                    willback[0]=i;
                    willback[1]=j;
                    back=true;
                    break;
            }
            }
            if(back){
    
    
                break;
            }
        }
        return willback;
    }
}

Method 2: The official hash table algorithm

class Solution {
    
    
    public int[] twoSum(int[] nums, int target) {
    
    
        Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; ++i) {
    
    
            if (hashtable.containsKey(target - nums[i])) {
    
    
                return new int[]{
    
    hashtable.get(target - nums[i]), i};
            }
            hashtable.put(nums[i], i);
        }
        return new int[0];
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/two-sum/solution/liang-shu-zhi-he-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

10. Effective Sudoku

Determine whether a 9x9 Sudoku is valid. It is only necessary to verify whether the entered number is valid according to the following rules.

The numbers 1-9 can only appear once in each line.
The numbers 1-9 can only appear once in each column.
The numbers 1-9 can only appear once in each 3x3 palace separated by a thick solid line.
Insert picture description here
Method 1: I use a hash table to count the number of occurrences of one row, one column, and nine square grids

class Solution {
    
    
    public boolean isValidSudoku(char[][] board) {
    
    
        //先判断直线的
        for(int i=0;i<9;i++){
    
    
            Map<String,Integer> map=new HashMap<String,Integer>();//横着
            Map<String,Integer> map1=new HashMap<String,Integer>();//竖着
            for(int j=0;j<9;j++){
    
    
                Map<String,Integer> map2=new HashMap<String,Integer>();//横着
                if(board[i][j] !='.'){
    
    
                    int count = map.getOrDefault(board[i][j]+"", 0) + 1;
                    map.put(board[i][j]+"", count);
                    if(count>1){
    
    
                        return false;
                    }
                }
                if(board[j][i]!='.'){
    
    
                    int count1 = map1.getOrDefault(board[j][i]+"", 0) + 1;
                    map1.put(board[j][i]+"", count1);
                    if(count1>1){
    
    
                        return false;
                    }
                }
                //九宫格
                if(i%3==0 && j%3==0){
    
    
                    for(int m=0;m<=2;m++){
    
    
                        for(int k=0;k<=2;k++){
    
    
                            if(board[i+m][j+k]!='.'){
    
    
                                int count = map2.getOrDefault(board[i+m][j+k]+"", 0) + 1;
                                System.out.println(count);
                                map2.put(board[i+m][j+k]+"", count);
                                if(count>1){
    
    
                                    return false;
                                }
                    
                            }
                        }
                        
                    }
                    

                }
            }

        }
        return true;

    }
}

Second, the official

class Solution {
    
    
  public boolean isValidSudoku(char[][] board) {
    
    
    // init data
    HashMap<Integer, Integer> [] rows = new HashMap[9];
    HashMap<Integer, Integer> [] columns = new HashMap[9];
    HashMap<Integer, Integer> [] boxes = new HashMap[9];
    for (int i = 0; i < 9; i++) {
    
    
      rows[i] = new HashMap<Integer, Integer>();
      columns[i] = new HashMap<Integer, Integer>();
      boxes[i] = new HashMap<Integer, Integer>();
    }

    // validate a board
    for (int i = 0; i < 9; i++) {
    
    
      for (int j = 0; j < 9; j++) {
    
    
        char num = board[i][j];
        if (num != '.') {
    
    
          int n = (int)num;
          int box_index = (i / 3 ) * 3 + j / 3;

          // keep the current cell value
          rows[i].put(n, rows[i].getOrDefault(n, 0) + 1);
          columns[j].put(n, columns[j].getOrDefault(n, 0) + 1);
          boxes[box_index].put(n, boxes[box_index].getOrDefault(n, 0) + 1);

          // check if this value has been already seen before
          if (rows[i].get(n) > 1 || columns[j].get(n) > 1 || boxes[box_index].get(n) > 1)
            return false;
        }
      }
    }

    return true;
  }
}

作者:LeetCode
链接:https://leetcode-cn.com/problems/valid-sudoku/solution/you-xiao-de-shu-du-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

11. Given an n × n two-dimensional matrix to represent an image

Rotate the image 90 degrees clockwise.
Note:
You must rotate the image in place, which means you need to directly modify the input two-dimensional matrix. Please do not use another matrix to rotate the image.

Method 1: My method, I really thought about the law for a long time.

"00>03 03>33 33>30 30>00";
“01>13 13>32 32>20 20>01”;
“02>23 23>31 31>10 10>02”;
“11>12 12>22 22>21 21>11”;

This is such a law.

class Solution {
    
    
    public void rotate(int[][] matrix) {
    
    
        int length1=matrix[0].length;
        int alltime=length1/2;
        if(length1%2 !=0){
    
    
            alltime++;
        }
        int left=0;
        int right=0;
        int left1=0;
        int right1=0;
        int bianyuan=length1;
        int time=length1*length1/4;
        for(int xx=0;xx<alltime;xx++){
    
    
            bianyuan=length1-xx*2;
            System.out.println(bianyuan);
            for(int i=xx;i<bianyuan-1+xx;i++){
    
    
            left=xx;
            right=i;
            int temp=matrix[xx][i];
            for(int j=0;j<4;j++){
    
    
                System.out.println(temp);
                left1=right;
                right1=length1-left-1;
                int temp1=matrix[left1][right1];
                matrix[left1][right1]=temp;
                temp=temp1;
                left=left1;
                right=right1;  
            }
        }
        }
    }
}

Guess you like

Origin blog.csdn.net/Shadownow/article/details/113325829