hw3打卡
阿新 • • 發佈:2017-07-19
出錯 數組 pre length ima slist alt splay win
PART I
smoosh多用幾個循環就可以了,創建一個新的數組用於臨時存儲。
public static void smoosh(int[] ints) { int[] reads= new int[ints.length]; for (int i=0;i<ints.length;i++) { reads[i]=-1; } reads[0]=ints[0]; int j=1; for (int i=1;i<ints.length;i++) { if(ints[i-1]!=ints[i]) { reads[j]View Code=ints[i]; j++; } } for(int i=0;i<ints.length;i++) { ints[i]=reads[i]; } }
PART II
之前沒有加size>0的判斷條件,結果最後一項list9的squish總是報錯,因為它是空的,head本身就是null,不存在null.next,所以有currentNode.next的判斷一定會出錯。
public void squish() { SListNode currentNode=head;View Codeif(size>0) { while(currentNode.next!=null) { if(currentNode.item.equals(currentNode.next.item)) { currentNode.next=currentNode.next.next; size--;} else currentNode=currentNode.next; } } }
PART III
publicView Codevoid twin() { SListNode currentNode=head; while(currentNode!=null) { currentNode.next=new SListNode(currentNode.item,currentNode.next);; currentNode=currentNode.next.next; size=size*2; } }
hw3打卡