农民代表
- 积分
- 5553
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-3-20
- 最后登录
- 1970-1-1
|
分享一个遇到的题目,没有遇到难题,算幸运,它家是不是越来越难了
public class TestSolution {
public static void main(String[] args) {
// int[] res = getPrimePair(6); ..
// System.out.println(res[0] + " " + res[1]);. 1point 3 acres
// res = getPrimePair(100);. 1point3acres
// System.out.println(res[0] + " " + res[1]);
// res = getPrimePair(23);
// System.out.println(res[0] + " " + res[1]);
ListNode first = new ListNode(1);
first.next = new ListNode(7);
first.next.next = new ListNode(9);
first.next.next.next = new ListNode(4);
first.next.next.next.next = new ListNode(6);
first.next.next.next.next.next = new ListNode(10);
first.next.next.next.next.next.next = new ListNode(2);
first.next.next.next.next.next.next.next = new ListNode(4);
first.next.next.next.next.next.next.next.next = new ListNode(6);
first.next.next.next.next.next.next.next.next.next = new ListNode(11);. .и
first.next.next.next.next.next.next.next.next.next.next = new ListNode(10);
. 1point 3 acres
ListNode second = new ListNode(9);
second.next = new ListNode(20);
second.next.next = new ListNode(2);
second.next.next.next = new ListNode(4);
second.next.next.next.next = new ListNode(20);
second.next.next.next.next.next = new ListNode(8);
second.next.next.next.next.next.next = new ListNode(12);. 1point 3acres
ListNode ret = longestMatching(first, second);
while (ret != null) {
System.out.println(ret.val);
ret = ret.next;. .и
}
} ..
private static int[] getPrimePair(int even) {.--
if (even%2 != 0) return new int[2];
//int startPrim = 2;
for (int startPrim = 2; startPrim <= even/2; startPrim++) {
if (!isPrim(startPrim)) continue;
int anotherPrim = even - startPrim;
if (isPrim(anotherPrim)) {
return new int[] {startPrim, anotherPrim};
}
}
return new int[2];
}
private static boolean isPrim(int x) {
int factor = 2;. From 1point 3acres bbs
while (factor <= x/2) {. 1point3acres.com
if (x%factor == 0) return false;
factor++;
}
return true;
}
// First linked list: 1->7->2->4->6->10->2->4->6->8->10
// Second linked list 9->20->2->4->6->8->12
//. .и
// Print the longest matching elements 2, 4, 6, 8
private static ListNode longestMatching(ListNode list1, ListNode list2) {
int maxLen = 0;
ListNode maxHead = null;
while (list1 != null) {
ListNode secondHead = list2;. Waral dи,
while (secondHead != null && secondHead.val != list1.val) {
secondHead = secondHead.next;. 1point3acres.com
}
ListNode firstHead = list1;
int len = getLongest(firstHead, secondHead);
if (maxLen < len) { ..
maxLen = len;.
maxHead = list1;
}
list1 = list1.next;
}. 1point3acres.com
ListNode ret = maxHead;
for (int i = 0; i < maxLen-1; i++) {
maxHead = maxHead.next;
}
maxHead.next = null;. 1point 3acres
return ret;.google и
}
private static int getLongest(ListNode first, ListNode second) {
int count = 0;
while (first != null && second != null && first.val == second.val) {
count++;
first = first.next;
second = second.next;
}
return count;
}
}
class ListNode {
int val;
ListNode next;
public ListNode() {}
public ListNode(int val) {-baidu 1point3acres
this.val = val;
}
}
|
|