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 @@ +<component name="ProjectCodeStyleConfiguration"> + <code_scheme name="Project" version="173"> + <ScalaCodeStyleSettings> + <option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" /> + </ScalaCodeStyleSettings> + </code_scheme> +</component> \ 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 @@ +<component name="ProjectCodeStyleConfiguration"> + <state> + <option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" /> + </state> +</component> \ 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="CompilerConfiguration"> + <bytecodeTargetLevel target="17" /> + </component> +</project> \ 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="GradleSettings"> + <option name="linkedExternalProjectsSettings"> + <GradleProjectSettings> + <option name="distributionType" value="DEFAULT_WRAPPED" /> + <option name="externalProjectPath" value="$PROJECT_DIR$" /> + <option name="modules"> + <set> + <option value="$PROJECT_DIR$" /> + </set> + </option> + </GradleProjectSettings> + </option> + </component> +</project> \ 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="RemoteRepositoriesConfiguration"> + <remote-repository> + <option name="id" value="central" /> + <option name="name" value="Maven Central repository" /> + <option name="url" value="https://repo1.maven.org/maven2" /> + </remote-repository> + <remote-repository> + <option name="id" value="jboss.community" /> + <option name="name" value="JBoss Community repository" /> + <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> + </remote-repository> + <remote-repository> + <option name="id" value="MavenRepo" /> + <option name="name" value="MavenRepo" /> + <option name="url" value="https://repo.maven.apache.org/maven2/" /> + </remote-repository> + <remote-repository> + <option name="id" value="MavenLocal" /> + <option name="name" value="MavenLocal" /> + <option name="url" value="file:$MAVEN_REPOSITORY$/" /> + </remote-repository> + </component> +</project> \ 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ExternalStorageConfigurationManager" enabled="true" /> + <component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK" /> +</project> \ 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Palette2"> + <group name="Swing"> + <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> + </item> + <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> + </item> + <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> + <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> + <initial-values> + <property name="text" value="Button" /> + </initial-values> + </item> + <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="RadioButton" /> + </initial-values> + </item> + <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="CheckBox" /> + </initial-values> + </item> + <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="Label" /> + </initial-values> + </item> + <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> + <preferred-size width="200" height="200" /> + </default-constraints> + </item> + <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> + <preferred-size width="200" height="200" /> + </default-constraints> + </item> + <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> + </item> + <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> + <preferred-size width="-1" height="20" /> + </default-constraints> + </item> + <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> + </item> + <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> + </item> + </group> + </component> +</project> \ 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project> \ 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..32e5a55eb 100644 --- a/src/ByteByByte/src/CodingInterviewQuestions/Array/P1_MedianOfArrays/Java/Solution.java +++ b/src/ByteByByte/src/CodingInterviewQuestions/Array/P1_MedianOfArrays/Java/Solution.java @@ -16,21 +16,21 @@ 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; - 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 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..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,6 +1,7 @@ package LeetCode.src.Explore.Interview.GoogleInterview.ArraysAndStrings.FirstMissingPositive.Java; +import LeetCode.src.SwapNumbersInArray; -public class Solution { +public class Solution extends SwapNumbersInArray { public static void main(String[] args) { int[] input = {3, 4, -1, 1}; int result = findMissingPositive(input); @@ -37,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/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<length; i++) { + buf[readBytes + i] = buffer[i]; + } + readBytes += length; + } + return readBytes; + } } \ No newline at end of file diff --git a/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/ReadNCharactersGivenRead4/Java/Solution.java b/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/ReadNCharactersGivenRead4/Java/Solution.java index 6f1d044ac..54485010c 100644 --- a/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/ReadNCharactersGivenRead4/Java/Solution.java +++ b/src/LeetCode/src/Explore/Interview/GoogleInterview/ArraysAndStrings/ReadNCharactersGivenRead4/Java/Solution.java @@ -10,29 +10,5 @@ public static void main(String[] args) { Solution solution = new Solution(); System.out.println(solution.read(buf, n)); - } - - /** - * @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<length; i++) { - buf[readBytes + i] = buffer[i]; - } - readBytes += length; - } - return readBytes; } } \ 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 f2981a869..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,6 +1,7 @@ package LeetCode.src.Explore.Interview.TopInterviewQuestions_Easy.Array.MoveZeroes.Java; +import LeetCode.src.SwapNumbersInArray; -public class Solution { +public class Solution extends SwapNumbersInArray { public static void main(String[] args) { int[] nums = {1,1,0,3,12}; moveZeroes2(nums); @@ -35,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 a205b72ac..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,6 +1,8 @@ package LeetCode.src.Problems.P1_P100.P31_NextPermutation.Java; -public class Solution { +import LeetCode.src.SwapNumbersInArray; + +public class Solution extends SwapNumbersInArray { public static void main(String[] args) { int[] nums = {1,2,3}; nextPermutation(nums); @@ -38,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 9347ab781..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,6 +1,7 @@ package LeetCode.src.Problems.P201_P300.P283_MoveZeroes.Java; +import LeetCode.src.SwapNumbersInArray; -public class Solution { +public class Solution extends SwapNumbersInArray { public static void main(String[] args) { int[] nums = {1,1,0,3,12}; moveZeroes2(nums); @@ -35,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/SwapNumbersInArray.java b/src/LeetCode/src/SwapNumbersInArray.java new file mode 100644 index 000000000..39750c2b7 --- /dev/null +++ b/src/LeetCode/src/SwapNumbersInArray.java @@ -0,0 +1,9 @@ +package LeetCode.src; + +public class SwapNumbersInArray { + public static void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +}