01204212/codes/zooma

จาก Theory Wiki
(เปลี่ยนทางจาก 01204212/Zooma)
ไปยังการนำทาง ไปยังการค้นหา
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]);
        }
    }
}