其他分享
首页 > 其他分享> > 1月27日报

1月27日报

作者:互联网

MySQL

10 创建和管理表

DDL:Data Definition Languages (数据定义语言)

Alter
将表的一个列排到另一个表的后面:After

ALTER TABLE employees MODIFY mobile VARCHAR(25) AFTER officeCode;

字段改名: change

ALTER TABLE dept80
CHANGE department_name dept_name varchar(15);

删除一个列

ALTER TABLE 表名 DROP 【COLUMN】字段名

13 约束 Constraints

  1. 外键约束:
    1. 在表创建时建立外键约束

15 存储过程和存储函数

创建一个存储过程,实现输入ID,返回姓名和手机号
在SQLyog中,Delimiter在前面和后面写,还有Begin

delimiter //

create procedure get_phone(in u_id INT , out b_name varchar(15), out b_phone varchar(15))
       begin 
           select b.`NAME`, b.phone into b_name, b_phone
           from beauty b
           where u_id = id;
       end //
       
delimiter;

16 变量、流程控制与游标

1. 变量

变量分为系统变量用户自定义变量

1.1 系统变量
1.1.1系统变量分类

1.1.2 查看系统变量

这里是引用

1.2 用户变量

面向对象

OOP (Object-oriented programing)

JAVA类Class以及类的成员

public TreeNode() {}

public TreeNode(int n) {
this.val = val;

}

类:是对一类事物的描述,是抽象的,概念上的定义 class,比如狗
对象:是实际存在的该类事物的个体,比如狗里的柯基

面向对象的重点就是设计类
设计类,其实就是设计类的成员

面向对象的三大特性

算法:

二分法

https://bbs.csdn.net/topics/604505141?spm=1001.2014.3001.6377

递归

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

在这里插入图片描述

这道题的巧妙之处在于利用f这样数列去代替函数,免去了递归,而是直接用数组去算。

class Solution {
    public int climbStairs(int n) {
        int[] f = new int[50];
        f[0] = f[1] = 1;
        for (int i = 2; i <= n; i++) {
            f[i] = f[i - 1] + f[i - 2];
        }
        return f[n];
    }
}

BFS宽度优先算法

Given the root of a binary tree, return the level order traversal of its nodes values. (From left to right, level by level)

图片来源LeetCode

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {

        
        List<List<Integer>> res= new ArrayList<>();
        if (root == null) {
            return res;
        }

        //解题关键在于这个Queue能储存的是个节点
        Queue<TreeNode> store = new LinkedList<>();
        store.add(root);
        //第一层while的触发,就是这一层开始被遍历
        while (store.size() > 0) {
            int tempSize = store.size();
            List<Integer> toAdd = new ArrayList<>();
            //第二层while就是对这一层中的所有节点进行处理,把节点的children添加到Queue
            //然后把自己的值添加到当前List中
            while (tempSize > 0) {
                TreeNode cur = store.poll();
                toAdd.add(cur.val);
                if (cur.left != null) {
                    store.add(cur.left);
                }

                if (cur.right != null) {
                    store.add(cur.right);
                }
                tempSize--;
            }
            //必须copy一份新的,防止传递
            List<Integer> copy = new ArrayList<>();
            for (int i = 0; i < toAdd.size(); i++) {
                copy.add(toAdd.get(i));
            }
            res.add(copy);
        }

        return res;

    }
}

并查集 Union Find

对于并查集来说,最主要的构建一个Class,在这个class里,有main, find, union三种功能。

main的功能就是构建一个长度符合功能要求的数组

并查集依靠数列去实现, 一开始每个数列里的值就是其本身的Index,但如果两个数合并,那么这个数作为Index在root数列中的值会变成另一个数的root。

Union(0, 1) -> root[1] = 0;
假设root[0] = 0;

对于find来说,就是不断去找到其相应的根的值,直到root本身的值和自身的Index相等为止

Find(x) -> if (x == root[x]) return x;
else return find(root[x]);

There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.

A province is a group of directly or indirectly connected cities and no other cities outside of the group.
You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise.
Return the total number of provinces.

在这里插入图片描述

class Solution {

    private int[] root;

    public int findCircleNum(int[][] isConnected) {

        //构建一个root数列
        int n = isConnected.length;
        root = new int[n];
        for (int i = 0; i < root.length; i++) {
            root[i] = i;
        }

        int count = 0;
        for (int i = 0; i < isConnected.length; i++) {
            for (int j = 0; j < isConnected[0].length; j++) {
                if (isConnected[i][j] == 1) {
                    union(i, j);
                }
            }
        }

        for (int i = 0; i < root.length; i++) {
            if (root[i] == i) {
                count++;
            }
        }

        return count;
    }

    private int find(int x) {
        if (x == root[x]) {
            return x;
        }
        return find(root[x]);
    }

    private void union(int x, int y) {
        root[find(x)] = find(y);
    }
}
class Solution {

    //process with UnionFind
    private int[] root;
    private int unionCount;
    private int row;
    private int column;

    // total - watercount (遍历中得到) - unionCount = result;

    public int numIslands(char[][] grid) {

        if (grid == null || grid.length == 0) {
            return 0;
        }

        //构建基本条件
        row = grid.length;
        column = grid[0].length;
        root = new int[row * column];
        for (int i = 0; i < root.length; i++) {
            root[i] = i;
        }
        int totalCount = row * column;
        int watercount = 0;

        //遍历
        for (int x = 0; x < row; x++) {
            for (int y = 0; y < column; y++) {
                if (grid[x][y] == '0') {
                    watercount++;
                } else {
                    processCoordinate(x, y, 1, 0, grid);
                    processCoordinate(x, y, -1, 0, grid);
                    processCoordinate(x, y, 0, 1, grid);
                    processCoordinate(x, y, 0, -1, grid);
                }
            }
        }

        return totalCount - unionCount - watercount;

    }

    private void processCoordinate(int x, int y, int offsetX, int offsetY, char [][] grid) {
        int curX = x + offsetX;
        int curY = y + offsetY;
        if (curX >= 0 && curX < row && curY >= 0 && curY < column && grid[curX][curY] == '1') {
            union(curX * column + curY, x * column + y);
            
        }
    }

    private int find(int x) {
        if (root[x] == x) {
            return x;
        }
        return find(root[x]);
    } 

    private void union(int x, int y) {
        int rootX = find(x);
        int rootY = find(y);
        if (rootX != rootY) {
            root[rootX] = rootY;
            unionCount++;
        }
    }
}

标签:27,return,日报,++,int,grid,TreeNode,root
来源: https://blog.csdn.net/jjsobig1/article/details/122722550