算法练习LeetCode初级算法之数组

  • 删除数组中的重复项

 官方解答:

       

  •  旋转数组

     

  •  存在重复元素

       

  •  只出现一次的数

   

   

官方解答:  同一个字符进行两次异或运算就会回到原来的值

       

   

  • 两个数组的交集 II

     

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

 

public class LeetCode {

    public static void main(String[] args) {

        Solution solution = new Solution();

        int a[] = {1,2,2 ,1,3};

        int n[]= {2,2,3};

        System.out.println(Arrays.toString(solution.intersect(a,n)));

        

    }

}

 

class Solution {

public int[] intersect(int[] nums1, int[] nums2) {

        List<Integer> list=new ArrayList<>();

        Arrays.sort(nums1);

        Arrays.sort(nums2);

        int n=nums1.length;

        int m=nums2.length;

        int j=0;

        int i=0;

        while (i<n&&j<m) {

            if (nums1[i]>nums2[j]) {

                j++;

            }else if (nums1[i]<nums2[j]) {

                i++;

            }else {

                list.add(nums1[i]);

                i++;

                j++;

            }

        }

        int sum[] = new int[list.size()];

        int p=0;

    for (int k : list) {

            sum[p]=k;

            p++;

        }

      

    return sum;

 

}

}

 

  • 加一

    这个题目要注意考虑[0]和[9]的情况,还有[9,9,9]的情况!!!

class Solution {

    public int[] plusOne(int[] digits) {

        for (int i = digits.length-1; i >= 0; i--) {

            if (digits[i]==9) {

                digits[i]=0;

            }else {

                digits[i]++;

                return digits;

            }

        }

        

        int[] sum=new int[digits.length+1];

        sum[0]=1;

        for (int i = 1; i < sum.length; i++) {

            sum[i]=digits[i-1];

        }

        return sum;

    }

}

  • 有效数独

我的解法:利用队列,容易理解。

import java.util.LinkedList;

import java.util.Queue;

class Solution {

public boolean isValidSudoku(char[][] board) {

        for (int i = 0; i < 9; i++) {

            Queue<Character> queue=new LinkedList<>();

            for (int j = 0; j < 9; j++) {

                if (Character.isDigit(board[i][j])) {

                    queue.offer(board[i][j]);

                }

            }

            while (!queue.isEmpty()) {

                char b=queue.poll();

                if (queue.contains(b)) {

                    return false;

                }

            }

        }

        for (int i = 0; i < 9; i++) {

            Queue<Character> queue=new LinkedList<>();

            for (int j = 0; j < 9; j++) {

                if (Character.isDigit(board[j][i])) {

                    queue.offer(board[j][i]);

                }

            }

            while (!queue.isEmpty()) {

                char b=queue.poll();

                if (queue.contains(b)) {

                    return false;

                }

            }

        }

        int k=0;

        while (k<9) {

            Queue<Character> queue=new LinkedList<>();

            for (int j = k; j < k+3; j++) {

                for (int j2 = 0; j2 < 3; j2++) {

                    if (Character.isDigit(board[j2][j])) {

                        queue.offer(board[j2][j]);

                    }

                }

            }

            while (!queue.isEmpty()) {

                char b=queue.poll();

                if (queue.contains(b)) {

                    return false;

                }

            }

            for (int j = k; j < k+3; j++) {

                for (int j2 = 3; j2 < 6; j2++) {

                    if (Character.isDigit(board[j2][j])) {

                        queue.offer(board[j2][j]);

                    }

                }

            }

            while (!queue.isEmpty()) {

                char b=queue.poll();

                if (queue.contains(b)) {

                    return false;

                }

            }

            for (int j = k; j < k+3; j++) {

                for (int j2 = 6; j2 < 9; j2++) {

                    if (Character.isDigit(board[j2][j])) {

                        queue.offer(board[j2][j]);

                    }

                }

            }

            while (!queue.isEmpty()) {

                char b=queue.poll();

                if (queue.contains(b)) {

                    return false;

                }

            }

            k+=3;

        }

    return true;

}

}

官方解答,利用了hashmap键值对,也好理解!

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;//此处将数独分配到0-93*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;

     }

    }

大神解答,过于难理解,现阶段不要求掌握,仅向大神学习!!!

画红色的部分为遍历3*3的方格内数字是否重复,首先分别将0、1、2和第1行和第2行的每个数比较

然后再用第1行的每个数分别于第2行比较,因为数自身所在的行在前面的for循环中已经比较过了,

所以无需再比较。

  • 旋转图像

解答:

class Solution {

public void rotate(int[][] matrix) {

int len=matrix.length;

for (int i = 0; i < len/2; i++) {//外层for循环看有多少层

int start=i;

int end=len-1-i;

for (int j = 0; j < end-start; j++) {//内层for循环看每层要几次循环替换

            int temp=matrix[start][start+j];

            matrix[start][start+j]=matrix[end-j][start];

            matrix[end-j][start]=matrix[end][end-j];

            matrix[end][end-j]=matrix[start+j][end];

            matrix[start+j][end]=temp;

        }

        }

 

}

}

代码看似简单,逻辑实则有些难度!!

外层循环要首先确定两个指针 头:start和尾: end

然后根据普通值来确定二维数组下标,不要根据特殊值,那样没法确定;

由于目前技术有限博客园客户端没有调好,导致格式看起来很难受!!!

 

posted @ 2019-01-23 17:07  GavinYGM  阅读(147)  评论(0编辑  收藏  举报