codewars045: Twice linear 原

圣洁之子

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

}
}
``````

圣洁之子

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

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 (独数)

dby_freedom
2018/12/06
0
0

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

12分钟前
2
0

huihuajiaocheng
17分钟前
3
0

1、实现clone();

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

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

daxiongdi
30分钟前
2
0