Skip to content

Commit 3509aea

Browse files
committed
Updated solutions
1 parent 7b719a2 commit 3509aea

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class ContainerWithMostWater {
2+
public int maxArea(int[] height) {
3+
int max=Integer.MIN_VALUE;
4+
5+
int i=0,j=height.length-1;
6+
while(i<j)
7+
{
8+
max=Math.max(Math.min(height[i],height[j]) * (j-i) , max);
9+
10+
if(height[i]<height[j]) i++;
11+
else if(height[j] < height[i]) j--;
12+
else if(height[i]==height[j])
13+
{
14+
i++;j--;
15+
}
16+
else
17+
break;
18+
}
19+
return max;
20+
}
21+
}

java/arrays/MajorityElement.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class MajorityElement {
2+
public int majorityElement(int[] nums) {
3+
int count=1, majority=nums[0];
4+
5+
for(int i=1;i<nums.length;i++)
6+
{
7+
if(nums[i]==majority)
8+
count++;
9+
else
10+
count--;
11+
12+
if(count==0)
13+
{
14+
count=1;
15+
majority=nums[i];
16+
}
17+
}
18+
return majority;
19+
}
20+
}

java/arrays/TwoSum.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class TwoSum {
2+
public int[] twoSum(int[] nums, int target) {
3+
int i,j;
4+
HashMap<Integer,Integer> cache=new HashMap<Integer,Integer>();
5+
int[] a = new int[2];
6+
for(i = 0; i<nums.length; i++)
7+
{
8+
if(cache.containsKey(nums[i]))
9+
{
10+
a[0] = cache.get(nums[i]);
11+
a[1] = i;
12+
break;
13+
}
14+
else
15+
cache.put(target-nums[i],i);
16+
}
17+
return a;
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class MergeTwoLists {
12+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
13+
if(list1==null) return list2;
14+
if(list2==null) return list1;
15+
if(list1.val<list2.val)
16+
{
17+
list1.next=mergeTwoLists(list1.next,list2);
18+
return list1;
19+
}
20+
else
21+
{
22+
list2.next=mergeTwoLists(list1,list2.next);
23+
return list2;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)