二叉树中序遍历打印节点信息

public static void main(String[] args){
BinaryTree tree = create();
MidOrderPrint(tree);
}


static class BinaryTree{
String data;
BinaryTree left;
BinaryTree right;

public BinaryTree(String data) {
this.data = data;
}
}

/**
* ........... a
* ........../.....\
* ........b........c
* ....../....\
* ....d........e
* .....\ ...../
* ......f....g

* dfbgeac
*/
private static BinaryTree create(){
BinaryTree a = new BinaryTree("a");

BinaryTree b = new BinaryTree("b");
BinaryTree c = new BinaryTree("c");
a.left = b;
a.right = c;

BinaryTree d = new BinaryTree("d");
BinaryTree e = new BinaryTree("e");

b.left = d;
b.right = e;

BinaryTree f = new BinaryTree("f");
BinaryTree g = new BinaryTree("g");

d.right = f;

e.left = g;

return a;
}

static void MidOrderPrint(BinaryTree tree){
if(tree == null) return ;

Stack<BinaryTree> stack = new Stack<>();
BinaryTree cur = tree;

while(!stack.isEmpty() || cur != null){
if(cur != null){
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
System.out.print(cur.data);
cur = cur.right;
}
}
}

有100个已排序数组,先需要合并为一个排序数组如何实现?数组长度在10w ~40w间

public static void main(String[] args){
int[][] arrs = new int[][]{
{1, 3, 4},
{1, 2, 5},
{0, 7, 5, 8}
};

int[] result = merge(arrs);

for(int num : result){
System.out.print(num+" ");
}
}


private static int getAllCount(int[][] arrs){
int size = 0;
for(int[] item : arrs){
if(item == null) continue;
size += item.length;
}
return size;
}

private static int[] getArrayIdxsAndSet_0(int size){
int[] idxs = new int[size];
// 初始化为零
for(int i = 0; i < idxs.length; i++){
idxs[i] = 0;
}
return idxs;
}

public static int[] merge(int[][] arrs) {
if(arrs == null) return null;

int allCount = getAllCount(arrs);

int[] result = new int[allCount];
int[] arrayIdxs = getArrayIdxsAndSet_0(arrs.length);

for(int resultIdx = 0; resultIdx < allCount; resultIdx++){

int minValue = 0;
int minArrayIdx = 0;
boolean tempNotSet = true;

for(int i = 0; i < arrs.length; i++){
int currentIdx = arrayIdxs[i];

if(currentIdx < arrs[i].length){
if(tempNotSet){
tempNotSet = false;
minValue = arrs[i][currentIdx];
minArrayIdx = i;
}else if(minValue > arrs[i][currentIdx]) {
minValue = arrs[i][currentIdx];
minArrayIdx = i;
}
}
}

arrayIdxs[minArrayIdx]++;
result[resultIdx] = minValue;
}

return result;
}

Condition

class BoundedBuffer {
final Lock lock = new ReentrantLock();
// 使用两个,可以做到通知对应的等待,不会通知到其他无关的对象
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition();
final Object[] items = new Object[100];
int putptr, takeptr, count;

public void put(Object x) throws InterruptedException {
lock.lock();
try {
while (count == items.length)
notFull.await();

items[putptr] = x;
if (++putptr == items.length) putptr = 0;
++count;

notEmpty.signal();
} finally {
lock.unlock();
}
}

public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0)
notEmpty.await();

Object x = items[takeptr];
if (++takeptr == items.length) takeptr = 0;
--count;

notFull.signal();
return x;
} finally {
lock.unlock();
}
}
}
public static void main(String[] args){
Bus bus = new Bus();

ExecutorService threadPool = Executors.newCachedThreadPool();
threadPool.execute(new MyRunnable(1, bus));
threadPool.execute(new MyRunnable(2, bus));
threadPool.execute(new MyRunnable(3, bus));
threadPool.shutdown();
}

static class MyRunnable implements Runnable{
private int idx;
private Bus bus;

public MyRunnable(int idx, Bus bus) {
this.idx = idx;
this.bus = bus;
}

@Override
public void run() {
for(int i = 0; i < 10; i++) {
switch (idx) {
case 1: bus.doOne(); break;
case 2: bus.doTwo(); break;
case 3: bus.doThree(); break;
}
}
}
}

static class Bus{
private Lock lock = new ReentrantLock();
private Condition oneCondition = lock.newCondition();
private Condition twoCondition = lock.newCondition();
private Condition threeCondition = lock.newCondition();

private int should = 1;

public void doOne(){
lock.lock();

try{
while (should != 1) toWait(oneCondition);

System.out.println("do one");
should = 2;
twoCondition.signal();
}finally {
lock.unlock();
}
}

public void doTwo(){
lock.lock();

try{
while (should != 2) toWait(twoCondition);

System.out.println("do two");
should = 3;
threeCondition.signal();
}finally {
lock.unlock();
}
}

public void doThree(){
lock.lock();

try{
while (should != 3) toWait(threeCondition);

System.out.println("do three");
System.out.println("");
should = 1;
oneCondition.signal();
}finally {
lock.unlock();
}
}

private void toWait(Condition condition){
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Two Sum

实例:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

public int[] twoSum(int[] nums, int target) {
Map map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target – nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException(“No two sum solution”);
}

MySql 常用命令

(1)show databases:查询所有数据库
(2)show tables:查询某一数据库中所有数据库表
(3)show variables:显示系统变量的名称和值
(4)show warnings:显示最后一条执行的SQL语句所产生的错误、警告等信息
(5)show status:显示系统特定资源的状态
(6)use database_name; 指定数据库
(7)desc table_name; 查看表结构

一、简单描述表结构,字段类型

select * from information_schema.columns
where table_schema = 'db' #表所在数据库
and table_name = 'tablename' ; #你要查的表

显示表结构,字段类型,主键,是否为空等属性,但不显示外键。

二、查询表中列的注释信息

select column_name, column_comment from information_schema.columns 
where table_schema ='db' and table_name = 'tablename' ;

四、查看表的注释

select table_name,table_comment from information_schema.tables 
where table_schema = 'db' and table_name ='tablename'

五、查看表生成的DDL

show create table table_name;

六、建表命令:

CREATE TABLE `t_sold_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` date DEFAULT NULL COMMENT '日期',
`hour` tinyint(2) DEFAULT '0' COMMENT '小时',
`hour_order` int(11) DEFAULT '0' COMMENT '小时订单数',
`total_order` int(11) DEFAULT '0' COMMENT '总的订单数',
`prediction` int(11) DEFAULT '0' COMMENT '预测订单数',
PRIMARY KEY (`id`),
UNIQUE KEY `dt_hour` (`dt`,`hour`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='实时订单数'

七、表操作命令:

复制表结构:create table table1 like table;
复制数据:insert into table1 select * from table

八、机器授权:

grant select on *.* to 'reader'@'%' identified by '123456' WITH GRANT OPTION
flush privileges

九、查询数据直接插入

insert into t_visual_user_domain(`user_id`,`domain`,`group`) 
select id,'www.baidu.com' as domain,`group` from t_visual_user;

十、修改表结构

alter table competitor_goods add sku_id bigint(20) unsigned DEFAULT NULL COMMENT '商品销售码';

Python 安装 Mysql模块 报错

Mac上使用命令:pip3 install mysql-connector-python-rf安装MySql模块时,

报错:
error: option –single-version-externally-managed not recognized

原因可能是我下载的安装包是wheel格式的安装包,如果电脑上没有安装wheel的话就会报这个错误。

我自己是这么解决的:

首先安装wheel:pip install wheel

然后再用pip安装我想要包:pip3 install mysql-connector-python-rf