Problem
最近也写了很多代码,但总感觉自己好像毫无长进,又学了很多。
How to go on it?
I don’t know how to treat my the code had writen before and how to optimize it.
Some of them is very terrible, especially the service works. It’s so lengthy and
tedious, maybe I need to trim its format and make it more concise.
代码有自己的理解,怎么去理解代码?经常会想这个问题,而很简单的业务,却
代码实现是又臭又长,xxxxxx。
像理解数学一样理解代码。
写了这个大学写过的一段代码,感觉现在的理解和以前完全不一样。
‘
代码中注释很多,不描述
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
/**
* @Author liubh
* @Email lbhbinhao@163.com
*/
public class Fibonacci {
public static void main(String[] args) {
// for (int i=1;i<=10;i++){
// System.out.print(Fibo1(i)+" ");
// }
// boolean aaad = isParlintromeString("aaad0aaa");
// System.out.println(aaad);
ArrayList<Integer> integers = new ArrayList<>();
integers.add(50);
integers.add(30);
integers.add(62);
integers.add(100);
integers.add(12);
integers.add(30);
integers.add(69);
quickSort(integers);
for (int i:integers){
System.out.println(i);
}
}
//代码理解,num应该理解成Fn中的n
//int Fn = Fn-1 +Fn-2
//变式
public static int Fibo(int num){
if (num == 1|num == 2){
return 2;
}
return Fibo(num-1)+Fibo(num-2);
}
//变形
//假设Fn = F1+F2+......+Fn-1该怎么写
public static int Fibo1(int num){
if (num == 1|num == 2){
return 1;
}
int result = 0;
while (num>1){
result += Fibo1(num-1);
num--;
}
return result;
}
/**
* text[1] == text
* String Palindrome
* @Author liubh
* 还有其他办法,最容易理解就是反转text,比较反序后的text和原text是否相同
*/
public static boolean isParlintromeString(String text){
int index = text.length()-1;
int i=0;
while (i<index)
{
if (text.charAt(i)!=text.charAt(index)){
return false;
}
i++;
index--;
}
return true;
}
/**
* Avoid deadlock
* Solution, Resource has an order, if you want to access Resource r2, you
* need to got r1 first, in this way, only one thread can get resouce.
*/
/**
* Remove duplicate item form an array
* @return
*/
public static <T> LinkedHashSet<T> removeDup(List<T> list){
LinkedHashSet<T> result = new LinkedHashSet<>(list);
return result;
}
/**
* quickSort
*/
public void swap(int i,int j,List<Integer> list){
Integer temp = list.get(i);
list.set(i,list.get(j));
list.set(j,temp);
}
/**
* How to solve the problem that Java Obj invoked a method that called my reference
* ????? why???
* @param list
*/
public static void quickSort(List<Integer> list){
int right = list.size()-1;
int high = right;
int left = 0;
int low = 0;
int middle = list.get(list.size()/2);
while (right<left){
while (list.get(left)<middle){
left++;
}
while (list.get(right)>middle){
right--;
}
if (low<right){
quickSort(list.subList(low,right));
}
if (high>left){
quickSort(list.subList(left,high));
}
}
}
}
来源:CSDN
作者:Lsilly
链接:https://blog.csdn.net/weixin_44671737/article/details/104056392