Skip to content

Adding java version for leetcode question 2 and 3 #3

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 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.class
29 changes: 29 additions & 0 deletions leetcode/1-Easy-Two-Sum/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.*;

class Solution {
public static int[] twoSum(int[] nums, int target)
{
Map<Integer,Integer> m = new HashMap<>();

for(int i = 0; i < nums.length; i++){
m.put(nums[i],i);
}
System.out.println(m.size());

for(int i = 0; i < nums.length; i++)
{
if(m.containsValue(target - nums[i] )&& m.get(target - nums[i]) != i)
{
int n[] = new int[]{ m.get(target - nums[i]), i};
return n;
}
}

throw new IllegalArgumentException("No two sum solution");
}
public static void main(String[] args) {
int nums[] = new int[]{3,3};
System.out.println(twoSum(nums, 6));

}
}
21 changes: 21 additions & 0 deletions leetcode/12-Medium-Integer-To-Roman/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public static String intToRoman(int num) {
StringBuilder sb = new StringBuilder();
int values[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
String syms[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int pnt = 0;
while (num > 0) {
if (num >= values[pnt]) {
sb.append(syms[pnt]);
num = num - values[pnt];
} else {
pnt++;
}
}
return sb.toString();
}
public static void main(String[] args)
{
System.out.println(intToRoman(1050));
}
}
38 changes: 38 additions & 0 deletions leetcode/13-Easy-Roman-To-Integer/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.*;

class solution
{
public static int romanToInt(String s)
{
int sum = 0;
char ch1='0', ch2='0';
for(int i=0;i<s.length();i++){
ch1 = s.charAt(i);
if(i+1<s.length()) ch2 = s.charAt(i+1);
if(ch1=='I'&& ch2=='V') {sum+=4;i++;continue;}
if(ch1=='X'&& ch2=='L') {sum+=40;i++;continue;}
if(ch1=='C'&& ch2=='D') {sum+=400;i++;continue;}
if(ch1=='I'&& ch2=='X') {sum+=9;i++;continue;}
if(ch1=='X'&& ch2=='C') {sum+=90;i++;continue;}
if(ch1=='C'&& ch2=='M') {sum+=900;i++;continue;}

switch(ch1){
case 'I': sum += 1;break;
case 'V': sum += 5;break;
case 'X': sum += 10;break;
case 'L': sum += 50;break;
case 'C': sum += 100;break;
case 'D': sum += 500;break;
case 'M': sum += 1000;break;
}
}
return sum;

}
public static void main(String[] args)
{
String s = "LVIII";
System.out.println(romanToInt(s));

}
}
38 changes: 38 additions & 0 deletions leetcode/14-Easy-Longest-Common-Prefix/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.*;
class Solution {
public static String longestCommonPrefix(String[] strs)
{
if (strs == null || strs.length == 0) return "";
if(strs.length == 1) return strs[0];
int prefix = 0 ;
boolean flag = true ;
while(flag && prefix < strs[0].length())
{
char ch = strs[0].charAt(prefix);
for(int i = 1; i < strs.length ;i++)
{
if( prefix < strs[i].length())
{
if(ch == strs[i].charAt(prefix))
{
flag = true;
}
else{
if(prefix == 0)
{
return "";
}
flag = false;
}
}
}
prefix++;
}
return strs[0].length() == 0 ? "" : prefix == 1 ? strs[0].substring(0, 1) :strs[0].substring(0, prefix - 1);

}
public static void main(String[] args) {
String[] strs = new String[]{"flower","flow","flight"};
System.out.println(longestCommonPrefix(strs));
}
}
96 changes: 96 additions & 0 deletions leetcode/2-Medium-Add-Two-Numbers/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import java.util.*;

public class Solution
{
public ListNode addTwoNumbers(ListNode l1, ListNode l2)
{
return adder(l1,l2,0);
}
public ListNode adder(ListNode l1, ListNode l2, int carry)
{
if (l1 == null && l2 == null )
{
if (carry == 0)
return null;
else
return new ListNode(1);
}
int sum = (l1 == null ? 0: l1.val) + (l2 == null ? 0: l2.val) + carry;
ListNode result = new ListNode(sum % 10);
result.next = adder((l1==null ? null : l1.next),(l2==null ? null : l2.next),sum/10);

return result;

}

/// testing
public static void main(String[] args)
{
int num1 = 342;
int num2 = 465;
int n = num1 % 10; // n = 2
ListNode l1 = new ListNode(n);
ListNode node1 = l1;
num1 = num1 /10;
while(num1 > 0)
{
n = num1 % 10;
node1.next = new ListNode(n);
num1 = num1 / 10;
node1 = node1.next;
}
n = num2 % 10; // n = 5
ListNode l2= new ListNode(n);
ListNode node2 = l2;
num2 = num2 /10;
while(num2 > 0)
{
n = num2 % 10;
node2.next = new ListNode(n);
num2 = num2 / 10;
node2 = node2.next;
}
Solution s = new Solution();

// calling function addTwoNumber , it will return result linkedlist ...........
ListNode result = s.addTwoNumbers(l1, l2);

/// printing input linkedlist 1...........
System.out.print("Input : ");
while(l1.next!= null)
{
System.out.print((l1 == null ? "": l1.val+"->"));
l1 = l1.next;

}
System.out.print(l1 != null ? l1.val : "");

/// printing input linkedlist 2.............

System.out.print(" + ");

while(l2.next != null)
{
System.out.print((l2 == null ? "": l2.val+"->"));
l2 = l2.next;
}
System.out.println(l2 != null ? l2.val : "");

//printing result linkedlist............
System.out.print("Output : ");
while(result.next != null)
{
System.out.print(result.val +"->");
result = result.next;
}
System.out.println(result != null ? result.val : "");

}

}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}

40 changes: 40 additions & 0 deletions leetcode/26-Easy-Remove-Duplicates-From-Sorted-Array/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.*;

class Solution {
/* public static int removeDuplicates(int[] nums)
{
LinkedHashSet<Integer> hs = new LinkedHashSet<>();
int j = 0;
for(int i = 0 ; i < nums.length; i++)
{
if( hs.add(nums[i]))
{
nums[j] = nums[i];
j++;
}

}
return hs.size();

}
*/
public static int removeDuplicates(int[] nums)
{
int j = 0;
for(int i = 1 ; i< nums.length; i++)
{
if(nums[j] != nums[i])
{
j++;
nums[j] = nums[i];

}
}
return j + 1;

}
public static void main(String[] args) {
int[] nums = new int[]{0,0,1,1,1,2,2,3,3,4};
System.out.println(removeDuplicates(nums));
}
}
18 changes: 18 additions & 0 deletions leetcode/27-Easy-Remove-Element/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.*;

class Solution {
public int removeElement(int[] nums, int val) {

int j = 0;
for(int i = 0 ; i < nums.length; i++)
{
if(nums[i] != val)
{
nums[j] = nums[i];
j++;
}
}
return j;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.*;
class Solution {

public static int lengthOfLongestSubstring(String s)
{
int i = 0, j = 0, max = 0;
HashSet<Character> set = new HashSet<>();

while (j < s.length()) {
if (!set.contains(s.charAt(j))) {
set.add(s.charAt(j++));
max = Math.max(max, set.size());
} else {
set.remove(s.charAt(i++));
}
}

return max;
}

////.................TESTING................./////

public static void main(String[] args)
{
String s = "dvdf";
System.out.println("Input : "+ s);
System.out.println("Output : "+lengthOfLongestSubstring(s));

}
}
32 changes: 32 additions & 0 deletions leetcode/5-Medium-Longest-Palindromic-Substring/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public static String longestPalindrome(String s) {
char[] chars = s.toCharArray();
int len = s.length();c
while (len >= 0) {
for (int i = 0; i + len - 1 < chars.length; i++) {
int left = i;
int right = i + len - 1;
boolean good = true;
while (left < right) {
if (chars[left] != chars[right]) {
good = false;
break;
}
left++;
right--;
}
if (good)
return s.substring(i, i + len);
}
--len;
}

return "";
}
public static void main(String[] args)
{
String s = "aabbbaa";
System.out.println(longestPalindrome(s));

}
}
3 changes: 2 additions & 1 deletion leetcode/6-Medium-Zig-Zag-Conversion/answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public static String convert(String s, int numRows) {
return s;
// Create string buffer to hold string buffer for each row
StringBuffer[] result = new StringBuffer[numRows];
// easy to maintain
for (int i = 0; i < numRows; i++){
result[i] = new StringBuffer();
}
Expand All @@ -27,4 +28,4 @@ public static String convert(String s, int numRows) {
}
return result[0].toString();
}
}
}
Loading