🔥 一、获取硬盘信息
🍎获取硬盘的分区信息
package day05.first;
//获取硬盘的分区信息
import java.io.File;
import java.text.DecimalFormat;
public class Solution {
public static void main(String[] args) {
File[] roots = File.listRoots();
for (File file: roots) {
System.out.println(file.getPath() + "信息如下");
long free = file.getFreeSpace();
long total = file.getTotalSpace();
long user = total - free ;
System.out.println("空闲未使用 = " + change(free) + "G");
System.out.println("已经使用 = " + change(user) + "G");
System.out.println("总容量 =" + change(total) + "G");
System.out.println("使用百分比 = " + bfb (user , total));
}
}
private static String bfb(Object num1, Object num2) {
double f1 = Double.valueOf(num1.toString());
double f2 = Double.valueOf(num2.toString());
if(f2 == 0){
return "0.0%";
}else{
DecimalFormat df = new DecimalFormat("#0.00");
return df.format((f1 / f2)/100) + "%";
}
}
private static long change(long free) {
//free的单位GB
//除第一个1024 ---> MB
//除第二个1024 ---> KB
//除第三个1024 ---> B
return free/1024/1024/1024 ;
}
}
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
🔥 二、二进制转换
🍎二进制数求和
🍎给定两个二进制字符串,返回它们的和(用二进制表示)
🍎输入为非空字符串且只包含数字1和0
思路:判断两个字符串长度是否相等
接着使用标志变量flag 标志两个字符相加是否为1 或者是 0
进位为1 不进位为0
package day05.second;
public class Solution {
public static void main(String[] args) {
String a = "111";
String b = "1";
System.out.println( addBinary(a , b)
);
}
public static String addBinary(String a , String b ){
//100
// 001
StringBuffer stringBuffer1 = new StringBuffer(a);
stringBuffer1.reverse();
//1000
// 0001
StringBuffer stringBuffer2 = new StringBuffer(b);
stringBuffer2.reverse();
if(stringBuffer1.length() > stringBuffer2.length()){
int n = stringBuffer1.length() - stringBuffer2.length() ;
for (int i = 0; i < n; i++) {
stringBuffer2.append('0');
}
}else if (stringBuffer1.length() < stringBuffer2.length()){
int n = stringBuffer2.length() - stringBuffer1.length();
for (int i = 0; i < n; i++) {
stringBuffer1.append('0');
}
}
StringBuffer stringBuffer = new StringBuffer("");
int i = 0 ;
char flag = '0';
while (i < stringBuffer1.length() && i<stringBuffer2.length()){
if(flag == '0') {
if (stringBuffer1.charAt(i) == stringBuffer2.charAt(i) && stringBuffer1.charAt(i) == '1') {
flag = '1';
stringBuffer.append('0');
} else if (stringBuffer1.charAt(i) == stringBuffer2.charAt(i) && stringBuffer1.charAt(i) == '0') {
stringBuffer.append('0');
} else {
stringBuffer.append('1');
}
}else{
if(stringBuffer1.charAt(i) == stringBuffer2.charAt(i) && stringBuffer1.charAt(i) == '1') {
flag = '1';
stringBuffer.append('1');
}else if (stringBuffer1.charAt(i) == stringBuffer2.charAt(i) && stringBuffer1.charAt(i) == '0') {
flag = '0';
stringBuffer.append('1');
}else{
flag = '1';
stringBuffer.append('0');
}
}
i++ ;
}
if(flag == '1'){
stringBuffer.append(flag);
}
stringBuffer.reverse();
return stringBuffer.toString();
}
}
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
🔥 三、螺旋矩阵2
给你一个正整数n,生成一个包含1到n的平方所有元素,且元素按顺时针顺序螺旋排序的n*n正方形martrix.
输入n = 3 输出: [[1,2,3] , [8,9,4], [7,6,5]]输入n = 1 输出: [[1]]
package day05.third;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
System.out.println(Arrays.deepToString(generateMatrix(3)));
}
public static int [][] generateMatrix (int n){
int [] [] res = new int[n][n];
if(n <= 0 ){
return res;
}
int left = 0 ;
int right = n - 1 ;
int up = 0 ;
int down = n - 1 ;
int i = 1 ;
int col = 0 ;
int row = 0 ;
while (i < n*n ){
//从左到右
for ( col = col ; col <= right ; col++) {
res[row][col] = i++;
}
// 3
col -- ; // 2
// up = 0
//从上到下
for ( row = row + 1 ; row <= down; row++) {
res[row][col] = i++;
}
row -- ;
for (col = col - 1 ; col >= left ; col--) {
res[row][col] = i++ ;
}
col ++ ;
for (row = row - 1 ; row > up ; row -- ) {
res[row][col] = i ++ ;
}
col ++ ;
row ++ ;
res[row][col] = i ;
}
return res;
}
}