头像

leetcoder


访客:984

离线:1个月前



leetcoder
2019-09-24 03:32

题目链接 LeetCode 46

我遇到了Java 答案元素为空的问题。

错误的代码:

class Solution {

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

    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) {
            ans.add(path);
            return;
        }

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

    }
}

编译器报了什么错误?Compile Error? Runtime Error?
编译正常 但是得到的答案元素都为空
屏幕快照 2019-09-23 下午8.31.18.png




leetcoder
2019-08-31 09:33

题目链接 LeetCode 165

用java改写直播中的C代码 部分 case 无法通过
原C
代码:

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

题目链接 LeetCode 92

将Y总的 92题 C++ 解法改成 Java 版 提交后遇到 Memory Limit Exceed(超出内存限制)问题。

错误的代码:


 public ListNode reverseBetween(ListNode head, int m, int n) {
    if(m==n) return head;

    ListNode dummy = new ListNode(0);
    dummy.next = head;

    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;

}