ผลต่างระหว่างรุ่นของ "01204212/codes/zooma"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
(01204212/Zooma ถูกเปลี่ยนชื่อเป็น 01204212/codes/zooma)
 
(ไม่แสดง 2 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน)
แถว 74: แถว 74:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
public class ListNode {
 
public class ListNode {
public int val;
+
    public int val;
public ListNode next = null;
+
    public ListNode next = null;
 
 
public ListNode(int val) {
+
    public ListNode(int val) {
this.val = val;
+
        this.val = val;
this.next = null;
+
        his.next = null;
}
+
    }
 
 
public ListNode() {
+
    public ListNode() {
this(0);
+
        this(0);
}
+
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
แถว 95: แถว 95:
 
   
 
   
 
public class Main {
 
public class Main {
private int n,m;
+
    private int n,m;
private int[] c;
+
    private int[] c;
private int[] d;
+
    private int[] d;
private int[] p;
+
    private int[] p;
 
   
 
   
public static void main(String[] args) throws IOException {
+
    public static void main(String[] args) throws IOException {
Main main = new Main();
+
        Main main = new Main();
 
   
 
   
main.process();
+
        main.process();
}
+
    }
 
   
 
   
private void process() throws IOException {
+
    private void process() throws IOException {
    readInput();
+
        readInput();
   
+
       
    ListNode[] nodes = new ListNode[n + m + 1];
+
        ListNode[] nodes = new ListNode[n + m + 1];
   
+
       
    ListNode head = null;
+
        ListNode head = null;
    ListNode tail = null;
+
        ListNode tail = null;
   
+
       
    for(int i=0; i<n; i++) {
+
        for(int i=0; i<n; i++) {
    ListNode newNode = new ListNode(i+1);
+
            ListNode newNode = new ListNode(i+1);
    nodes[i+1] = newNode;  
+
            nodes[i+1] = newNode;  
   
+
           
    if(tail != null) {
+
            if(tail != null) {
    tail.next = newNode;
+
                tail.next = newNode;
    tail = newNode;
+
                tail = newNode;
    } else {
+
            } else {
    head = tail = newNode;
+
                head = tail = newNode;
    }
+
            }
    }
+
        }
   
+
       
    for(int i=0; i<m; i++) {
+
        for(int i=0; i<m; i++) {
    int ballNumber = n + i + 1;
+
            int ballNumber = n + i + 1;
    int pred = p[i];
+
            int pred = p[i];
   
+
           
    ListNode newNode = new ListNode(ballNumber);
+
            ListNode newNode = new ListNode(ballNumber);
    nodes[ballNumber] = newNode;
+
            nodes[ballNumber] = newNode;
   
+
           
    /* -----  this is the deleted code where we look for the node with val=pred
+
            /* -----  this is the deleted code where we look for the node with val=pred
    ListNode currentNode = head;
+
            ListNode currentNode = head;
    while(currentNode != null) {
+
            while(currentNode != null) {
    if(currentNode.val == pred) {
+
                if(currentNode.val == pred) {
    break;
+
                    break;
    }
+
                }
    currentNode = currentNode.next;
+
                currentNode = currentNode.next;
    }
+
            }
    */
+
            */
   
+
           
    ListNode currentNode = nodes[pred];
+
            ListNode currentNode = nodes[pred];
   
+
           
    newNode.next = currentNode.next;
+
            newNode.next = currentNode.next;
    currentNode.next = newNode;
+
            currentNode.next = newNode;
    }
+
        }
   
+
       
    ListNode currentNode = head;
+
        ListNode currentNode = head;
    while(currentNode != null) {
+
        while(currentNode != null) {
    System.out.println(currentNode.val);
+
            System.out.println(currentNode.val);
    currentNode = currentNode.next;
+
            currentNode = currentNode.next;
    }
+
        }
}
+
    }
 
   
 
   
private void readInput() throws IOException {
+
    private void readInput() throws IOException {
    BufferedReader reader = new BufferedReader(
+
        BufferedReader reader = new BufferedReader(
              new InputStreamReader(System.in) );
+
                  new InputStreamReader(System.in) );
 
   
 
   
    String[] items = reader.readLine().split(" ");  
+
        String[] items = reader.readLine().split(" ");  
n = Integer.parseInt(items[0]);
+
        n = Integer.parseInt(items[0]);
m = Integer.parseInt(items[1]);
+
        m = Integer.parseInt(items[1]);
 
   
 
   
c = new int[n];
+
        c = new int[n];
d = new int[m];
+
        d = new int[m];
p = new int[m];
+
        p = new int[m];
 
   
 
   
for(int i=0; i<n; i++) {
+
        for(int i=0; i<n; i++) {
c[i] = Integer.parseInt(reader.readLine());
+
            c[i] = Integer.parseInt(reader.readLine());
}
+
        }
 
   
 
   
for(int j=0; j<m; j++) {
+
        for(int j=0; j<m; j++) {
items = reader.readLine().split(" ");
+
            items = reader.readLine().split(" ");
d[j] = Integer.parseInt(items[0]);
+
            d[j] = Integer.parseInt(items[0]);
p[j] = Integer.parseInt(items[1]);
+
            p[j] = Integer.parseInt(items[1]);
}
+
        }
}
+
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

รุ่นแก้ไขปัจจุบันเมื่อ 23:28, 31 สิงหาคม 2559

Back to 01204212

First implemenation with arrays

FlexiArray.java

public class FlexiArray<T> {

	public interface ItemMatcher<T> {
		boolean isMatch(T item); 
	}
	
	private T[] items;
	private int itemCount;
	private int size;

	@SuppressWarnings("unchecked")
	public FlexiArray(int size) {
		this.items = (T[]) new Object[size];
		this.size = size;
		this.itemCount = 0;
	}
	
	public int findIndex(ItemMatcher<T> matcher) {
		for(int i=0; i<itemCount; i++) {
			if(matcher.isMatch(items[i])) {
				return i;
			}
		}
		return -1;
	}
	
	public void insert(int i, T item) {
		for(int j=itemCount - 1; j >= i; j--) {
			items[j + 1] = items[j];
		}
		items[i] = item;
		itemCount++;
	}

	public void remove(int i) {
		for(int j = i + 1; j < itemCount; j++) {
			items[j - 1] = items[j];
		}
		itemCount--;
	}
	
	public T get(int i) {
		return items[i];
	}
}

Examples of lambda expression

Using lambda expression:

			pLocation = outComments.findIndex((Comment c) -> (c.getId() == p));

Without lambda (anonymous class):

			pLocation = outComments.findIndex(new FlexiArray.ItemMatcher<Comment>() {
				public boolean isMatch(Comment c) {
					return c.getId() == p;
				}
			});

Zooma 1 solution with linked list (ListNode)

ListNode.java

public class ListNode {
    public int val;
    public ListNode next = null;
	
    public ListNode(int val) {
        this.val = val;
        his.next = null;
    }
	
    public ListNode() {
        this(0);
    }
}

Main.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
    private int n,m;
    private int[] c;
    private int[] d;
    private int[] p;
 
    public static void main(String[] args) throws IOException {
        Main main = new Main();
 
        main.process();
    }
 
    private void process() throws IOException {
        readInput();
        
        ListNode[] nodes = new ListNode[n + m + 1];
        
        ListNode head = null;
        ListNode tail = null;
        
        for(int i=0; i<n; i++) {
            ListNode newNode = new ListNode(i+1);
            nodes[i+1] = newNode; 
            
            if(tail != null) {
                tail.next = newNode;
                tail = newNode;
            } else {
                head = tail = newNode;
            }
        }
        
        for(int i=0; i<m; i++) {
            int ballNumber = n + i + 1;
            int pred = p[i];
            
            ListNode newNode = new ListNode(ballNumber);
            nodes[ballNumber] = newNode;
            
            /* -----  this is the deleted code where we look for the node with val=pred
            ListNode currentNode = head;
            while(currentNode != null) {
                if(currentNode.val == pred) {
                    break;
                }
                currentNode = currentNode.next;
            }
            */
            
            ListNode currentNode = nodes[pred];
            
            newNode.next = currentNode.next;
            currentNode.next = newNode;
        }
        
        ListNode currentNode = head;
        while(currentNode != null) {
            System.out.println(currentNode.val);
            currentNode = currentNode.next;
        }
    }
 
    private void readInput() throws IOException {
        BufferedReader reader = new BufferedReader(
                   new InputStreamReader(System.in) );
 
        String[] items = reader.readLine().split(" "); 
        n = Integer.parseInt(items[0]);
        m = Integer.parseInt(items[1]);
 
        c = new int[n];
        d = new int[m];
        p = new int[m];
 
        for(int i=0; i<n; i++) {
            c[i] = Integer.parseInt(reader.readLine());
        }
 
        for(int j=0; j<m; j++) {
            items = reader.readLine().split(" ");
            d[j] = Integer.parseInt(items[0]);
            p[j] = Integer.parseInt(items[1]);
        }
    }
}