codewars045: Twice linear

Instructions:

Twice linear

Solution:

import java.util.*;
import java.util.Map.Entry;

class DoubleLinear {
	private static Map<Integer, Boolean> map = new TreeMap<Integer, Boolean>();
	private static int[] arr = new int[100000];
	static {
		map.put(1, false);

		int count = 0;
		while (count < 100000) {
			for (Entry<Integer, Boolean> x : map.entrySet()) {
				if (!x.getValue()) {
					map.put(x.getKey() * 2 + 1, false);
					map.put(x.getKey() * 3 + 1, false);
					arr[count++] = x.getKey();
					map.remove(x.getKey());
					break;
				}
			}
		}
		Arrays.sort(arr);
	}

	public static int dblLinear(int n) {

		return arr[n];
	}
}

Example Test:

import static org.junit.Assert.*;
import org.junit.Test;


public class DoubleLinearTest {

    private static void testing(int actual, int expected) {
        assertEquals(expected, actual);
    }
 
    @Test
    public void test() {
        System.out.println("Fixed Tests dblLinear");
        testing(DoubleLinear.dblLinear(10), 22);
        testing(DoubleLinear.dblLinear(20), 57);
        testing(DoubleLinear.dblLinear(30), 91);
        testing(DoubleLinear.dblLinear(50), 175);
             
    }
}

转载于:https://my.oschina.net/u/553266/blog/817999

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值