File tree 3 files changed +52
-0
lines changed
3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > twoSum (vector<int >& nums, int target) {
4
+ int n = nums.size ();
5
+ vector<int > store;
6
+ unordered_map<int ,int > s;
7
+ for (int i = 0 ; i < n; i++)
8
+ {
9
+ /*
10
+ -> create a hashmap to store
11
+ -> nums[i] = complement or target - nums[i];
12
+ -> store initial index
13
+ -> find if complement present in array
14
+ -> yes , store its index and current i 's index
15
+ -> return
16
+ */
17
+ int complement = target-nums[i];
18
+ if (s.find (complement) != s.end ())
19
+ {
20
+ store.push_back (s[complement]);
21
+ store.push_back (i);
22
+ return store;
23
+ }
24
+ s[nums[i]] = i;
25
+ }
26
+ return store;
27
+ }
28
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ ListNode *addTwoNumbers (ListNode *l1, ListNode *l2) {
4
+ ListNode dummy (0 );
5
+ ListNode* p = &dummy;
6
+ int carry = 0 , sum;
7
+ while (l1 || l2) {
8
+ sum = carry;
9
+ if (l1) sum += l1->val ;
10
+ if (l2) sum += l2->val ;
11
+ if (sum >= 10 ) {
12
+ carry = sum / 10 ;
13
+ sum %= 10 ;
14
+ } else carry = 0 ;
15
+ p->next = new ListNode (sum);
16
+ p = p->next ;
17
+ if (l1) l1 = l1->next ;
18
+ if (l2) l2 = l2->next ;
19
+ }
20
+ if (carry)
21
+ p->next = new ListNode (carry);
22
+ return dummy.next ;
23
+ }
24
+ };
You can’t perform that action at this time.
0 commit comments