leetcoder

leetcoder
2019-09-24 03:32

class Solution {

public int n;
public boolean[] st;
List<List<Integer>> ans = new ArrayList<>();

public List<List<Integer>> permute(int[] nums) {
n = nums.length;
st = new boolean[n];
dfs(nums, 0);

return ans;
}

public void dfs(int[] nums, int u) {
if (u == n) {
return;
}

for (int i = 0; i < n; i++) {
if (st[i] == false) {
st[i] = true;
dfs(nums, u + 1);
path.removeLast();
st[i] = false;
}
}

}
}


leetcoder
2019-08-31 09:33

int a = i == x ? 0 : atoi(s1.substring(i, x - i).c_str()); 中 c_Str()函数的作用是什么？

public int compareVersion(String s1, String s2) {
int i = 0;
int j = 0;
while (i < s1.length() || j < s2.length()) {
int x = i, y = j;
while (x < s1.length() && s1.charAt(x) != '.')
x++;
while (y < s2.length() && s2.charAt(y) != '.')
y++;
int a = i == x ? 0 : Integer.valueOf(s1.substring(i, x - i));
int b = j == y ? 0 : Integer.valueOf(s2.substring(j, y - j));
if (a > b)
return 1;
if (a < b)
return -1;
i = x + 1;
j = y + 1;
}
return 0;
}


✘ testcase: ‘“1.0”\n”1.1”’
bug处：

int a = i == x ? 0 : Integer.valueOf(s1.substring(i, x - i));

Line 18: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

leetcoder
2019-08-26 06:01

 public ListNode reverseBetween(ListNode head, int m, int n) {

ListNode dummy = new ListNode(0);

ListNode start_pre = dummy, end = dummy;
for (int i = 0; i < m - 1; i++)
start_pre = start_pre.next;
for (int i = 0; i < n; i++)
end = end.next;

ListNode start = start_pre.next;
ListNode end_last = end.next;
for (ListNode a = start, b = start.next; b != end_last;) {
ListNode c = b.next;
b.next = a;
a = b;
b = c;
}

start_pre = end;
start = end_last;
return dummy.next;

}