Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions #7 Reverse Integer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//https://leetcode.com/problems/reverse-integer/
class Solution {
public int reverse(int x) {
int rev = 0;
while(x!=0)
{
int r = x%10;
x=x/10;
if(rev> Integer.MAX_VALUE/10 ||rev == Integer.MAX_VALUE/10 && r > 7)
return 0;
if(rev< Integer.MIN_VALUE/10 ||rev == Integer.MIN_VALUE/10 && r > 8)
return 0;
rev=rev*10+r;
}
return rev;
}
}
20 changes: 20 additions & 0 deletions 1184 Distance Between Bus Stops.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// https://leetcode.com/problems/distance-between-bus-stops/

class Solution {
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
int clockwise = 0, anticlockwise = 0;
int i = start;

while(i != destination) {
clockwise += distance[i];
i = (i + 1)%distance.length;
}

while(i != start) {
anticlockwise += distance[i];
i = (i + 1)%distance.length;
}

return Math.min(anticlockwise, clockwise);
}
}
18 changes: 18 additions & 0 deletions 1539 Kth Missing Positive Number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// https://leetcode.com/problems/kth-missing-positive-number/submissions/

class Solution {
public int findKthPositive(int[] arr, int k) {
if(k > arr[arr.length - 1] - arr.length) return k + arr.length;
int x = 1;
int i = 0;
while(i < arr.length) {
if(arr[i] != x) {
k--;
if(k == 0) return x;
}
else i++;
x++;
}
return k + x - 1;
}
}
29 changes: 29 additions & 0 deletions BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Scanner;

public class BinarySearch {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter size of an array");
int n = scan.nextInt();
int[] a = new int[n];
System.out.println("Enter array elements");
for(int i=0;i<n;i++)
a[i] = scan.nextInt();
System.out.println("Enter Element to be Searched");
int k = scan.nextInt();
int low=0;
int high=n-1;
while(low<=high){
int mid = (low+high)/2;
if(a[mid]==k){
System.out.println(k + " found at index " + mid);
return;
}
if(k>a[mid])
low=mid+1;
else
high=mid-1;
}
System.out.println("Element not found");
}
}
57 changes: 57 additions & 0 deletions Doubly Linked List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.*;
class Node
{
int data;
Node next;
Node prev;
}
class DoublyLinkedList
{
Node head;
public void insert (int data)
{
Node node = new Node ();
node.data = data;
if (head == null)
{
head = node;

}
else
{
Node n = head;
while (n.next != null)
{
n = n.next;
}
n.next = node;
node = n.next;

}
}
public void show ()
{
Node n1 = head;
while (n1.next != null)
{
System.out.println (n1.data);
n1= n1.next;

}
System.out.println (n1.data);

}
}

public class Main
{
public static void main (String[]args)
{
DoublyLinkedList l = new DoublyLinkedList ();
l.insert (2);
l.insert (3);
l.insert (4);
l.show();

}
}
46 changes: 46 additions & 0 deletions PasswordValidation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.*;
class MyException extends Exception{

MyException(String msg){
super(msg);
}
}
class PasswordValidation{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("Password must contain one digit, one uppercase character, one lowercase character and length should be greater than equal to 8");
System.out.println("Enter password : ");
String y=sc.nextLine();
int ctd=0,ctu=0,ctl=0;
try{
if(y.length()<8)
throw new MyException("Too short password!");
for(int i=0;i<y.length();i++){
if(y.charAt(i)>='0'&&y.charAt(i)<='9'){
ctd++;
}
if(y.charAt(i)>='A'&&y.charAt(i)<='Z')
ctu++;
if(y.charAt(i)>='a'&&y.charAt(i)<='z')
ctl++;
}
if(ctd==0&&ctl==0&&ctu==0)
throw new MyException("No Digit , no uppercase and no lower case!");
else if(ctd==0&&ctl==0)
throw new MyException("No Digit and no lowercase!");
else if(ctl==0&&ctu==0)
throw new MyException("No lowercase and no uppercase!");
else if(ctd==0&&ctu==0)
throw new MyException("No digit and no uppercase!");
else if(ctd==0)
throw new MyException("No Digit!");
else if(ctu==0)
throw new MyException("No Uppercase character!");
else if(ctl==0)
throw new MyException("No Uppercase character!");
System.out.println("correct password");
}
catch(MyException e){
System.out.println(e.getMessage());
}
}}
14 changes: 14 additions & 0 deletions Rotate_Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//https://leetcode.com/problems/rotate-array/
class Solution {
public void rotate(int[] nums, int k) {
for(int i=0;i<k;i++){
int temp = nums[nums.length-1];
int j = nums.length-2;
while(j>=0){
nums[j+1] = nums[j];
j--;
}
nums[0] = temp;
}
}
}
141 changes: 141 additions & 0 deletions SortLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
class LinkedList
{
Node head;

public LinkedList()
{
this.head=null;
}

void insertAtEnd(int data)
{
Node newNode=new Node(data);
if(head==null)
{
head=newNode;
}
else
{
Node current=head;
while(current.next!=null)
{
current=current.next;
}
current.next=newNode;
}
}
void insertAtStart(int data)
{
Node newNode=new Node(data);
if(head==null)
{
head=newNode;
}
else
{
newNode.next=head;
head=newNode;
}
}

void traverse()
{
Node n=head;
while(n!=null)
{
System.out.print(n.data+" -> ");
n=n.next;
}
}

void sortList()
{
int swap=0;
Node n=head;
Node s1=head;
Node s2=head;
while(s1!=null)
{
s2=head;
Node tmp=head;
Node tmp2=null;
while(s2.next!=null)
{
if(s2.data>s2.next.data)
{
swap++;
if(s2==head)
{
head=s2.next;
}
tmp2=s2.next.next;
s2.next.next=s2;
tmp.next=s2.next;
s2.next=tmp2;

}
tmp=s2;
s2=s2.next;
if(s2==null)
{
break;
}
}
s1=s1.next;
}
System.out.println("Swap " + swap);
}
}
class Node
{
int data;
Node next;

public Node(int data)
{
this.data=data;
this.next=null;
}

}
public class Main
{
public static void main(String[] args) {
LinkedList l1=new LinkedList();
l1.insertAtEnd(5);
l1.insertAtEnd(2);
l1.insertAtEnd(3);
l1.insertAtEnd(1);
l1.insertAtEnd(12);
l1.insertAtEnd(-1);
l1.insertAtEnd(14);
l1.insertAtEnd(15);
l1.insertAtEnd(16);
l1.insertAtEnd(-5);
l1.insertAtEnd(6);
l1.insertAtEnd(7);
l1.insertAtEnd(8);
l1.insertAtEnd(9);
l1.insertAtEnd(10);
l1.insertAtEnd(-11);
l1.insertAtEnd(17);
l1.insertAtEnd(-18);
l1.insertAtEnd(0);
l1.insertAtEnd(-222);
l1.traverse();
System.out.println();
l1.sortList();
l1.traverse();
System.out.println();

}
}

Loading