From 9950a90f7fc03f31683393c6d480db391bbac37d Mon Sep 17 00:00:00 2001 From: Hardee Garala Date: Thu, 24 Mar 2022 19:50:24 -0300 Subject: [PATCH 1/3] refactor - rename variable, extract method, move method, pull up method, extract class --- .idea/.gitignore | 3 + .idea/codeStyles/Project.xml | 7 + .idea/codeStyles/codeStyleConfig.xml | 5 + .idea/compiler.xml | 6 + .idea/gradle.xml | 16 +++ .idea/jarRepositories.xml | 25 ++++ .idea/misc.xml | 5 + .idea/sonarlint/issuestore/index.pb | 0 .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 6 + .../P1_MedianOfArrays/Java/Solution.java | 10 +- .../D18_QueuesAndStacks/Java/Solution.java | 13 +- .../D22_BinarySearchTrees/Java/Node.java | 42 ++++++ .../D22_BinarySearchTrees/Java/Solution.java | 40 +----- .../FirstMissingPositive/Java/Solution.java | 12 +- .../Java/Reader4.java | 25 ++++ .../Java/Solution.java | 24 ---- .../Array/MoveZeroes/Java/Solution.java | 12 +- .../P31_NextPermutation/Java/Solution.java | 12 +- .../P283_MoveZeroes/Java/Solution.java | 12 +- src/LeetCode/src/SwapNumbersInArray.java | 9 ++ 21 files changed, 318 insertions(+), 90 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/sonarlint/issuestore/index.pb create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D22_BinarySearchTrees/Java/Node.java create mode 100644 src/LeetCode/src/SwapNumbersInArray.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..26d33521a --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 000000000..919ce1f1f --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 000000000..a55e7a179 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 000000000..b589d56e9 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 000000000..ba1ec5c7e --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 000000000..d20b5975a --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..668048d36 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb new file mode 100644 index 000000000..e69de29bb diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 000000000..e96534fb2 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/ByteByByte/src/CodingInterviewQuestions/Array/P1_MedianOfArrays/Java/Solution.java b/src/ByteByByte/src/CodingInterviewQuestions/Array/P1_MedianOfArrays/Java/Solution.java index 83c52ae64..6c84af3cd 100644 --- a/src/ByteByByte/src/CodingInterviewQuestions/Array/P1_MedianOfArrays/Java/Solution.java +++ b/src/ByteByByte/src/CodingInterviewQuestions/Array/P1_MedianOfArrays/Java/Solution.java @@ -16,12 +16,12 @@ public static double findMedianSortedArrays(int[] nums1, int[] nums2) { int r = len1; //find the m1 so that nums1[m1] >= nums2[m2 - 1] while(l < r){ - int m1 = l + (r - l) / 2; - int m2 = k - m1; - if (nums1[m1] < nums2[m2 - 1]) { - l = m1 + 1; + int nums1ArrayIndex = l + (r - l) / 2; + int nums2ArrayIndex = k - nums1ArrayIndex; + if (nums1[nums1ArrayIndex] < nums2[nums2ArrayIndex - 1]) { + l = nums1ArrayIndex + 1; } else { - r = m1; + r = nums1ArrayIndex; } } int m1 = l; diff --git a/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D18_QueuesAndStacks/Java/Solution.java b/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D18_QueuesAndStacks/Java/Solution.java index 8dc41400a..9157e5d54 100644 --- a/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D18_QueuesAndStacks/Java/Solution.java +++ b/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D18_QueuesAndStacks/Java/Solution.java @@ -23,6 +23,14 @@ public static void main(String[] args) { } // Pop/Dequeue the chars at the head of both data structures and compare them: + boolean isPalindrome = isPalindrome(s, p); + + //Finally, print whether string s is palindrome or not. + System.out.println( "The word, " + input + ", is " + + ( (!isPalindrome) ? "not a palindrome." : "a palindrome." ) ); + } + + private static boolean isPalindrome(char[] s, Solution p) { boolean isPalindrome = true; for (int i = 0; i < s.length/2; i++) { if (p.popCharacter() != p.dequeueCharacter()) { @@ -30,10 +38,7 @@ public static void main(String[] args) { break; } } - - //Finally, print whether string s is palindrome or not. - System.out.println( "The word, " + input + ", is " - + ( (!isPalindrome) ? "not a palindrome." : "a palindrome." ) ); + return isPalindrome; } } diff --git a/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D22_BinarySearchTrees/Java/Node.java b/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D22_BinarySearchTrees/Java/Node.java new file mode 100644 index 000000000..d84575bea --- /dev/null +++ b/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D22_BinarySearchTrees/Java/Node.java @@ -0,0 +1,42 @@ +package HackerRank.src.Tracks.Tutorials.ThirtyDaysOfCode.D22_BinarySearchTrees.Java; + +public class Node{ + Node left,right; + int data; + Node(){ + } + + Node(int data){ + this.data=data; + left=right=null; + } + + + public static Node insert(Node root,int data){ + if(root==null){ + return new Node(data); + } + else{ + Node cur; + if(data<=root.data){ + cur=insert(root.left,data); + root.left=cur; + } + else{ + cur=insert(root.right,data); + root.right=cur; + } + return root; + } + } + + public static int getHeight(Node root){ + if (root == null){ + return -1; + } + else { + return 1+ Math.max(getHeight(root.left), getHeight(root.right)); + } + } +} + diff --git a/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D22_BinarySearchTrees/Java/Solution.java b/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D22_BinarySearchTrees/Java/Solution.java index 2bd47ac6b..0b701b136 100644 --- a/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D22_BinarySearchTrees/Java/Solution.java +++ b/src/HackerRank/src/Tracks/Tutorials/ThirtyDaysOfCode/D22_BinarySearchTrees/Java/Solution.java @@ -5,51 +5,17 @@ public class Solution { public static void main(String[] args) { + Node node = new Node(); Scanner sc=new Scanner(System.in); int T=sc.nextInt(); Node root=null; while(T-->0){ int data=sc.nextInt(); - root=insert(root,data); + root=node.insert(root,data); } - int height=getHeight(root); + int height=node.getHeight(root); System.out.println(height); sc.close(); } - - public static Node insert(Node root,int data){ - if(root==null){ - return new Node(data); - } - else{ - Node cur; - if(data<=root.data){ - cur=insert(root.left,data); - root.left=cur; - } - else{ - cur=insert(root.right,data); - root.right=cur; - } - return root; - } - } - - public static int getHeight(Node root){ - if (root == null){ - return -1; - } - else { - return 1+ Math.max(getHeight(root.left), getHeight(root.right)); - } - } } -class Node{ - Node left,right; - int data; - Node(int data){ - this.data=data; - left=right=null; - } -} \ No newline at end of file diff --git a/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/FirstMissingPositive/Java/Solution.java b/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/FirstMissingPositive/Java/Solution.java index 85984e1f3..91cd35370 100644 --- a/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/FirstMissingPositive/Java/Solution.java +++ b/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/FirstMissingPositive/Java/Solution.java @@ -1,5 +1,7 @@ package LeetCode.src.Explore.Interview.GoogleInterview.ArraysAndStrings.FirstMissingPositive.Java; +import static LeetCode.src.SwapNumbersInArray.swap; + public class Solution { public static void main(String[] args) { int[] input = {3, 4, -1, 1}; @@ -37,9 +39,9 @@ public static int findMissingPositive(int[] nums) { } - private static void swap(int[] nums, int i, int j) { - int temp = nums[i]; - nums[i] = nums[j]; - nums[j] = temp; - } +// private static void swap(int[] nums, int i, int j) { +// int temp = nums[i]; +// nums[i] = nums[j]; +// nums[j] = temp; +// } } \ No newline at end of file diff --git a/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/ReadNCharactersGivenRead4/Java/Reader4.java b/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/ReadNCharactersGivenRead4/Java/Reader4.java index 648d45709..21545ad67 100644 --- a/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/ReadNCharactersGivenRead4/Java/Reader4.java +++ b/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/ReadNCharactersGivenRead4/Java/Reader4.java @@ -4,4 +4,29 @@ public class Reader4 { int read4(char[] buf) { return 4; } + + + /** + * @param buf Destination buffer + * @param n Maximum number of characters to read + * @return The number of characters read + */ + public int read(char[] buf, int n) { + char[] buffer = new char[4]; + boolean endOfFile = false; + int readBytes = 0; // bytes read so far + + while (readBytes < n && !endOfFile) { + int currentReadBytes = read4(buffer); + if (currentReadBytes != 4) { + endOfFile = true; + } + int length = Math.min(n - readBytes, currentReadBytes); + for (int i=0; i Date: Thu, 24 Mar 2022 20:33:19 -0300 Subject: [PATCH 2/3] refactor code: rename variable, extract method, pull-up method, move method and extract class. --- .../FirstMissingPositive/Java/Solution.java | 10 ++-------- .../Array/MoveZeroes/Java/Solution.java | 10 ++-------- .../P1_P100/P31_NextPermutation/Java/Solution.java | 9 ++------- .../P201_P300/P283_MoveZeroes/Java/Solution.java | 10 ++-------- 4 files changed, 8 insertions(+), 31 deletions(-) diff --git a/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/FirstMissingPositive/Java/Solution.java b/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/FirstMissingPositive/Java/Solution.java index 91cd35370..6e2ff3099 100644 --- a/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/FirstMissingPositive/Java/Solution.java +++ b/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/FirstMissingPositive/Java/Solution.java @@ -1,8 +1,7 @@ package LeetCode.src.Explore.Interview.GoogleInterview.ArraysAndStrings.FirstMissingPositive.Java; +import LeetCode.src.SwapNumbersInArray; -import static LeetCode.src.SwapNumbersInArray.swap; - -public class Solution { +public class Solution extends SwapNumbersInArray { public static void main(String[] args) { int[] input = {3, 4, -1, 1}; int result = findMissingPositive(input); @@ -39,9 +38,4 @@ public static int findMissingPositive(int[] nums) { } -// private static void swap(int[] nums, int i, int j) { -// int temp = nums[i]; -// nums[i] = nums[j]; -// nums[j] = temp; -// } } \ No newline at end of file diff --git a/src/LeetCode/src/Explore/Interview/TopInterviewQuestions_Easy/Array/MoveZeroes/Java/Solution.java b/src/LeetCode/src/Explore/Interview/TopInterviewQuestions_Easy/Array/MoveZeroes/Java/Solution.java index 008468ffd..a15a45910 100644 --- a/src/LeetCode/src/Explore/Interview/TopInterviewQuestions_Easy/Array/MoveZeroes/Java/Solution.java +++ b/src/LeetCode/src/Explore/Interview/TopInterviewQuestions_Easy/Array/MoveZeroes/Java/Solution.java @@ -1,8 +1,7 @@ package LeetCode.src.Explore.Interview.TopInterviewQuestions_Easy.Array.MoveZeroes.Java; +import LeetCode.src.SwapNumbersInArray; -import static LeetCode.src.SwapNumbersInArray.swap; - -public class Solution { +public class Solution extends SwapNumbersInArray { public static void main(String[] args) { int[] nums = {1,1,0,3,12}; moveZeroes2(nums); @@ -37,9 +36,4 @@ public static void moveZeroes2(int[] nums) { } } -// private static void swap(int[] nums, int i, int j) { -// int temp = nums[i]; -// nums[i] = nums[j]; -// nums[j] = temp; -// } } \ No newline at end of file diff --git a/src/LeetCode/src/Problems/P1_P100/P31_NextPermutation/Java/Solution.java b/src/LeetCode/src/Problems/P1_P100/P31_NextPermutation/Java/Solution.java index 540b7568d..77a401772 100644 --- a/src/LeetCode/src/Problems/P1_P100/P31_NextPermutation/Java/Solution.java +++ b/src/LeetCode/src/Problems/P1_P100/P31_NextPermutation/Java/Solution.java @@ -1,8 +1,8 @@ package LeetCode.src.Problems.P1_P100.P31_NextPermutation.Java; -import static LeetCode.src.SwapNumbersInArray.swap; +import LeetCode.src.SwapNumbersInArray; -public class Solution { +public class Solution extends SwapNumbersInArray { public static void main(String[] args) { int[] nums = {1,2,3}; nextPermutation(nums); @@ -40,9 +40,4 @@ private static void reverse(int[] nums, int start) { } } -// private static void swap(int[] nums, int i, int j) { -// int temp = nums[i]; -// nums[i] = nums[j]; -// nums[j] = temp; -// } } \ No newline at end of file diff --git a/src/LeetCode/src/Problems/P201_P300/P283_MoveZeroes/Java/Solution.java b/src/LeetCode/src/Problems/P201_P300/P283_MoveZeroes/Java/Solution.java index 316dfd861..8a2307c14 100644 --- a/src/LeetCode/src/Problems/P201_P300/P283_MoveZeroes/Java/Solution.java +++ b/src/LeetCode/src/Problems/P201_P300/P283_MoveZeroes/Java/Solution.java @@ -1,8 +1,7 @@ package LeetCode.src.Problems.P201_P300.P283_MoveZeroes.Java; +import LeetCode.src.SwapNumbersInArray; -import static LeetCode.src.SwapNumbersInArray.swap; - -public class Solution { +public class Solution extends SwapNumbersInArray { public static void main(String[] args) { int[] nums = {1,1,0,3,12}; moveZeroes2(nums); @@ -37,9 +36,4 @@ public static void moveZeroes2(int[] nums) { } } -// private static void swap(int[] nums, int i, int j) { -// int temp = nums[i]; -// nums[i] = nums[j]; -// nums[j] = temp; -// } } \ No newline at end of file From 9ecb3d0484fe7c8d424c4ecf4558715fd624cc96 Mon Sep 17 00:00:00 2001 From: Hardee Garala Date: Fri, 25 Mar 2022 02:21:19 -0300 Subject: [PATCH 3/3] modified refactor: rename variable --- .../Array/P1_MedianOfArrays/Java/Solution.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ByteByByte/src/CodingInterviewQuestions/Array/P1_MedianOfArrays/Java/Solution.java b/src/ByteByByte/src/CodingInterviewQuestions/Array/P1_MedianOfArrays/Java/Solution.java index 6c84af3cd..32e5a55eb 100644 --- a/src/ByteByByte/src/CodingInterviewQuestions/Array/P1_MedianOfArrays/Java/Solution.java +++ b/src/ByteByByte/src/CodingInterviewQuestions/Array/P1_MedianOfArrays/Java/Solution.java @@ -24,13 +24,13 @@ public static double findMedianSortedArrays(int[] nums1, int[] nums2) { r = nums1ArrayIndex; } } - int m1 = l; - int m2 = k - l; - int c1 = Math.max(m1 <= 0 ? Integer.MIN_VALUE : nums1[m1 - 1], - m2 <= 0? Integer.MIN_VALUE: nums2[m2 - 1]); + int nums1ArrayIndex = l; + int nums2ArrayIndex = k - l; + int c1 = Math.max(nums1ArrayIndex <= 0 ? Integer.MIN_VALUE : nums1[nums1ArrayIndex - 1], + nums2ArrayIndex <= 0? Integer.MIN_VALUE: nums2[nums2ArrayIndex - 1]); if ((len1 + len2) % 2 ==1) return c1; - int c2 = Math.min(m1 >= len1 ? Integer.MAX_VALUE : nums1[m1], - m2 >= len2 ? Integer.MAX_VALUE: nums2[m2]); + int c2 = Math.min(nums1ArrayIndex >= len1 ? Integer.MAX_VALUE : nums1[nums1ArrayIndex], + nums2ArrayIndex >= len2 ? Integer.MAX_VALUE: nums2[nums2ArrayIndex]); return (c1 + c2) / 2.0; } } \ No newline at end of file