leetcode #86: Partition List

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode a = new ListNode(0), b = new ListNode(0), tmpA = a, tmpB = b;
        while (head != null) {
            if (head.val < x) {
                tmpA.next = head;
                tmpA = tmpA.next;
            } else {
                tmpB.next = head;
                tmpB = tmpB.next;
            }
            head = head.next;
        }
        tmpB.next = null;
        tmpA.next = b.next;
        return a.next;
    }
}

leetcode #448: Find All Numbers Disappeared in an Array

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            while (nums[i] - 1 != i && nums[nums[i] - 1] != nums[i]) {
                int v = nums[i];
                nums[i] = nums[v - 1];
                nums[v - 1] = v;
            }
        }

        for (int k = 0; k < nums.length; k++) {
            if (nums[k] != k + 1) result.add(k + 1);
        }
        return result;
    }
}

leetcode #442: Find All Duplicates in an Array

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            while (nums[i] - 1 != i && nums[nums[i] - 1] != nums[i]) {
                int v = nums[i];
                nums[i] = nums[v - 1];
                nums[v - 1] = v;
            }
        }

        for (int k = 0; k < nums.length; k++) {
            if (nums[k] != k + 1) result.add(nums[k]);
        }
        return result;
    }
}

leetcode #149: Max Points on a Line

/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */

public class Solution {
    public int maxPoints(Point[] points) {
        if (points.length <= 1) return points.length;

        int result = 0;
        for (int i = 0; i < points.length; i++) {
            Point I = points[i];
            for (int j = 0; j < points.length; j++) {
                if (j == i) continue;
                Point J = points[j];
                int max = 0;
                for (int k = j + 1; k < points.length; k++) {
                    if (k == i) continue;
                    Point K = points[k];
                    if (J.x == I.x) {
                        if (I.x == K.x) max++;
                    } else {
                        if ((K.y - I.y) * (J.x - I.x) == (J.y - I.y) * (K.x - I.x)) max++;
                    }
                }
                if (max > result) result = max;
            }
        }
        return result + 2;
    }
}

leetcode #279: Perfect Squares

public class Solution {
    public int numSquares(int n) {
        int table[] = new int[n + 1];
        for (int i = 1; i < table.length; i++) {
            int j = 1, min = i;
            while (j * j <= i) {
                min = Math.min(table[i - j * j] + 1, min);
                j++;
            }
            table[i] = min;
        }
        return table[n];
    }
}

leetcode #29: Divide Two Integers

public class Solution {
    public int divide(int dividend, int divisor) {
        if (divisor == 0) return 0;

        long a = Math.abs((long) dividend), b = Math.abs((long) divisor), result = 0;
        while (b <= a) {
            long cur = b, base = 1;
            while ((cur << 1) < a) {
                cur <<= 1;
                base <<= 1;
            }
            result += base;
            a -= cur;
        }

        if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) result = -result;
        if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) return Integer.MAX_VALUE;
        return (int) result;
    }
}

urionlinejudge #1516: Image

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter stdout = new PrintWriter(System.out);

        String line;
        while ((line = stdin.readLine()) != null && !line.equals("0 0")) {
            int originHeight = Integer.parseInt(line.substring(0, line.indexOf(' ')));
            int originWidth = Integer.parseInt(line.substring(line.indexOf(' ') + 1));
            String[] tmp = new String[originHeight];
            for (int i = 0; i < originHeight; i++) tmp[i] = stdin.readLine();
            String scale = stdin.readLine();
            int newHeight = Integer.parseInt(scale.substring(0, scale.indexOf(' '))), cHeight = newHeight / originHeight;
            int newWidth = Integer.parseInt(scale.substring(scale.indexOf(' ') + 1)), cWidth = newWidth / originWidth;
            String[] result = new String[originHeight];
            for (int i = 0; i < tmp.length; i++) {
                StringBuilder stringBuilder = new StringBuilder();
                for (int j = 0; j < tmp[i].length(); j++) {
                    for (int k = 0; k < cWidth; k++) stringBuilder.append(tmp[i].charAt(j));
                }
                result[i] = stringBuilder.toString();
            }

            for (String l : result) {
                for (int n = 0; n < cHeight; n++) stdout.println(l);
            }
            stdout.println("");
        }
        stdout.close();
    }
}

urionlinejudge #2149: The Uncle Phill Bonati’s Sequence

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter stdout = new PrintWriter(System.out);

        long[] table = new long[]{0, 1, 1, 1, 2, 2, 4, 8, 12, 96, 108, 10368, 10476,
                108615168, 108625644, 11798392572168192L, 11798392680793836L};

        String line;
        while ((line = stdin.readLine()) != null && line.length() != 0) {
            stdout.println(table[Integer.parseInt(line) - 1]);
        }
        stdout.close();
    }
}

urionlinejudge #1761: Christmas Decorations

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter stdout = new PrintWriter(System.out);

        String line;
        while ((line = stdin.readLine()) != null && line.length() != 0) {
            String[] box = line.split(" ");
            double a = Double.parseDouble(box[0]);
            double b = Double.parseDouble(box[1]);
            double c = Double.parseDouble(box[2]);
            stdout.printf("%.2f\n", (b * Math.tan(a / 180 * 3.141592654) + c) * 5);
        }
        stdout.close();
    }
}

leetcode #477: Total Hamming Distance

public class Solution {
    public int totalHammingDistance(int[] nums) {
        int result = 0, len = nums.length;
        for (int i = 0; i < 32; i++) {
            int odd = 0;
            for (int k = 0; k < len; k++) {
                if ((nums[k] & 1) == 1) odd++;
                nums[k] >>= 1;
            }
            result += odd * (len - odd);
        }
        return result;
    }
}