forked from Fabayu/HACKTOBERFEST-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode
239 lines (160 loc) · 4.57 KB
/
Leetcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
--------------------------------------------------------------------------------------------------------
Problem 1 :
Link to question : https://leetcode.com/problems/two-sum/
Solution :
In Python 3:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1,len(nums)):
sum=nums[i]+nums[j]
if sum==target:
return i,j
return []
--------------------------------------------------------------------------------------------------------
Problem 2 :
Link : https://leetcode.com/problems/reverse-linked-list/
Solution
CPP
ListNode* reverseList(ListNode* head) {
if( head==NULL || head->next==NULL){
return head;
}
ListNode* newnode=reverseList(head->next);
head->next->next=head;
head->next=NULL;
return newnode;
}
------------------------------------------------------------------------------------------------------------
Problem 3:
Link : https://leetcode.com/problems/first-bad-version/
solution
CPP
int firstBadVersion(int n) {
int low=1,high=n;
int mid=0;;
while(low<=high){
mid=low+(high-low)/2;
if(low==high) return mid;
if(isBadVersion(mid))
high=mid;
else
low=mid+1;
}
return-1;
}
---------------------------------------------------------------------------------------------------------------
Problem 4:
link : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
solution
CPP
int maxProfit(vector<int>& prices) {
int lsf = INT_MAX;
int op = 0;
int pist = 0;
for(int i = 0; i < prices.size(); i++){
if(prices[i] < lsf){
lsf = prices[i];
}
pist = prices[i] - lsf;
if(op < pist){
op = pist;
}
}
return op;
}
---------------------------------------------------------------------------------------------------------------
Problem 9:
link : https://leetcode.com/problems/palindrome-number/
solution
CPP
class Solution {
long long convert(int x){
//reversing can exceed the allowed INTEGER domain for 32-bit.
long long ans=0;
while(x>0){
ans = ans*10+x%10;
x=x/10;
}
return ans;
}
public:
bool isPalindrome(int x) {
if(x<0) return false;
//getting the reverse number and checking whether reversed_number==original_number
long long ans=convert(x);
//typeCasting for Checking
return ans==(long long)(x);
}
};
---------------------------------------------------------------------------------------------------------------
Problem 704:
link : https://leetcode.com/problems/binary-search/
solution
C
int search(int* nums, int numsSize, int target){
if (numsSize == 1 && nums[0] == target)
return 0;
int min , max , mid ;
min=0;
max=numsSize-1;
while(min<=max)
{ mid=(min+max)/2;
if(nums[mid]==target)
{
// printf("%d",target);
return mid;
}
else if(target > nums[mid])
{
min=mid+1;
}
else
{
max=mid-1;
}
}
return -1;
}
----------------------------------------------------------------------------------------------------------------------------
Problem 42:
link : https://leetcode.com/problems/trapping-rain-water/
solution
java
import java.util.*;
class Solution {
public int trap(int[] height) {
int result = 0;
if(height==null || height.length<=2)
return result;
int left[] = new int[height.length];
int right[]= new int[height.length];
//scan from left to right
int max = height[0];
left[0] = height[0];
for(int i=1; i<height.length; i++){
if(height[i]<max){
left[i]=max;
}else{
left[i]=height[i];
max = height[i];
}
}
//scan from right to left
max = height[height.length-1];
right[height.length-1]=height[height.length-1];
for(int i=height.length-2; i>=0; i--){
if(height[i]<max){
right[i]=max;
}else{
right[i]=height[i];
max = height[i];
}
}
//calculate totoal
for(int i=0; i<height.length; i++){
result+= Math.min(left[i],right[i])-height[i];
}
return result;
}
}