十进制转二进制
for(int i = 31;i >=0; i--){
System.out.print((num & (1 << 31)) == 0 ? "0" : "1");
}
求1!+2!+3!+…+N!
方法1
public static long functionA(int n){
long sum = 0;
for(int i = 1; i <= n; i++){
sum += functionA1(i);
}
return sum;
}
public static long functionA1(int n){
long ans = 1;
for(int i = 1;i <= n;i++){
ans *= i
}
return ans;
}
方法2
public static long functionB(int n){
long sum = 0;
long ans = 1;
for(int i = 1; i <= n; i++){
ans *= i;
sum += ans;
}
}
选择排序
public static void selectionSort(int[] arr){
if(arr == null || arr.length < 2){
return;
}
int n = arr.length;
for(int i = 0; i < n-1; i++){
int minValueIndex = i;
for(j = i+1; j < n; j++){
minValueIndex = arr[j] < arr[minValueIndex] ? j : minValueIndex;
}
int temp = arr[j];
arr[j] = arr[minValueIndex];
arr[minValueIndex] = temp;
}
}
冒泡排序
public static void bubbleSort(int[] arr){
if(arr = null || arr.length < 2){
return;
}
int n = arr.length;
for(int end = n-1; end >= 0; end--){
for(int second = 1; second <= end; second++){
if(arr[second] < arr[second - 1]){
int temp = arr[second];
arr[second] = arr[second - 1];
arr[second - 1] = temp;
}
}
}
}
插入排序
public static void insertSort(int[] arr){
if(arr == null || arr.length < 2){
return;
}
int n = arr.length;
for(int end = 1; end < n; end++){
int newValueIndex = end;
while(newValueIndex - 1 >= 0 && arr[newVlueIndex - 1] > arr[newVlueIndex]){
int temp = arr[newVlueIndex];
arr[newVlueIndex] = arr[newVlueIndex - 1]
arr[newVlueIndex - 1] = temp;
newValueIndex--;
}
}
}
插入排序优化
public static void insertSort(int[] arr){
if(arr == null || arr.length < 2){
return;
}
int n = arr.length;
for(int end = 1; end < n; end++){
for(int pre = end -1; pre - 1 >= 0 && arr[pre -1] > arr[pre]; pre++)
int temp = arr[pre];
arr[pre] = arr[pre - 1]
arr[pre - 1] = temp;
}
}
思路看懂了但是写不出来代码是什么原因???