Jerry's Blog

Recording what I learned everyday

View on GitHub


3 August 2019

Database(1) -- VM and Install CentOS

by Jerry Zhang

LeetCode Day 29: P191. Number of 1 Bits (Easy)

题目:

给一个无符号整数,求它的二进制表示的反转数的十进制整数。

Input: 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.

我的思路:

因为所有整数都是4个字节,所以就有32位。for循环32次,每次向右移i位,再跟1做“与”运算。

我的代码:

public class E_191_NumberOf1Bits {
    public int hammingWeight(int n) {
        int counter = 0;
        for (int i = 0; i < 32; i++) {
            if ((n >> i & 1) == 1) {
                counter++;
            }
        }
        return counter;
    }
}

只超过45%

最优解

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int count = 0;
        while(n != 0){
            count = count + (n & 1);
            n = n >>> 1;
        }
        return count;
    }
}

>>> 是无符号右移,忽略符号位,空位都以0补齐。但因为本题中全是正数,所以就无所谓了。都是一样的。

答案的效率高,原因是并不一定要做满32次。只要n等于0了就结束。因为很多整数的二进制表示其实前面0非常多,所以这样算的次数就远远比我少。

附上做的几个位运算的实验:

public class E_191_NumberOf1Bits {
    public static void main(String[] args) {
        int a = 2;
        a = a >> 1;
        String s = Integer.toBinaryString(a);
        System.out.println("a = " + a);
        System.out.println("s = " + s);
        int b = -4;
        b = b >>> 1;
        System.out.println("b = " + b);
        String s1 = Integer.toBinaryString(b);
        System.out.println("s1 = " + s1);
    }
}

Database:

新零售数据库设计

前置课:《与MySQL的零距离接触》

安装VMware Workstation

下载CentOS镜像文件(.iso)

新建虚拟机:典型->稍后安装->Linux CentOS 7->4核CPU->虚拟化Intel vt-x/EPT才能用Docker->桥接模式才有独立的IP地址

SSH

MobaXterm

ip addr 可以看到虚拟机的ip地址

在MobaXterm里点Session,输入ip地址,就可以建立SSH连接远程控制了。

Linux基础知识

常用目录

常用命令

a = show processes for all users
u = display the process's user/owner
x = also show processes not attached to a terminal
tags: Database, - Linux