- 博客(140)
- 收藏
- 关注
原创 解决mysql执行删除表中外键的语句,外键却仍然存在的问题
笔者最近在学习尚硅谷的数据库课程时,又遇到了一个奇怪的小问题,但是老师在网课里却没有演示并解释如何去解决::使用 `alter table` 语句删除外键约束: alter table stuinfo drop foreign key fk_stuinfo_major; Terminal显示语句执行成功: 但是当使用`show index` 语句再次查看索引时却发现依然存在: show index from stuinfo; 原因是为什么呢?笔者经过搜索和阅读,找到了一种比.
2021-04-14 19:00:31
958
1
原创 在 MySQL 5.7.19 已经安装完成之后修改默认字符集
笔者在B站先后学习了狂神和尚硅谷的数据库课程。在学习尚硅谷的课程中遇到了插入记录失败的问题,错误码是1366 Incorrect string value,经查询应该是字符集的问题。首先以管理员身份运行Terminal,登录进入MySQL以后,输入如下指令:show variables like '%char%';可以看到类似这样的结果:说明我们有些地方还未设置为 utf8,才会出现这种情况。然后我们需要在安装目录中打开 my.ini 文件,对配置进行修改,如下图所示:分别在 [c
2021-04-09 18:42:15
362
原创 Mac下安装配置一种或多种版本JDK的方法总结
1.安装一个版本的JDK1.1 从官网下载JDK安装1.2 通过homebrew下载2.安装多个版本的JDK2.1如何配置环境变量
2020-10-27 23:25:26
318
原创 JZ29 最小的K个数
保持堆的大小为K,然后遍历数组中的数字,遍历的时候做如下判断:若目前堆的大小小于K,将当前数字放入堆中; 否则判断当前数字与大根堆堆顶元素的大小关系,如果当前数字比大根堆堆顶还大,这个数就直接跳过; 反之如果当前数字比大根堆堆顶小,先poll掉堆顶,再将该数字放入堆中。import java.util.*;public class Solution { public ArrayList<Integer> GetLeastNumbers_Solution(int [] in
2020-09-07 01:56:56
287
原创 JZ28 数组中出现次数超过一半的数字
这道题最简单的方法是摩尔投票法:https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/solution/mian-shi-ti-39-shu-zu-zhong-chu-xian-ci-shu-chao-3/代码如下:public class Solution { public int MoreThanHalfNum_Solution(int [] arra.
2020-09-07 01:28:54
124
原创 JZ30 连续子数组的最大和
这是很经典的动态规划题:public class Solution { public int FindGreatestSumOfSubArray(int[] array) { int ans = array[0]; int prev = array[0]; for(int i = 1; i < array.length; i++){ int curr = array[i]; if(prev
2020-09-07 00:02:33
104
原创 JZ27 字符串的排列
https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/solution/mian-shi-ti-38-zi-fu-chuan-de-pai-lie-hui-su-fa-by/代码如下:import java.util.*;public class Solution { public ArrayList<String> Permutation(String str) { ArrayList<
2020-09-06 23:14:09
154
原创 JZ26 二叉搜索树与双向链表
https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/solution/mian-shi-ti-36-er-cha-sou-suo-shu-yu-shuang-xian-5/采用中序遍历来做。需要注意的是牛客网不需要我们把头和尾相连接。代码如下:/**public class TreeNode { int val = 0; TreeNode left = null;
2020-09-06 22:09:59
371
原创 JZ25 复杂链表的复制
复制每个节点,如:复制节点A得到A1,将A1插入到节点A后面; 遍历链表,A1.random = A.random.next; 将链表拆分成原链表和复制后的链表。/*public class RandomListNode { int label; RandomListNode next = null; RandomListNode random = null; RandomListNode(int label) { this.label = lab
2020-09-06 20:53:56
194
原创 JZ24 二叉树中和为某一值的路径
https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/solution/mian-shi-ti-34-er-cha-shu-zhong-he-wei-mou-yi-zh-5/代码如下:import java.util.ArrayList;/**public class TreeNode { int val = 0; TreeNode left = null;
2020-08-31 23:54:56
201
原创 JZ23 二叉搜索树的后序遍历序列
https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/solution/mian-shi-ti-33-er-cha-sou-suo-shu-de-hou-xu-bian-6/代码如下:public class Solution { public boolean VerifySquenceOfBST(int [] sequence) { // Corner Case
2020-08-31 23:15:44
189
原创 JZ22 从上往下打印二叉树
这道题就是经典的BFS。需要注意的两点是当root == null的时候直接返回空ArrayList,还有就是向队列里添加左右子节点是需要先判断它们是否为null。import java.util.*;/**public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = va
2020-08-31 17:49:54
146
原创 JZ21 栈的压入、弹出序列
考虑借用一个辅助栈stack,模拟压入 / 弹出操作的排列。根据是否模拟成功,即可得到结果。算法流程:初始化: 辅助栈 stack,弹出序列的索引 idx; 遍历压栈序列: 各元素记为 num; 元素 num 入栈; 循环出栈:若 stack 的栈顶元素 == 弹出序列元素 popped[idx],则执行出栈与 idx++; 返回值: 若 stack 为空,则此弹出序列合法import java.util.ArrayList;import java.util.Stack;publ..
2020-08-31 17:32:59
177
原创 JZ20 包含min函数的栈
新建一个栈,栈内存放的元素是一个长度为2的数组。数组的第一个元素是要压入栈的整数本身,第二个元素是迄今为止最小的数。import java.util.Stack;public class Solution { Stack<int[]> stack = new Stack<>(); public void push(int node) { if(stack.isEmpty()){ stack.push(new
2020-08-31 17:04:34
130
原创 JZ19 顺时针打印矩阵
https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/solution/mian-shi-ti-29-shun-shi-zhen-da-yin-ju-zhen-she-di/import java.util.ArrayList;public class Solution { public ArrayList<Integer> printMatrix(int [][] matrix) {
2020-08-28 18:10:08
227
原创 JZ18 二叉树的镜像
/**public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class Solution { public void Mirror(TreeNode root) { // Base Case and.
2020-08-28 17:23:28
288
原创 JZ17 树的子结构
HasSubtree()函数:Corner Case判断:当Tree1和Tree2都不为null的时候才进行比较,否则直接返回false 判断必须以当前节点root1为根节点的Tree1是否包含Tree2,调用helper()函数按照先序遍历来检查每一个node 如果不包含,分别拿root1的左子节点和右子节点作为输入,再调用HasSubtree()自身helper()函数:如果Tree2已经遍历完了都能对应的上,返回true 如果Tree2还没有遍历完,Tree1却遍历完了,返回false
2020-08-28 17:11:53
1059
原创 JZ16 合并两个排序的列表
采用假头+双指针法,但是需要注意当遍历完了至少一个list以后的情况(即post-processing)。代码如下:/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode Merge(ListNode list1,ListN
2020-08-28 12:31:18
110
原创 JZ13 调整数组顺序使奇数位于偶数前面
这道题如果用快慢指针的话会打乱顺序,因此我们选择新开一个数组,用空间换时间:public class Solution { public void reOrderArray(int [] array) { int[] evens = new int[array.length]; int oddIndex = 0, evenIndex = 0; for(int num: array){ if(num % 2 != 0){
2020-08-28 12:13:14
113
原创 JZ12 数值的整数次方
首先需要讨论各种case:如果 base == 0, 直接返回本身; 如果 exponent < 0, base取倒数,exponent取相反数;再通过一个循环,得到 base ^ exponent。代码如下:public class Solution { public double Power(double base, int exponent) { // Corner Case if(base == 0){ return
2020-08-27 12:03:18
154
原创 JZ11 二进制中1的个数
每次和1做按位与运算,如果得到的是1那么count就加1,然后再无符号右移一位。代码如下:public class Solution { public int NumberOf1(int n) { int count = 0; while(n != 0){ if((n & 1) != 0){ count++; } n = n >>>
2020-08-27 11:39:36
95
原创 JZ10 矩形覆盖
依旧是斐波那契数列2*n的大矩形,和n个2*1的小矩形其中target*2为大矩阵的大小有以下几种情形:target <= 0 大矩形为<= 2*0,直接return 1; target = 1大矩形为2*1,只有一种摆放方法,return1; target = 2 大矩形为2*2,有两种摆放方法,return2; target = n 分为两步考虑: 第一次摆放一块 2*1 的小矩阵,则摆放方法总共为f(n-1) ✔ ✔...
2020-08-27 11:26:05
111
原创 JZ9 变态跳台阶
因为n级台阶,第一步有n种跳法:跳1级、跳2级、到跳n级:跳1级,剩下n-1级,则剩下跳法是f(n-1);跳2级,剩下n-2级,则剩下跳法是f(n-2);跳n-1级,剩下1级,则剩下跳法是f(1);还有1种跳法就是直接一口气跳n级。所以f(n)=f(n-1)+f(n-2)+...+f(1)+1;因为f(n-1)=f(n-2)+f(n-3)+...+f(1)+1;所以f(n)=2*f(n-1)。public class Solution { public int JumpFloor
2020-08-27 11:01:20
243
原创 LeetCode 437. Path Sum III
/** * 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) { .
2020-08-11 10:59:36
86
原创 LeetCode 113. Path Sum II
这道题用DFS解比较好,注意吃和吐成对出现,代码如下:/** * 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, TreeN
2020-08-11 08:27:04
98
原创 LeetCode 112. Path Sum
这道题用recursive的方法比较简洁,代码如下:/** * 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, TreeNo
2020-08-11 07:52:12
115
原创 HJ6 质数因子
import java.util.*;public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ long n = sc.nextLong(); long k = 2; while(k <= n){ .
2020-08-08 12:03:19
868
原创 HJ5 进制转换
这道题需要注意的点是要从第2位开始读取数字,因为开头两个是0x。代码如下:import java.util.*;public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){ int sum = 0; String s = sc.nex
2020-08-08 10:10:56
451
原创 HJ4 字符串分割
StringBuilder的substring()方法返回的是String,而delete()方法返回的还是StringBuilder(index是左闭右开)。代码如下:import java.util.*;public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){
2020-08-08 09:38:39
575
原创 LeetCode 55. Jump Game
这道题可以用贪心算法也可以用动态规划。老师说虽然动态规划的解法时间复杂度更高,但更具有普遍性。代码如下:class Solution { public boolean canJump(int[] nums) { int n = nums.length; if(n == 1){ return true; } boolean[] dp = new boolean[n]; dp[n - 1] =
2020-08-08 07:37:56
117
原创 LeetCode 119. Pascal‘s Triangle II
这道题需要注意的是每次循环在List头部插入1,其效果就是将上一次循环计算出来的List整体向后平移1位。然后使用set方法,将List的第 j 位设置成 List.get(j) + List.get(j + 1)的值,也就是在Pascal三角中自己上方的两个元素。因为之前的List已经整体平移过1个单位,所以是j & j + 1。代码如下:class Solution { public List<Integer> getRow(int rowIndex) {
2020-08-08 06:56:49
137
原创 LeetCode 118. Pascal‘s Triangle
这道题需要注意的是index越界问题。代码如下:class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> res = new ArrayList<>(); for(int row = 0; row < numRows; row++){ List<Int
2020-08-07 15:38:39
91
原创 LeetCode 442. Find All Duplicates in an Array
因为数组里所有的数大小都在1~n之间,所有我们可以建立一种映射:当我们访问到a[i]时,我们把index为 a[i] - 1(亦即第a[i]个)的数取反。因此当我们第二次遇到a[i]的时候,对应index的数已经是负数了。这时候我们就把 a[i] = index + 1加入到List当中。代码如下:class Solution { public List<Integer> findDuplicates(int[] nums) { List<Integer>
2020-08-07 15:01:35
125
原创 LeetCode 705. Design HashSet
我们采用 LinkedList 实现 HashSet 中的桶。对于每个功能 add,remove,contains,我们首先生成桶的散列值,操作相对应的桶。class MyHashSet { private Bucket[] bucketArray; private int keyRange; /** * Initialize your data structure here. */ public MyHashSet() { keyRange = 769;
2020-08-06 12:38:18
120
原创 LeetCode 342. Power of Four
class Solution { public boolean isPowerOfFour(int num) { while((num > 0) && (num % 4 == 0)){ num /= 4; } return num == 1; }}
2020-08-06 09:34:13
82
原创 LeetCode 51. N-Queens
class Solution { public List<List<String>> solveNQueens(int n) { char[][] chess = new char[n][n]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ chess[i][j] = '.'; } .
2020-08-04 09:42:27
93
原创 LeetCode 520. Detect Capital
这道题使用一个count变量来统计大写字母出现的次数。当且仅当下列三种情况出现时才是正确的:count == 0: 全是小写字母 count == n: 全是大写字母 count == 1 && word.charAt(0) <= 'Z': 只有开头是大写字母值得注意的是大写字母的ASCII码小于小写字母的ASCII码,代码如下:class Solution { public boolean detectCapitalUse(String word) {
2020-08-03 10:23:54
118
原创 LeetCode 254. Factor Combinations
step 1: 先按照从大到小的顺序找出所有的因数step 2: 递归地解决问题(类似于99 cents)需要注意的是在尝试完本层所有的branch以后退回上一层之前需要状态重置即删除之前加入path的数。class Solution { public List<List<Integer>> getFactors(int n) { List<List<Integer>> res = new ArrayList<>
2020-08-03 09:37:45
114
原创 LaiCode 179. All Valid Permutations Of Parentheses II
public class Solution { public List<String> validParentheses(int l, int m, int n) { // Write your solution here int targetLen = (l + m + n) * 2; StringBuilder curr = new StringBuilder(); List<String> res = new ArrayList<&g.
2020-08-02 08:17:10
268
原创 LeetCode 79. Word Search
class Solution { public boolean exist(char[][] board, String word) { // Corner Case if(board == null || board.length == 0 || board[0] == null || board[0].length == 0){ return false; } int rows = board.length.
2020-07-30 09:45:35
93
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人