ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1月27日报

2022-01-27 23:33:42  阅读:149  来源: 互联网

标签:27 return 日报 ++ int grid TreeNode root


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系统变量分类

  • 服务器层面
  • 全局系统变量 (global) 和会话系统变量(local)
  • 全局变量不能跨重启

1.1.2 查看系统变量

  • show global variables;
  • show session variables
  • show variables //默认查询的是会话系统变量
  • 如何查看和修改系统变量

这里是引用

1.2 用户变量

  • 会话用户变量和局部变量
  • 会话用户变量:作用域和会话变量一样,只对当前连接会话有效。
  • 局部变量:只在 BEGIN 和 END 语句块中有效。局部变量只能在存储过程和函数中使用。

面向对象

OOP (Object-oriented programing)

JAVA类Class以及类的成员

  • 属性,class里的一些变量,比如name, id…,成员变量
  • 方法,method
  • 构造器
public TreeNode() {}

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

}
  • 代码块
  • 内部类,子类,一个class里再接一个class

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

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

面向对象的三大特性

  • 封装: Encapsulation
  • 继承: Inheritance
  • 多态: Polymorphism

算法:

二分法

  • 搜索旋转排序
    参考九日集训第三日的帖子

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

递归

  • 70 Climbing Stairs
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宽度优先算法

  • 102 Binary Tree Level Order Traversal & 102 Reverse(反转前面的结果)

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]);

  • 547 Number of Provinces

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);
    }
}
  • 200 岛屿数量

  • 在这里插入图片描述

    • 二维数组和一维数组的坐标转换 (x * column + 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有