z******e 发帖数: 82 | 1 -_-!!, what bad test skill I have.
for this test case, change
if (c == '"' && !slash2 && !slashstar) {
to
if (c == '"' && lastc != '\' && !slash2 && !slashstar) {
complete code:
private static String uncomment(String str) {
boolean slash2 = false;
boolean inStr = false;
boolean slashstar = false;
StringBuilder sb = new StringBuilder();
char lastc = ' ';
char c = ' ';
int deleteStart = -1;
for (int i = 0; i < str.length(); i++) {
... 阅读全帖 |
|
z******e 发帖数: 82 | 2 new test cases:
--------------------
char str[] = "\\"; // test \ inside "
char str[] = "\\";
char str[] = '\"'; // test \ inside "
char str[] = '\"';
code:
---------------------------
private static String uncomment(String str) {
boolean slash2 = false;
boolean inStr = false;
boolean slashstar = false;
boolean escape = false;
StringBuilder sb = new StringBuilder();
char lastc = ' ';
char c = ' ';
int deleteStart = -1;
... 阅读全帖 |
|
z******e 发帖数: 82 | 3 多谢大牛的test case
private static String uncomment(String str) {
boolean slash2 = false;
boolean inStr = false;
boolean slashstar = false;
StringBuilder sb = new StringBuilder();
char lastc = ' ';
char c = ' ';
int deleteStart = -1;
for (int i = 0; i < str.length(); i++) {
lastc = c;
c = str.charAt(i);
sb.append(c);
// ""
if (c == '"' && !slash2 && !slashstar) {
... 阅读全帖 |
|
z******e 发帖数: 82 | 4 // " /*dfsfsdf*/ //fdsfs " is processed.
private static String uncomment(String str) {
boolean slash2 = false;
boolean inStr = false;
boolean slashstar = false;
StringBuilder sb = new StringBuilder();
char lastc = ' ';
char c = ' ';
int deleteStart = -1;
for (int i = 0; i < str.length(); i++) {
lastc = c;
c = str.charAt(i);
sb.append(c);
// ""
if (c == '"') {
inStr = !inStr;
continue;
}
if (!inStr && c == '\n' && slash2) {
slash2 = false;
sb.delete(deleteStart, sb.length() - 1);
deleteStart = -1... 阅读全帖 |
|
t****a 发帖数: 1212 | 5 1、最简单办法是穷举, choose(m, n),但计算量太大
2、改进的办法是搜索,也就是给出coin集合的情况下,搜索另一个更好的coin集合。
可以一步步去搜索,前提是本题的解要满足局部最优解=全局最优解的条件(我还不会
证明这一点)。这个方法的计算复杂度已经大大低于1
3、更好的的办法是迭代法,通过n-1个coin的解来算n个coin的解
4、可能存在某个解析的方法(比如写出方程式,求偏导数=0,解方程组)一步计算出
所有的coin取值。方程组可以写出,但由于存在coin的取值全部为正整数的约束条件,
偏倒数=0无法得到正整数解。
呼唤高人来给出3、4的解法。
----------------------------------------
推不出策略3的解,所以我去尝试策略2:
首先要猜一个不错的初始取值,
然后在它的基础上进行搜索。
我猜的解是:
C_n = (M^(n-1))^(1/n)
C_{n-1} = (M^(n-2))^(1/(n-1))
...
C_1 = (C_2^0)^1 = 1
clojure code如下 (不包含搜索的部分)
(use 'clojure... 阅读全帖 |
|
|