Skip to content

tasks 160- 172, without 161,163,170 #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 18, 2021
Merged

Conversation

IvanMenov
Copy link
Contributor

No description provided.

@@ -16,6 +16,12 @@ public ListNode(int val, ListNode next) {
this.next = next;
}

@Override
public boolean equals(Object obj) {
return this.val == ((ListNode) obj).val;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We compare ListNode by pointers not by value. The custom comparator may be applied for a solution.

}
if (getClass() != obj.getClass()) return false;
ListNode other = (ListNode) obj;
return val == other.val;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to compare ListNode by pointer, not by value.

@javadev
Copy link
Owner

javadev commented Nov 18, 2021

@IvanMenov
Copy link
Contributor Author

IvanMenov commented Nov 18, 2021

Those codecov/patch, codecov/project check that were not successful was beacause the test cover only one scenario, as we agreed.

@@ -7,7 +7,7 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode pA = headA;
ListNode pB = headB;
while (pA != null || pB != null) {
if (pA != null && pB != null && pA.equals(pB)) {
if (pA != null && pB != null && pA.toString().equals(pB.toString())) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution works for me:

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode node1 = headA;
        ListNode node2 = headB;
        while (node1 != node2) {
            node1 = node1 == null ? headB : node1.next;
            node2 = node2 == null ? headA : node2.next;
        }
        return node1;
    }
}

pB = pB == null ? headA : pB.next;
ListNode node1 = headA;
ListNode node2 = headB;
while (!node1.toString().equals(node2.toString())) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why toString?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above implementation, that you wrote results in nullpoinerexception in the test case, as node1 == node2 evaluates to false

Copy link
Owner

@javadev javadev Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it in leetcode. There are no exceptions.

image

public class Solution {
private Solution() {}

public static int trailingZeroes(int n) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why static?

new ListNode(
6,
new ListNode(
1, new ListNode(8, new ListNode(4, new ListNode(5))))));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the same ListNode(8) for both lists.


public int compareVersion(String version1, String version2) {
String[] ver1 = version1.split("\\.", 0);
String[] ver2 = version2.split("\\.", 0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use solution without split.

class Solution {
    public int compareVersion(String version1, String version2) {
        // acquire first number
        int numA = 0;
        int i;
        for(i=0; i<version1.length(); i++){
            char c = version1.charAt(i);
            if(c == '.'){
                break;
            }else{
                numA = numA*10 + ((int)c - 48);
            }
        }
        
        // acquire second number
        int numB = 0;
        int j;
        for(j=0; j<version2.length(); j++){
            char c = version2.charAt(j);
            if(c == '.'){
                break;
            }else{
                numB = numB*10 + ((int)c - 48);
            }
        }
        
        // compare
        if(numA > numB){
            return 1;
        }else if(numA < numB){
            return -1;
        }else{ // equal
            String v1 = "";
            String v2 = "";

            if(i != version1.length())
                v1 = version1.substring(i+1);

            if(j != version2.length())
                v2 = version2.substring(j+1);
            
            // if both versions end here, they are equal
            if(v1.equals("") && v2.equals("")){
                return 0;
            }else{
                return compareVersion(v1, v2);
            }
        }
    }
}

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

79.2% 79.2% Coverage
0.0% 0.0% Duplication

@javadev javadev merged commit 822d921 into javadev:main Nov 18, 2021
@IvanMenov IvanMenov deleted the tasks_160_172 branch November 19, 2021 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants