文档章节

codewars045: Twice linear

圣洁之子
 圣洁之子
发布于 2017/01/03 17:03
字数 123
阅读 209
收藏 0

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);
             
    }
}

© 著作权归作者所有

圣洁之子
粉丝 10
博文 402
码字总数 124050
作品 0
深圳
后端工程师
私信 提问
leetcode- Single Number

Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement i......

thoresa
2015/11/07
5
0
只出现一次的数字

原题   Given an array of integers, every element appears twice except for one. Find that single one.   Note:   Your algorithm should have a linear runtime complexity. Cou......

一贱书生
2016/12/23
2
0
Leetcode|136. Single Number

【问题描述】 Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity.......

darlingwood2013
2018/05/15
0
0
SingleNumber136 leetCode

Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you im......

woshixin
2018/10/17
5
0
LeetCode 136. Single Number (独数)

原题 Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could y......

dby_freedom
2018/12/06
0
0

没有更多内容

加载失败,请刷新页面

加载更多

python学习10.04:Python list列表使用技巧及注意事项

前面章节介绍了很多关于 list 列表的操作函数,细心的读者可能会发现,有很多操作函数的功能非常相似。例如,增加元素功能的函数有 append() 和 extend(),删除元素功能的有 clear()、 remo...

太空堡垒185
12分钟前
2
0
新手插画学习的方法?教你如何自学?

插画学习的方法?教你如何自学? 从小喜欢画一些漫画头像随笔画,但是其实没有基础。个人偏好小清新手绘风的插画(如下图),每每看到都希望自己能画出这样的作品。 我其实很想说画这种美术功...

huihuajiaocheng
17分钟前
3
0
面试题

1、实现clone();

gtandsn
28分钟前
3
0
CentOS 7 部署 tesseract-ocr

官方地址 github yum-config-manager --add-repo https://download.opensuse.org/repositories/home:/Alexander_Pozdnyakov/CentOS_7/ 若提示 yum-config-manager: command not found 执行以......

阿白
29分钟前
2
0
JAVA比较器中comparator的使用

一个专用的比较器Comparator Comparator是一个专用的比较器,当一个不支持自比较或者自比较函数不能满足要求时,可写一个比较器来完成两个对象之间大小的比较。Comparator体现了一种策略模式...

daxiongdi
30分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部