8000 Solved more problems · yashkathe/DSA-with-Javascript@04cebac · GitHub
[go: up one dir, main page]

Skip to c 8000 ontent

Commit 04cebac

Browse files
committed
Solved more problems
1 parent 1551312 commit 04cebac

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function reversePrint(llist) {
2+
3+
if(!llist) return
4+
reversePrint(llist.next)
5+
console.log(llist.data)
6+
7+
}

A2 Recursion/problems/sum-of-array.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Given an array of integers, find the sum of its elements.
3+
*/
4+
5+
function simpleArraySum(ar) {
6+
7+
if(ar.length === 0) return 0
8+
9+
return ar[0] + simpleArraySum(ar.slice(1))
10+
11+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
3+
4+
Return the running sum of nums.
5+
6+
7+
8+
Example 1:
9+
10+
Input: nums = [1,2,3,4]
11+
Output: [1,3,6,10]
12+
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
13+
14+
Example 2:
15+
16+
Input: nums = [1,1,1,1,1]
17+
Output: [1,2,3,4,5]
18+
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
19+
20+
Example 3:
21+
22+
Input: nums = [3,1,2,10,1]
23+
Output: [3,4,6,16,17]
24+
25+
26+
27+
Constraints:
28+
29+
1 <= nums.length <= 1000
30+
-10^6 <= nums[i] <= 10^6
31+
32+
*/
33+
34+
var runningSum = function(nums) {
35+
36+
for(i=0;i<nums.length -1;i++){
37+
38+
nums[i+1] = nums[i] + nums[i+1]
39+
}
40+
41+
return nums
42+
43+
};
44+
45+
/*
46+
used two pointers to keep on addidng i and i+1 and going further
47+
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Create a function stringify which accepts an argument list / $list and returns a string representation of the list. The string representation of the list starts with the value of the current Node, specified by its data / $data / Data property, followed by a whitespace character, an arrow and another whitespace character (" -> "), followed by the rest of the list. The end of the string representation of a list must always end with null / NULL / None / nil / nullptr / null() ( all caps or all lowercase depending on the language you are undertaking this Kata in ). For example, given the following list:
3+
4+
new Node(1, new Node(2, new Node(3)))
5+
6+
... its string representation would be:
7+
8+
"1 -> 2 -> 3 -> null"
9+
10+
And given the following linked list:
11+
12+
new Node(0, new Node(1, new Node(4, new Node(9, new Node(16)))))
13+
14+
... its string representation would be:
15+
16+
"0 -> 1 -> 4 -> 9 -> 16 -> null"
17+
*/
18+
19+
20+
// recursion
21+
22+
function stringify(list) {
23+
24+
let str = ""
25+
26+
const recurse = (llist) => {
27+
28+
if(!llist) {
29+
str += "null"
30+
return str + "null"
31+
}
32+
33+
str = str + `${llist.data} -> `
34+
recurse(llist.next)
35+
36+
}
37+
38+
recurse(list)
39+
40+
console.log(str)
41+
return str
42+
43+
44+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ _-With JavaScript and TypeScript_
3939
- [Hash Tables](./A12%20Hash%20Tables)
4040
- [Hash Functions](./A12%20Hash%20Tables/hash-functions.js)
4141
- [Hash Tables](./A12%20Hash%20Tables/hash-tables.js)
42+
- [Graphs](./A13%20Graphs/graph.js)
4243

4344
#### Leet Code Problems
4445

@@ -49,6 +50,7 @@ _-With JavaScript and TypeScript_
4950
- [Reverse Array](./B1%20LEETCODE-Easy/Arrays/reverse-array.js)
5051
- [Shuffle Array](./B1%20LEETCODE-Easy/Arrays/suffle-array.js)
5152
- [Number of good pairs](./B1%20LEETCODE-Easy/Arrays/number-of-good-pairs.js)
53+
- [Running sum of 1d array](./B1%20LEETCODE-Easy/Arrays/running-sum-1d.js)
5254
- [Binary Search](./B1%20LEETCODE-Easy/Binary%20Search/)
5355
- [Count negative numbers in a sorted matrix](./B1%20LEETCODE-Easy/Binary%20Search/count-negatives.js)
5456
- [Binary Search Tree](./B1%20LEETCODE-Easy/Binary-Search-Tree/)
@@ -63,12 +65,14 @@ _-With JavaScript and TypeScript_
6365
- [Palindrome Linked List](./B1%20LEETCODE-Easy/Linked%20List/palindrome-linked-list.js)
6466
- [Remove duplicates from sorted list](./B1%20LEETCODE-Easy/Linked%20List/remove-duplicates-from-sorted-list.js)
6567
- [Remove linked list elements](./B1%20LEETCODE-Easy/Linked%20List/remove-linked-list-elements.js)
68+
- [Print linked list as string](./B1%20LEETCODE-Easy/Linked%20List/linked_list-to-string.js)
6669
- [Merging and Sorting](./B1%20LEETCODE-Easy/Merging%20and%20Sorting/)
6770
- [Merge two sorted lists](./B1%20LEETCODE-Easy/Merging%20and%20Sorting/merge_two_sorted_lists.js)
6871
- [Remove duplicates from sorted array](./B1%20LEETCODE-Easy/Merging%20and%20Sorting/remove_duplicates_from_sorted_array.js)
6972
- [Recursion](./B1%20LEETCODE-Easy/Recursion/)
7073
- [Power of 2](./B1%20LEETCODE-Easy/Recursion/power-of-2.js)
7174
- [Power of 4](./B1%20LEETCODE-Easy/Recursion/power-of-4.js)
75+
- [Print linked list as string](./B1%20LEETCODE-Easy/Linked%20List/linked_list-to-string.js)
7276
- [Strings](./B1%20LEETCODE-Easy/String/)
7377
- [Two Sum](./B1%20LEETCODE-Easy/String/two-sum.js)
7478
- [Palindrome Number](./B1%20LEETCODE-Easy/String/palindrome-number.js)

0 commit comments

Comments
 (0)
0