MySQL安装(Ubuntu系统)

安装

1
apt-get install mysql-server

安装过程中输入root账号密码 rootpassword

配置

因为安装成功后通常只有本地root账户,所以需配置远程用户。

1
2
3
4
5
6
7
mysql -u root -u rootpassword

GRANT ALL PRIVILEGES ON *.* TO username@localhost IDENTIFIED BY 'password' WITH GRANT OPTION;
或者
INSERT INTO user VALUES('%','username',PASSWORD('password'), 'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y');

flush privileges;//使修改生效

大小写

Ubuntu安装mysql后发现大小写敏感

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> show variables like "%case%";
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_file_system | OFF |
| lower_case_table_names | 0 |
+------------------------+-------+

修改/etc/mysql/my.cnf
[mysqld]的后面加
lower_case_table_names=1
0,区分大小写; 1,不区分

重启mysql sudo service mysql restart

JDK配置(Ubuntu系统)

1、下载JDK8解压至/opt/jdk/jdk8 相关命令:tar zxvf

2、配置环境变量

1
2
3
4
5
6
sudo vi ~/.bashrc

export JAVA_HOME=/opt/jdk/jdk8
export PATH=$PATH:$JAVA_HOME/bin

source .bashrc

3、java -version 发现配置无效
原来系统存在默认JDK(OpenJDK),为了将新安装的JDK设成系统默认JDK,需进行如下操作。

1
2
3
4
5
6

sudo update-alternatives --install /usr/bin/java java /opt/jdk/jdk8/bin/java 300
sudo update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk8/bin/javac 300

sudo update-alternatives --config java
#输入数字选择默认jdk

4、java -version发现配置成功

Linked List Cycle

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

两个node,速度不同,循环
slow 为 慢
fast为快

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast){
return true;
}
}
return false;
}
}

Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

1
2
3
4
5
6
站在当前节点的角度,设置其next。
next存在
判断next是否重复
重复(当前next设置为next的next(即移除重复),循环判断新next值是否满足条件)
不重复(next不变,判断next的next是否满足条件)
不存在(返回null)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
List l = new ArrayList();
if(head==null){
return null;
}
l.add(head.val);
head.next=ge(head,l);
return head;
}

public ListNode ge(ListNode ln,List l){
if(ln.next == null){
return null;
}
if(l.contains(ln.next.val)){
if(ln.next.next==null){
ln.next = null;
return null;
}else{
ln.next = ln.next.next;
ln.next=ge(ln,l);
}
return ln.next;
}else{
l.add(ln.next.val);
ln.next.next=ge(ln.next,l);
return ln.next;
}
}
}

Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

最初思路,第一手股票遇第一个极大值卖出,然后等极小值(必须接下来有其他涨幅(这里需))买入。
当在某一时刻时,可买时,找下一时刻极小值且不为最后元素;可卖时找找极大值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Solution {
public int maxProfit(int[] prices) {
int lirun = 0;
int money = 0;
int price = 0;
boolean buy = true;
if(prices.length==0||prices.length==1){
return 0;
}
if(prices[0]<prices[1]){
money=-prices[0];
buy=false;
}
for(int i=1;i<prices.length;i++){
boolean cz = t(i,prices,buy);
if(cz){
if(buy){
money = money - prices[i];
price = prices[i];
buy = false;
}else{
money = money + prices[i];
price = 0;
buy = true;
}
}
}
return money;
}


public boolean t(int i,int[] prices,boolean buy){
if(buy){
if(i>=prices.length-1){
return false;
}
if(prices[i]<prices[i+1]){
return true;
}
return false;
}else{
if(i>=prices.length-1){
return true;
}else if(prices[i]>prices[i+1]){
return true;
}
return false;
}
}
}

Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.
Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer’s last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10):
Test cases had been added to test the overflow behavior.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class Solution {
public int reverse(int x) {
//几种特殊情况,100,反转去前导0;超过int范围的返回0。注意符号。
List<Integer> arrayList = new ArrayList<Integer>();
if(x==0){
return 0;
}
int ss = 1;

for(;;){
//求余;
int r = x%ss;
if(r==0&&x<ss){
break;
}else{
arrayList.add(r);
ss*=10;
}
}
double dd = 0;
for(int i=arrayList.size()-1;i>0;i--){
dd += arrayList.get(i)*(Math.pow(10,i));
}

if(dd>Math.pow(2,16)){
return 0;
}
int ri = (int)dd;
if(x<0){
return 0-ri;
}else{
return ri;
}
}
}
///Runtime Error
Runtime Error Message: Line 12: java.lang.ArithmeticException: / by zero
Last executed input: 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//逻辑混乱
public class Solution {
public long g(long xx,long g){
if(g==10){
return (xx%g);
}
if(xx<g){
return (xx-xx%(g/10))/(g/10);
}
return ((xx%g)-xx%(g/10))/(g/10);
}
public int reverse(int x) {
//几种特殊情况,100,反转去前导0;超过int范围的返回0。注意符号。
List<Long> arrayList = new ArrayList<Long>();
boolean zf = true;
if(x<0)zf=false;
if(x==0){
return 0;
}
long ss = 10;
double sss = 1;
if(x>Math.pow(2,31)-1||x<-Math.pow(2, 31)){
return 0;
}else if(x>-10&&x<10){
return x;
}
x=Math.abs(x);
for(;;){
//求余;
if(x<sss)break;
//int r = x%ss;
long r = g(x, ss);
arrayList.add(r);
ss=ss*10;
sss=sss*10;
}
double dd = 0;
for(int i=0,j=arrayList.size()-1;i<arrayList.size();i++,j--){
dd += arrayList.get(i)*(Math.pow(10,j));
}

if(dd>Math.pow(2,31)){
return 0;
}
int ri = (int)dd;
if(!zf){
return 0-ri;
}else{
return ri;
}


}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
//厉害答案。。。
public int reverse(int x) {
int ret = 0;
while (x != 0) {
// handle overflow/underflow
if (Math.abs(ret) > 214748364) {
return 0;
}
ret = ret * 10 + x % 10;
x /= 10;
}
return ret;
}

Same Tree

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//开始疏忽了其中一个节点为空的情况,val也忘了比较。。。
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null){
return true;
}else if(p==null||q==null){
return false;
}
if(p.val==q.val){
if(isSameTree(p.left,q.left)&&isSameTree(p.right,q.right)){
return true;
}else{
return false;
}
}else{
return false;
}
}
}

Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

找到子节点,返回最大深度值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

public class Solution {

public int maxDepth(TreeNode root) {

//初次提交忘记提交该判断。

if(root==null){

return 0;

}

return g(root,1);

}

//这里depth为该tn节点的深度,如果tn没子节点,则直接返回

public int g(TreeNode tn,int depth){

TreeNode leftNode = tn.left;

TreeNode rigthNode = tn.right;

int leftL = depth;

int rightL = depth;

if(leftNode!=null){

leftL = g(leftNode,depth+1);

}

if(rigthNode!=null){

rightL = g(rigthNode,depth+1);

}

return leftL>rightL?leftL:rightL;

}

}

Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//第一题,无脑超时
public class Solution {
public int singleNumber(int[] A) {
List<Integer> resultList = new ArrayList<Integer>();
List list = new ArrayList();
for(int i=0;i<A.length;i++){
Integer aiInteger = new Integer(A[i]);
if(list.contains(aiInteger)){
resultList.remove(aiInteger);
}else{
list.add(aiInteger);
resultList.add(aiInteger);
}
}
return resultList.get(0);
}
}
//查看别人算法后:按位异或,所有重复项抵消
public class Solution {
public int singleNumber(int[] A) {
int sum = 0;
for(int i = 0;i<A.length;i++){
sum^=A[i];
}
return sum;
}
}