迁移仓库

This commit is contained in:
zeek 2020-02-23 22:02:58 +08:00
commit 863e298e91
23 changed files with 776 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
target
.idea

9
README.md Normal file
View File

@ -0,0 +1,9 @@
## 简介
日常刷lintCode算法,提高自己的编码能力.
## 简单
## 中等
- [三个数之和](leetCode/ThreeSum.md)

60
leetCode/ThreeSum.md Normal file
View File

@ -0,0 +1,60 @@
## 题目描述
给定一个包含 n 个整数的数组 nums判断 nums 中是否存在三个元素 abc 使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例:
给定数组 nums = [-1, 0, 1, 2, -1, -4]
满足要求的三元组集合为:
```
[
[-1, 0, 1],
[-1, -1, 2]
]
```
## 解题思路
![2020-01-19_21-44.png](https://img.zeekling.cn/images/2020/01/19/2020-01-19_21-44.png)
## 答案
```java
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
int min=0,mid=1, max=nums.length-1, s= 0;
while(nums.length >= 3 && min < nums.length && nums[min] <= 0){
mid = min + 1;
max = nums.length-1;
if(min > 0 && nums[min] == nums[min -1]){
min ++;
continue;
}
while(mid < max){
s = nums[min] + nums[mid] + nums[max];
if (s < 0){
mid ++;
}else if(s > 0){
max --;
}else if (s == 0){
res.add(Arrays.asList(nums[min],nums[mid], nums[max]));
while(mid < max && nums[mid] == nums[mid + 1]){
mid ++;
}
while(mid< max && nums[max] == nums[max-1]){
max --;
}
mid ++;
max --;
}
}
min++;
}
return res;
}
}
```

16
make.sh Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
#author:zeekling
echo "compiling"
if [ ! -d target ];then
mkdir target
else
rm -rf target/*
fi
file=`find -name "*.java"`
echo "copying file"
all=`find -name "*"`
javac -g -source 8 -target 8 -d ./target/ ${file} -encoding UTF-8 &&
echo -e "\033[32mcompile finish *_*\033[0m \033[37m" ||
echo -e "\033[31mcompile error \033[0m \033[37m"

View File

@ -0,0 +1,40 @@
## 题目
某网站包含两个表Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Customers 表:
```
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
```
Orders 表:
```
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
```
例如给定上述表格,你的查询应返回:
```
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
```
## 答案
```sql
select Name as Customers
from Customers a
where a.id not in (select CustomerId from Orders)
```

View File

@ -0,0 +1,26 @@
## 题目
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
示例:
```
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
```
根据以上输入,你的查询应返回以下结果:
```
+---------+
| Email |
+---------+
| a@b.com |
+---------+
```
## 答案
```sql
select Email from Person group by Email having count(*) > 1
```

View File

@ -0,0 +1,32 @@
## 题目:
Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id此外还有一列对应员工的经理的 Id。
```
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+
```
给定 Employee 表,编写一个 SQL 查询该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中Joe 是唯一一个收入
超过他的经理的员工。
```
+----------+
| Employee |
+----------+
| Joe |
+----------+
```
## 答案
```sql
select a.Name as Employee
from Employee a, (select Salary,Id from Employee) b
where a.ManagerId=b.Id and a.Salary > b.Salary
```

26
src/jiuzhang/PlusAB.java Normal file
View File

@ -0,0 +1,26 @@
package jiuzhang;
/**
* @apiNote 给出两个整数 aaa bbb , 求他们的和
* @author zeekling
* @version 1.0
* @since 2019-12-11
*/
public class PlusAB {
public int plus(int a, int b){
while(b != 0){
int ta = a^b;
int tb = (a&b)<<1;
a = ta;
b = tb;
}
return a;
}
public static void main(String[] args){
PlusAB ab = new PlusAB();
System.out.println(ab.plus(1,3));
}
}

View File

@ -0,0 +1,54 @@
package leetCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ThreeSum{
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
int min=0,mid=1, max=nums.length-1, s= 0;
while(nums.length >= 3 && min < nums.length && nums[min] <= 0){
mid = min + 1;
max = nums.length-1;
if(min > 0 && nums[min] == nums[min -1]){
min ++;
continue;
}
while(mid < max){
s = nums[min] + nums[mid] + nums[max];
if (s < 0){
mid ++;
}else if(s > 0){
max --;
}else if (s == 0){
res.add(Arrays.asList(nums[min],nums[mid], nums[max]));
while(mid < max && nums[mid] == nums[mid + 1]){
mid ++;
}
while(mid< max && nums[max] == nums[max-1]){
max --;
}
mid ++;
max --;
}
}
min++;
}
return res;
}
public static void main(String[] args){
ThreeSum sum = new ThreeSum();
int[] arr = new int[]{0,0,0};
List<List<Integer>> res = sum.threeSum(arr);
for(List<Integer> list: res){
for (Integer a: list){
System.out.print(a + "\t");
}
System.out.println("");
}
}
}

View File

@ -0,0 +1,47 @@
package simple;
/**
* @apiNote 给定一个二叉树,确定它是高度平衡的对于这个问题,一棵高度平衡的二叉树的定义是一棵二叉树中每个节点的两个子树的深度相差不会超过1
* @author zeekling
* @version 1.0
* @since 2019-12-28
*/
public class BalancedTree {
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
public boolean isBalanced(TreeNode root) {
// write your code here
if (root == null){
return true;
}
int leftDepth = depth(root.left);
int rightDepth = depth(root.right);
if (Math.abs(leftDepth - rightDepth) > 1){
return false;
}else{
return isBalanced(root.left) && isBalanced(root.right);
}
}
public int depth(TreeNode root){
if (root == null){
return 0;
}
int leftDepth = depth(root.left) + 1;
int rightDepth = depth(root.right) + 1;
return Math.max(leftDepth, rightDepth);
}
}
class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}

View File

@ -0,0 +1,39 @@
package simple;
/**
* @apiNote 比较两个字符串A和B确定A中是否包含B中所有的字符字符串A和B中的字符都是 大写字母
* A 中出现的 B 字符串里的字符不需要连续或者有序
* @author zeekling
* @version 1.0
* @since 2019-12-15
*/
public class CompareStrings {
/**
* @param A: A string
* @param B: A string
* @return: if string A contains all of the characters in B return true else return false
*/
public boolean compareStrings(String a, String b) {
// write your code here
if(a.length() < b.length()){
return false;
}
char[] aa = a.toCharArray();
char[] ba = b.toCharArray();
for (int i=0; i< ba.length;i++ ){
boolean e = false;
for (int j=0;j<aa.length;j++){
if(aa[j] == ba[i]){
e = true;
aa[j] = ' ';
break;
}
}
if(!e){
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,47 @@
package simple;
/**
* @apiNote 合并两个有序升序的整数数组A和B变成一个新的数组新数组也要有序
* @author zeekling
* @version 1.0
* @since 2019-12-12
*/
public class MergeSortedArray{
/**
* @param A: sorted integer array A
* @param B: sorted integer array B
* @return: A new sorted integer array
*/
public int[] mergeSortedArray(int[] A, int[] B) {
// write your code here
int size = A.length + B.length;
int[] result = new int[size];
int ia = 0,ib = 0, ir = 0;
while (ia < A.length && ib < B.length ){
if (A[ia] <= B[ib]){
result[ir++] = A[ia++];
}else {
result[ir++] = B[ib++];
}
}
while (ia < A.length){
result[ir++] = A[ia++];
}
while(ib < B.length){
result[ir++] = B[ib++];
}
return result;
}
public static void main(String[] args){
int a[] = {1,2};
int b[] = {2,3};
MergeSortedArray m = new MergeSortedArray();
int[] c = m.mergeSortedArray(a, b);
for (int i=0;i<c.length;i++){
System.out.print(c[i] + "\t");
}
}
}

View File

@ -0,0 +1,50 @@
package simple;
/**
* @apiNote 合并两个排序的整数数组A和B变成一个新的数组
* 样例
* 样例 1:
*
* 输入[1, 2, 3] 3 [4,5] 2
* 输出[1,2,3,4,5]
* 解释:
* 经过合并新的数组为[1,2,3,4,5]
* 样例 2:
*
* 输入[1,2,5] 3 [3,4] 2
* 输出[1,2,3,4,5]
* 解释
* 经过合并新的数组为[1,2,3,4,5]
* 注意事项
* 你可以假设A具有足够的空间A数组的大小大于或等于m+n去添加B中的元素
* @author zeekling
* @version 1.0
* @since 2019-12-16
*/
public class MergeSortedArrayInA {
/*
* @param A: sorted integer array A which has m elements, but size of A is m+n
* @param m: An integer
* @param B: sorted integer array B which has n elements
* @param n: An integer
* @return: nothing
*/
public void mergeSortedArray(int[] a, int m, int[] b, int n) {
// write your code here
int ia = m - 1,ib = n-1, il = m + n - 1;
while (il>=0) {
int min = 0;
if(ia >= 0 && ib >= 0 && a[ia] >= b[ib]){
min = a[ia--];
}else if (ia>=0 && ib>= 0 && b[ib] > a[ia]){
min = b[ib--];
} else if (ia >=0 ){
min = a[ia--];
} else {
min = b[ib--];
}
a[il--] = min;
}
}
}

View File

@ -0,0 +1,29 @@
package simple;
/**
* @apiNote 给定一个只含非负整数的m*n网格找到一条从左上角到右下角的可以使数字和最小的路径
* @author zeekling
* @version 1.0
* @since 2019-12-29
*/
public class MinPathSum {
/**
* @param grid: a list of lists of integers
* @return: An integer, minimizes the sum of all numbers along its path
*/
public int minPathSum(int[][] grid) {
// write your code here
int m = grid.length;
int n = grid[0].length;
int[][] dp = new int[m][n];
dp[0][0] = grid[0][0];
for(int i=1;i<m;i++)
dp[i][0] = dp[i-1][0] + grid[i][0];
for(int j=1;j<n;j++)
dp[0][j] = dp[0][j-1] + grid[0][j];
for(int i=1;i<m;i++)
for(int j=1;j<n;j++)
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j];
return dp[m-1][n-1];
}
}

View File

@ -0,0 +1,33 @@
package simple;
import java.util.ArrayList;
import java.util.List;
public class ProductExcludeItself {
/*
* @param nums: Given an integers array A
* @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
*/
public List<Long> productExcludeItself(List<Integer> nums) {
// write your code here
int n = nums.size();
long[] left = new long[n];
long[] right = new long[n];
for(int i=0;i<n;i++){
left[i] = 1;
right[i] = 1;
}
ArrayList<Long> res = new ArrayList<>();
for (int i = 1; i < n; ++i){
left[i] = left[i-1] * nums.get(i-1);
right[n-i-1] = right[n-i] * nums.get(n-i);
}
for(int i = 0; i != nums.size(); ++i){
res.add( left[i] * right[i] );
}
return res;
}
}

View File

@ -0,0 +1,36 @@
package simple;
import java.util.List;
/**
* @apiNote 给定一个旋转排序数组在原地恢复其排序升序
* Example1: [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]
* Example2:[6,8,9,1,2] -> [1,2,6,8,9]
* @author zeekling
* @version 1.0
* @since 2019-12-14
*/
public class RecoverRotatedSortedArray {
/**
* @param nums: An integer array
* @return: nothing
*/
public void recoverRotatedSortedArray(List<Integer> nums) {
// write your code here
int end = 0;
for (int i=1; i<nums.size();i++ ){
if (nums.get(i-1) > nums.get(i)){
end = i;
break;
}
}
if (end == 0 || end == nums.size()){
return;
}
for (int i=0;i<end;i++){
nums.add(nums.get(i));
}
nums.subList(0, end).clear();
}
}

View File

@ -0,0 +1,30 @@
package simple;
/**
* @apiNote 给定一个排序数组在原数组中删除重复出现的数字使得每个元素只出现一次并且返回数组的长度
* 不要使用额外的数组空间必须在不使用额外空间的条件下原地完成
* @author zeekling
* @version 1.0
* @since 2019-12-28
*/
public class RemoveDuplicates {
/*
* @param nums: An ineger array
* @return: An integer
*/
public int removeDuplicates(int[] nums) {
// write your code here
if (nums == null || nums.length == 0) {
return 0;
}
int tmp = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] != nums[tmp]){
tmp++;
nums[tmp] = nums[i];
}
}
return tmp + 1;
}
}

View File

@ -0,0 +1,31 @@
package simple;
/**
* @apiNote 翻转一个链表
* 输入: 1->2->3->null
* 输出: 3->2->1->null
* @author zeekling
* @version 1.0
* @since 2019-12-14
*/
public class ReverseLinkedList {
public ListNode revert(ListNode head){
ListNode next = null, pre = null;
while (head != null){
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
}
class ListNode{
int val;
ListNode next;
public ListNode(int val){
this.val = val;
}
}

View File

@ -0,0 +1,43 @@
package simple;
/**
* @apiNote 给定一个字符串逐个翻转字符串中的每个单词
* 单词的构成
* 无空格字母构成一个单词有些单词末尾会带有标点符号
* 输入字符串是否包括前导或者尾随空格可以包括但是反转后的字符不能包括
* 如何处理两个单词间的多个空格在反转字符串中间空格减少到只含一个
* https://www.lintcode.com/problem/reverse-words-in-a-string/description
* @author zeekling
* @version 1.0
* @since 2019-12-15
*/
public class ReverseWordsByBlank {
/*
* @param s: A string
* @return: A string
*/
public String reverseWords(String s) {
// write your code here
if (s.length() == 0){
return s;
}
StringBuilder sb = new StringBuilder();
char[] sa = s.toCharArray();
int end = sa.length,start = sa.length-1;
for (int i=sa.length-1;i>=0 ; i--) {
if (sa[i] != ' '){
start = i;
}
if (sa[i] == ' ' && (start - 1) == i){
sb.append(s.substring(start, end)).append(" ");
}
if (sa[i] == ' '){
end = i;
}
}
sb.append(s.substring(0,end));
return sb.toString();
}
}

View File

@ -0,0 +1,27 @@
package simple;
public class RotateString {
public char[] rotateString(char[] A, int offset) {
if (A == null || A.length == 0) {
return A;
}
int len = A.length;
offset %= len;
reverse(A, 0, len - offset - 1);
reverse(A, len - offset, len - 1);
reverse(A, 0, len - 1);
return A;
}
private void reverse(char[] str, int start, int end) {
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
}

View File

@ -0,0 +1,36 @@
package simple;
/**
* @apiNote 写出一个高效的算法来搜索 m × n矩阵中的值这个矩阵具有以下特性
* 每行中的整数从左到右是排序的
* 每行的第一个数大于上一行的最后一个整数
* @author zeekling
* @version 1.0
* @since 2019-12-14
*/
public class SearchSortedMatrix {
/**
* @param matrix: matrix, a list of lists of integers
* @param target: An integer
* @return: a boolean, indicate whether matrix contains target
*/
public boolean searchMatrix(int[][] matrix, int target) {
// write your code here
if (matrix.length == 0 || matrix[0].length == 0){
return false;
}
int row=0,line=matrix.length-1, len=matrix[0].length;
while(row < len && line >= 0){
if(matrix[line][row] == target){
return true;
}
if (matrix[line][row] < target){
row ++;
}else{
line --;
}
}
return false;
}
}

View File

@ -0,0 +1,37 @@
package simple;
/**
* @apiNote 对于一个给定的 source 字符串和一个 target 字符串你应该在 source 字符串中找出 target 字符串出现的第一个位
* (从0开始)如果不存在则返回 -1
* @author zeekling
* @version 1.0
* @since 2019-12-13
*/
public class SubStringLocation {
public int strStr(String source, String target){
char[] s = source.toCharArray();
char[] t = target.toCharArray();
if (s.length == t.length && s.length == 0){
return 0;
}
int i = 0;
while (i < s.length){
int j = 0, k = i;
while (j < t.length && k < s.length && s[k] == t[j]){
j++;
k++;
}
if (j == t.length){
return i;
}
i++;
}
return -1;
}
public static void main(String[] args){
SubStringLocation s = new SubStringLocation();
System.out.println(s.strStr("source","target"));
}
}

25
src/simple/ZerosOfN.java Normal file
View File

@ -0,0 +1,25 @@
package simple;
/**
* @apiNote 设计一个算法计算出n阶乘中尾部零的个数
* @author zeekling
* @version 1.0
* @since 2019-12-12
*/
public class ZerosOfN{
public long trailingZeros(long n){
long tmp = 5;
long count = 0;
while (tmp <= n){
count += n / tmp;
tmp *= 5;
}
return count;
}
public static void main(String[] args){
ZerosOfN z = new ZerosOfN();
System.out.println(z.trailingZeros(11));
}
}