Saturday, January 3, 2015

Java弱弱请救几个小问题

发信人: mywater (梦), 信区: Programming
标  题: Java弱弱请救几个小问题
发信站: BBS 未名空间站 (Sat Jan  3 16:53:50 2015, 美东)

我是最近刚转Java的弱弱,有几个小问题,请牛牛们轻拍。

这次转java,总算像是某位说的那样,原来c++转java也就分分钟的事儿。写得顺的时
候,边google边写,也可以写得稀里糊涂、腾云架雾、行云流水;可问题是bug一出现
,立马歇菜!比如,min stack, stack1.peek()==stack2.peek(),顶上的两个数字明明
是相等的,为什么会return false呢?想去google都不知道该搜什么。。。等把java刷
一遍lc就算我可以写java了?

我有个function, public boolean dfdjdf(){return dfkld;} 为什么它一定要我在最
后一句话加个return啊,方法里的逻辑到那一步早就应该已经return了啊?

还有就是,刷lc搭了个local的架,本来没什么心理障碍,可是每遇到有Node,
ListNode, TreeNode的题就一股无名火,我不知道该把这些定义的类塞到哪里?试过两
种:

public class getIntersectionNode {
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public static class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            if (headA == null || headB == null) {
                return null;
            }
           
            ListNode p1 = headA;
            ListNode p2 = headB;
            while (p1 != null && p2 != null && p1 != p2) {
                p1 = p1.next;
                p2 = p2.next;
                if (p1 == p2) return p1;
                if (p1 == null) {
                    p1 = headB;
                }
                if (p2 == null) {
                    p2 = headA;
                }
            }
            return p1;
        }
    }

    public static void main(String[] args){
        Solution result = new Solution();
        ListNode a, b; // main不work
        ListNode res = result.getIntersectionNode(a, b);
        System.out.println(res);
    }
}
这种main里面就不知道该怎么办了。

public class hasCycle {
    public static class Solution {
        public class ListNode {
            int val;
            ListNode next;
            ListNode(int x) {
                val = x;
                next = null;
            }
        }

        public ListNode ListNode(int x) {
            ListNode res = new Solution.ListNode(-1);
            res.val = x;
            res.next = null;
            return res;
        }

        public boolean hasCycle(ListNode head) {
            if (head == null || (head != null && head.next == null))  //
special situation
                return false;
            else if (head != null && head.next == head)
                return true;
           
            ListNode fast = head;
            ListNode slow = head;
            while (fast != null && slow != null) {
                slow = slow.next;
                fast = fast.next.next; // 2X speed
                while (fast != null && fast.next != null && slow != null &&
fast.val != slow.val) {
                    slow = slow.next;
                    fast = fast.next.next; // 2X speed
                }
                if (fast == null || fast.next == null || slow == null)
                    return false;
                else if (fast != null && slow != null && fast.val == slow.
val)
                    return true;
                else return false;
            }
            return false;
        }
    }

    public static void main(String[] args){
        Solution result = new Solution();
        Solution.ListNode one = result.ListNode(1);
        Solution.ListNode two = result.ListNode(1);
        boolean res = result.hasCycle(one);
        System.out.println(res);
    }
}
这种可以用,就是main里每次Solution.不方便。

也试过package leetcode;但还是找不到我的node类,是因为我装java时路径没设对?
我想知道假如我想保留题目的名字作为类名,保留Solution因定类的话,有没有什么更
好的方法我可以方便用node类的?

想听大牛小牛牛牛们说句话,如果知道没什么更好的办法,也许我再不知道把它塞哪儿
的时候就不那么堵了。。。

--
※ 来源:·WWW 未名空间站 网址:mitbbs.com 移动:在应用商店搜索未名空间·[FROM: 98.]

http://www.mitbbs.com/article_t/Programming/31393541.html

No comments:

Post a Comment