Skip to content

Commit 78ed6cc

Browse files
Merge branch 'PawanJaiswal08:master' into master
2 parents 448b3b8 + f1eb53f commit 78ed6cc

29 files changed

+2063
-0
lines changed

1.Two-sum.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
for (int i = 0; i < nums.length; i++) {
5+
int x = nums[i];
6+
if (map.containsKey(target - x)) {
7+
return new int[]{map.get(target - x), i};
8+
}
9+
map.put(x, i);
10+
}
11+
throw new IllegalArgumentException("No two sum solution");
12+
}
13+
}

11. Container With Most Water

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int maxArea(vector<int>& height) {
4+
int maxstored = INT_MIN;
5+
int waterfilled;
6+
int s = 0;
7+
int e = height.size()-1;
8+
while(s<=e)
9+
{
10+
waterfilled = (e-s)*(min(height[s], height[e]));
11+
maxstored = max(waterfilled, maxstored);
12+
if(height[s]<height[e]) s++;
13+
else e--;
14+
}
15+
return maxstored;
16+
}
17+
};

112. PathSum.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/path-sum
2+
// 98% efficient solution
3+
4+
class Solution {
5+
public:
6+
int pathSum = 0;
7+
bool pathFound = false;
8+
9+
bool hasPathSum(TreeNode* root, int targetSum)
10+
{
11+
if(root != NULL)
12+
{
13+
pathSum += root->val;
14+
15+
if(root->left == NULL && root->right == NULL)
16+
{
17+
if(pathSum == targetSum)
18+
pathFound = true;
19+
}
20+
else
21+
{
22+
hasPathSum(root->left, targetSum);
23+
hasPathSum(root->right, targetSum);
24+
}
25+
26+
pathSum -= root->val;
27+
}
28+
29+
return (pathFound == true) ? true : false;
30+
}
31+
};

12.Integer to Roman.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String intToRoman(int num) {
3+
Map<Integer, String> map = new HashMap();
4+
map.put(1, "I"); map.put(5, "V"); map.put(10, "X");
5+
map.put(50, "L"); map.put(100, "C"); map.put(500, "D"); map.put(1000, "M");
6+
map.put(4, "IV"); map.put(9, "IX"); map.put(40, "XL"); map.put(90, "XC");
7+
map.put(400, "CD"); map.put(900, "CM");
8+
9+
int[] sequence = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
10+
11+
StringBuffer sb = new StringBuffer();
12+
for (int i = 0; i<sequence.length; i++) {
13+
int base = sequence[i];
14+
15+
while (num >= base) {
16+
sb.append(map.get(base));
17+
num -= base;
18+
}
19+
}
20+
21+
return sb.toString();
22+
}
23+
}

132-pattern.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool find132pattern(vector<int>& nums) {
4+
int ak = numeric_limits<int>::min();
5+
stack<int> st;
6+
for (int i = nums.size() - 1; i >= 0; --i) {
7+
if (nums[i] < ak) {
8+
return true;
9+
} else {
10+
while (!st.empty() && nums[i] > st.top()) {
11+
ak = st.top(), st.pop();
12+
}
13+
}
14+
st.emplace(nums[i]);
15+
}
16+
return false;
17+
}
18+
};

2.Add-Two-Numbers.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
public class Solution {
3+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
4+
ListNode dummyHead = new ListNode(0);
5+
ListNode p = l1, q= l2, curr = dummyHead;
6+
int carry = 0;
7+
while (p != null || q!= null) {
8+
int x = (p != null) ? p.val : 0;
9+
int y = (q != null) ? q.val : 0;
10+
int digit = carry + x + y;
11+
carry = digit / 10;
12+
curr.next = new ListNode(digit % 10);
13+
curr = curr.next;
14+
if (p != null) p = p.next;
15+
if (q != null) q = q.next;
16+
}
17+
if (carry > 0) {
18+
curr.next = new ListNode(carry);
19+
}
20+
return dummyHead.next;
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> rearrangeArray(vector<int>& nums) {
4+
int x= nums.size();
5+
vector<int> a(x);
6+
int i=0,j=1;
7+
for(int k=0;k<x;k++){
8+
if(nums[k]>0){
9+
a[i]=nums[k];
10+
i+=2;
11+
}
12+
else{
13+
a[j]=nums[k];
14+
j+=2;
15+
}
16+
}
17+
return a;
18+
}
19+
};

268.Missing_Number.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int missingNumber(int[] nums) {
3+
int n = nums.length;
4+
int sum = (n * (n+1)) / 2;
5+
for(int i=0; i<n; i++) {
6+
sum -= nums[i];
7+
}
8+
return sum;
9+
}
10+
}

3.Longest_Substring.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public int lengthOfLongestSubstring(String s) {
3+
int[] charMap = new int[256];
4+
Arrays.fill(charMap, -1);
5+
int i = 0, maxLen = 0;
6+
for (int j = 0; j < s.length(); j++) {
7+
if (charMap[s.charAt(j)] >= i) {
8+
i = charMap[s.charAt(j)] + 1;
9+
}
10+
charMap[s.charAt(j)] = j;
11+
maxLen = Math.max(j - i + 1, maxLen);
12+
}
13+
return maxLen;
14+
}
15+
}

39. Combination Sum

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
void helper(vector<int>& candidates, int target, int sumtillnow, vector<int>&subset, vector<vector<int>>&ans, int i)
4+
{
5+
if(sumtillnow==target)
6+
{
7+
ans.push_back(subset);
8+
return;
9+
}
10+
if(sumtillnow>target) return;
11+
if(i>=candidates.size()) return;
12+
helper(candidates, target, sumtillnow, subset, ans, i+1);
13+
//pick
14+
sumtillnow+=candidates[i];
15+
subset.push_back(candidates[i]);
16+
helper(candidates, target, sumtillnow, subset, ans, i);
17+
sumtillnow-=candidates[i];
18+
subset.pop_back();
19+
20+
}
21+
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
22+
vector<int>subset;
23+
vector<vector<int>>ans;
24+
int sumtillnow=0;
25+
sort(candidates.begin(), candidates.end());
26+
helper(candidates, target, sumtillnow, subset, ans, 0);
27+
return ans;
28+
}
29+
};

0 commit comments

Comments
 (0)