-
Notifications
You must be signed in to change notification settings - Fork 89
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
Conversation
@@ -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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There is an instruction for the readme.md generation tool: https://github.com/javadev/LeetCode-in-Java/releases/tag/generatemd The jar file itself: https://github.com/javadev/LeetCode-in-Java/releases/download/generatemd/generatemd-1.0.jar |
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())) { |
There was a problem hiding this comment.
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())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why toString?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public class Solution { | ||
private Solution() {} | ||
|
||
public static int trailingZeroes(int n) { |
There was a problem hiding this comment.
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)))))); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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);
}
}
}
}
Kudos, SonarCloud Quality Gate passed! |
No description provided.