-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSnippets.js
More file actions
351 lines (322 loc) · 10.9 KB
/
Copy pathSnippets.js
File metadata and controls
351 lines (322 loc) · 10.9 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
export const Articles = [
{
"uuid": "98879124-986e-4a08-887f-5e918e89a558",
"title": "Sum of numbers in a string",
"tags": ["String"],
"language": "python",
"short_intro": " Function calculates the sum of all the numbers in the given string",
"contributor_name": "dinesh bhuwad",
"contribution_date": "19/2/2022",
"content": `
The function calculates the sum of all the numbers in the given string.
- The function traverses through all the characters if the string.
- If the chracter is a number, the function adds its value to the result.
- Multiple consecutive numbers are considered as one number.
`,
"code": `# Sum of numbers in a string
def findSum(str1):
temp = "0"
Sum = 0
for ch in str1:
if (ch.isdigit()):
temp += ch
else:
Sum += int(temp)
temp = "0"
return Sum + int(temp)
`,
"examples": `#examples
print(findSum("12ab20z40")) #72
print(findSum("120nb")) #120
print(findSum("1m2n2")) #5
`
}, {
"uuid": "a7ce11fb-a603-405e-b79c-13b13c7ce694",
"title": "Spiral Traversal of Matrix ",
"tags": ["Matrix"],
"language": "java",
"short_intro": "Function prints the elements of the string in a spiral sequence",
"contributor_name": "Dinesh Bhuwad",
"contribution_date": "5/9/2022",
"content": `
The function prints the elements of the string in a spiral sequence.
- It uses four pointers - right,left,top & bottom.
- If traverses in a clockwise direction.
`,
"code": `
static ArrayList<Integer> spirallyTraverse(int matrix[][], int r, int c)
{
int top,bottom,left,right,i;
top=0;
bottom=r-1;
left=0;
right=c-1;
int div = 0;
ArrayList<Integer> a = new ArrayList<>();
while(top<=bottom && left<=right){
if(div==0){
for(i=left; i<=right; i++){
a.add(matrix[top][i]);
}
top=top+1;
}
if(div==1){
for(i=top; i<=bottom; i++){
a.add(matrix[i][right]);
}
right=right-1;
}
if(div==2){
for(i=right; i>=left; i--){
a.add(matrix[bottom][i]);
}
bottom=bottom-1;
}
if(div==3){
for(i=bottom; i>=top; i--){
a.add(matrix[i][left]);
}
left=left+1;
}
div = (div+1)%4;
}
return a;
}
`,
"examples": `#examples
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
# 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
# 1 2 3 4 8 12 11 10 9 5 6 7
`
}, {
"uuid": "08a729df-9b1f-4572-94cf-fb918fd2a557",
"title": "Search a 2D Matrix",
"tags": ["Matrix"],
"language": "java",
"short_intro": "Function searchs for a number in the given integer matrix",
"contributor_name": "dinesh bhuwad",
"contribution_date": "5/9/2022",
"content": `
The function searchs for a number in the given integer matrix.
- The function returns a boolean true if the number is present and false if the value is not present.
`,
"code": `# Searching an element in the matrix
public boolean searchMatrix(int[][] matrix, int target) {
for(int i=0;i<matrix.length;i++){
if(matrix[i][0]>target) return false;
for(int j=0;j<matrix[i].length;j++){
if(matrix[i][j]==target) return true;
if(matrix[i][j]>target) return false;
}
}
return false;
}
`,
"examples": `#examples
matrix = [[1,3,5,7],
[10,11,16,20],
[23,30,34,60]],
target = 3
# true
matrix = [[1,3,5,7],
[10,11,16,20],
[23,30,34,60]],
target = 13
# false
`
}, {
"uuid": "641b5f3f-9cc0-4b02-b914-21dff4f1bcf7",
"title": "Median in a row-wise sorted Matrix",
"tags": ["Matrix"],
"language": "java",
"short_intro": " Function returns the median for the row wise sorted matrix",
"contributor_name": "dinesh bhuwad",
"contribution_date": "5/9/2022",
"content": `
The function returns the median for the row wise sorted matrix.
- Given a row wise sorted matrix of size RxC where R and C are always odd, the function returns the median of the matrix.
`,
"code": `# Median in a row-wise sorted Matrix
int median(int matrix[][], int r, int c) {
ArrayList<Integer>b = new ArrayList<>();
for(int i = 0; i < r ;i++){
for(int j = 0; j < c;j++){
b.add(matrix[i][j]);
}
}
Collections.sort(b);
int a = b.size()/2;
return b.get(a);
}
`,
"examples": `#examples
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
# 5
R = 3, C = 1
M = [[1], [2], [3]]
# 2`
},
{
"uuid": "0f620672-b230-4c1b-8d60-74c1cc4dff21",
"title": "Square root of number without using inbuilt function",
"tags": ["Math"],
"language": "java",
"short_intro": " Function calculates the sum of all the numbers in the given string",
"contributor_name": "Janak Avhad",
"contribution_date": "19/2/2022",
"content": `
Given a non-negative integer x, compute and return the square root of x.
Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.
`,
"code": `# Square root of number without using inbuilt function
class Solution {
public int mySqrt(int x) {
if (x == 0 || x == 1) return x;
int start=0;
int end=x;
while(start<=end){
int mid=start+(end-start)/2;
//long val=mid*mid;
if(mid == x / mid){
return mid;
}
else if(mid > x / mid){
end=mid-1;
}
else{
start=mid+1;
}
}
return end;
}
}
`,
"examples": `#examples
Example 1:
Input: x = 4
Output: 2
Example 2:
Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
`
}, {
"uuid": "da9c607e-98e7-4f96-b892-dca06dba10d9",
"title": "Reverse a LinkedList ",
"tags": ["linkedlist"],
"language": "java",
"short_intro": " The given function reverses the given linkedlist",
"contributor_name": "dinesh bhuwad",
"contribution_date": "7/9/2022",
"content": `
The function reverses the given linkedlist.
- The time complexity of the function is O(N).
- The space complexity of the function is O(1).
`,
"code": `
Node reverse(Node node)
{
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
`,
"examples": `#examples
Input : 1->2->3->4->NULL
Output : 4->3->2->1->NULL
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
`
}, {
"uuid": "6da2db53-d0a4-48b8-b0c1-7e1d2b83d0b6",
"title": "Detect loops in a given linkedlist",
"tags": ["Linkedlist"],
"language": "java",
"short_intro": " Function calculates the sum of all the numbers in the given string",
"contributor_name": "dinesh bhuwad",
"contribution_date": "7/9/2022",
"content": `
The function returns a boolean value after checking if loop is present in the linkedlist.
- The time complexity of the function is O(N).
- The space complexity of the function is O(1).
`,
"code": `
public static boolean detectLoop(Node head){
Node slow = head;
Node fast = head;
while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
return true;
}
}
return false;
}
`,
"examples": `#examples
N = 3
value[] = {1,3,4}
x(position at which tail is connected) = 2
Output: True
Explanation: In above test case N = 3.
The linked list with nodes N = 3 is
given. Then value of x=2 is given which
means last node is connected with xth
node of linked list. Therefore, there
exists a loop.
`
},
{
"uuid": "c79aa6ea-f31e-4a40-9079-aee38aad77ad",
"title": "inorder traversal ",
"tags": ["BST"],
"language": "java",
"short_intro": "function that accepts a tree as input and prints out in-order representation of tree",
"contributor_name": "legit person",
"contribution_date": "25/10/2022 ",
"content": `
Inorder traversal is a traversal technique for binary trees. It traverses the trees in the following order : left-center-right.
TC : O(N)
SC:O(N)
`,
"code": `
public static void inorder(Node root){
if (root == null) {
return;
}
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
`,
"examples": `
Input 132
Output 321
Input 10,20,30,40,60,50
Output: 40 20 60 10 50 30
`
},
]
export const tags = ["string"]