Skip to content

Refactor #199

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -23,17 +23,22 @@ 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()) {
isPalindrome = false;
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;
}
}

Original file line number Diff line number Diff line change
@@ -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));
}
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions src/LeetCode/src/SwapNumbersInArray.java
Original file line number Diff line number Diff line change
@@ -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;
}
}