-
第 21 楼 / webdriver
- 时间: 2014-9-28 21:36算法问题推荐解法来了...
merge sort.
解法(Java)
代码:
public class SortList {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode fast = head;
ListNode slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
fast = slow.next;
slow.next = null;
slow = sortList(head);
fast = sortList(fast);
return merge(slow, fast);
}
private ListNode merge(ListNode slow, ListNode fast) {
ListNode head = new ListNode(0);
ListNode cur = head;
while (slow != null && fast != null) {
if (slow.val < fast.val) {
cur.next = slow;
slow = slow.next;
} else {
cur.next = fast;
fast = fast.next;
}
cur = cur.next;
}
if (slow != null) {
cur.next = slow;
} else if (fast != null) {
cur.next = fast;
}
return head.next;
}
}
-
-
第 22 楼 / webdriver
- 时间: 2014-9-28 21:39
-
-
第 23 楼 / webdriver
- 时间: 2014-9-28 21:40
-
第 24 楼 / webdriver
- 时间: 2014-9-28 21:41
-
第 25 楼 / webdriver
- 时间: 2014-9-28 21:42算法问题推荐解法来了...
1. Binary search in range [0, x / 2 + 1].
2. Newton iteration method. x(i+1) = (x(i) + n/x(i)) / 2.
See my blog (http://www.cnblogs.com/AnnieKim/archive/2013/04/18/3028607.html) for more explanation (in Chinese).
解法(Java)
代码:
public class Sqrtx {
public int sqrt(int x) {
if (x == 0 || x == 1) return x;
long low = 1;
long high = x;
long mid = 0;
while (low <= high) {
mid = (low + high) / 2;
if (mid * mid <= x && (mid + 1) * (mid + 1) > x) {
break;
}
if (mid * mid > x) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return new Long(mid).intValue();
}
} -
第 26 楼 / webdriver
- 时间: 2014-9-28 21:44S算法问题: String to Integer (atoi)
问题描述:
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not
see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs).
You are responsible to gather all the input requirements up front.
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first
non-whitespace character is found. Then, starting from this character, takes an optional
initial plus or minus sign followed by as many numerical digits as possible, and interprets
them as a numerical value.
The string can contain additional characters after those that form the integral number, which
are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or
if no such sequence exists because either str is empty or it contains only whitespace characters,
no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out
of the range of representable values, INT_MAX (2147483647) or INT_MIN (-214748364is returned.
-
第 27 楼 / webdriver
- 时间: 2014-9-28 21:45算法问题推荐解法来了...
...
解法(Java)
代码:
public class StringtoIntegerAtoi {
public int atoi(String str) {
str = str.trim();
int length = str.length();
if (length == 0)
return 0;
int i = 0;
boolean minus = false;
if (str.charAt(0) == '-') {
minus = true;
i++;
} else if (str.charAt(0) == '+') {
i++;
}
long MIN_VALUE = Integer.MIN_VALUE;
long MAX_VALUE = Integer.MAX_VALUE;
long num = 0;
boolean finished = false;
for (; i < length && !finished; i++) {
char c = str.charAt(i);
if (c >= '0' && c <= '9') {
num *= 10;
num += c - '0';
} else {
finished = true;
}
if (minus && 0 - num < MIN_VALUE) {
return Integer.MIN_VALUE;
}
if (!minus && num > MAX_VALUE) {
return Integer.MAX_VALUE;
}
}
return minus ? new Long(0 - num).intValue() : new Long(num).intValue();
}
} -
第 28 楼 / webdriver
- 时间: 2014-9-28 21:47S算法问题: Subsets II
问题描述:
Given a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
] -
第 29 楼 / webdriver
- 时间: 2014-9-28 21:49算法问题推荐解法来了...
..Similar to Subset I, except for line 45.
解法(Java)
代码: import java.util.ArrayList;
import java.util.Arrays;
public class SubsetsII {
public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> lastLevel = null;
ret.add(new ArrayList<Integer>());
Arrays.sort(num);
for (int i = 0; i < num.length; i++) {
ArrayList<ArrayList<Integer>> tmp = new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> prev = i == 0 || num[i] != num[i - 1] ? ret : lastLevel;
for (ArrayList<Integer> s : prev) {
ArrayList<Integer> newSet = new ArrayList<Integer>(s);
newSet.add(num[i]);
tmp.add(newSet);
}
ret.addAll(tmp);
lastLevel = tmp;
}
return ret;
}
} -
第 30 楼 / webdriver
- 时间: 2014-9-28 21:52S算法问题: Subsets
问题描述:
Given a set of distinct integers, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]