diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/leetcode/1-Easy-Two-Sum/Solution.java b/leetcode/1-Easy-Two-Sum/Solution.java new file mode 100644 index 0000000..6b17238 --- /dev/null +++ b/leetcode/1-Easy-Two-Sum/Solution.java @@ -0,0 +1,29 @@ +import java.util.*; + +class Solution { + public static int[] twoSum(int[] nums, int target) + { + Map 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)); + + } +} \ No newline at end of file diff --git a/leetcode/12-Medium-Integer-To-Roman/Solution.java b/leetcode/12-Medium-Integer-To-Roman/Solution.java new file mode 100644 index 0000000..6d964d0 --- /dev/null +++ b/leetcode/12-Medium-Integer-To-Roman/Solution.java @@ -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)); + } +} \ No newline at end of file diff --git a/leetcode/13-Easy-Roman-To-Integer/Solution.java b/leetcode/13-Easy-Roman-To-Integer/Solution.java new file mode 100644 index 0000000..d22a4b9 --- /dev/null +++ b/leetcode/13-Easy-Roman-To-Integer/Solution.java @@ -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 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; } + } + diff --git a/leetcode/26-Easy-Remove-Duplicates-From-Sorted-Array/Solution.java b/leetcode/26-Easy-Remove-Duplicates-From-Sorted-Array/Solution.java new file mode 100644 index 0000000..9eca1b5 --- /dev/null +++ b/leetcode/26-Easy-Remove-Duplicates-From-Sorted-Array/Solution.java @@ -0,0 +1,40 @@ +import java.util.*; + +class Solution { + /* public static int removeDuplicates(int[] nums) + { + LinkedHashSet 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)); + } +} \ No newline at end of file diff --git a/leetcode/27-Easy-Remove-Element/solution.java b/leetcode/27-Easy-Remove-Element/solution.java new file mode 100644 index 0000000..0ef70ab --- /dev/null +++ b/leetcode/27-Easy-Remove-Element/solution.java @@ -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; + + } +} \ No newline at end of file diff --git a/leetcode/3-Medium-Longest-Substring-Without-Repeating-Characters/Solution.java b/leetcode/3-Medium-Longest-Substring-Without-Repeating-Characters/Solution.java new file mode 100644 index 0000000..fdcf420 --- /dev/null +++ b/leetcode/3-Medium-Longest-Substring-Without-Repeating-Characters/Solution.java @@ -0,0 +1,30 @@ +import java.util.*; +class Solution { + + public static int lengthOfLongestSubstring(String s) + { + int i = 0, j = 0, max = 0; + HashSet 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)); + + } +} diff --git a/leetcode/5-Medium-Longest-Palindromic-Substring/solution.java b/leetcode/5-Medium-Longest-Palindromic-Substring/solution.java new file mode 100644 index 0000000..a86ffe2 --- /dev/null +++ b/leetcode/5-Medium-Longest-Palindromic-Substring/solution.java @@ -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)); + + } +} \ No newline at end of file diff --git a/leetcode/6-Medium-Zig-Zag-Conversion/answer.java b/leetcode/6-Medium-Zig-Zag-Conversion/answer.java index 5e9aa12..d276189 100644 --- a/leetcode/6-Medium-Zig-Zag-Conversion/answer.java +++ b/leetcode/6-Medium-Zig-Zag-Conversion/answer.java @@ -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(); } @@ -27,4 +28,4 @@ public static String convert(String s, int numRows) { } return result[0].toString(); } -} \ No newline at end of file +} diff --git a/leetcode/7-Easy-Reverse-Integer/Solution.java b/leetcode/7-Easy-Reverse-Integer/Solution.java new file mode 100644 index 0000000..fe4c32d --- /dev/null +++ b/leetcode/7-Easy-Reverse-Integer/Solution.java @@ -0,0 +1,28 @@ +class Solution { + public int reverse(int x) + { + int rev = 0,n = 0 ; + boolean neg = false; + if( x < 0) + { + x = x * -1; + neg = true; + } + while( x > 0) + { + n = x % 10; + if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && n > 7)) return 0; + if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && n < -8)) return 0; + rev = rev * 10 + n; + x = x / 10; + + } + if(neg){ rev = -1 * rev ;} + return rev; + } + public static void main(String[] args) + { + Solution s = new Solution(); + System.out.println(s.reverse(324)); + } +} \ No newline at end of file diff --git a/leetcode/8-Medium-Atoi/Solution.java b/leetcode/8-Medium-Atoi/Solution.java new file mode 100644 index 0000000..d744a16 --- /dev/null +++ b/leetcode/8-Medium-Atoi/Solution.java @@ -0,0 +1,55 @@ +import java.util.*; + +class Solution { + public static int myAtoi(String str) + { + int num = 0; + int d = 10, neg = 1; + boolean digit_occur = false; + for(int i = 0 ; i < str.length(); i++) { + + char ch = str.charAt(i); + if(Character.isAlphabetic(ch) || ch == '.' || ch == '-' || ch ==' ' || ch == '+'){ + if(digit_occur ){ + return neg * num; + } + else{ + if (ch == ' ' || ch == '+' || ch == '-'){ + if(i+ 1 < str.length()) + { + if(Character.isDigit(str.charAt(i+1))) + { neg = ch == '-' ? neg * -1 : neg ;} + else if (ch == ' ') { + continue; + } + else + {return neg * num;} + + } + + } + else + { + return neg * num; + } + } + } + if(Character.isDigit(ch)){ + digit_occur = true; + if (num > Integer.MAX_VALUE/ 10 || num == Integer.MAX_VALUE /10 && Integer.MAX_VALUE % 10 < (ch - '0') ) { + return neg == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;} + num = num * d + Character.getNumericValue(ch); + } + + } + + return neg *num; + + } + public static void main(String[] args) + { + + String s = ".1"; + System.out.println( myAtoi(s)); + } +} \ No newline at end of file