File tree 3 files changed +166
-0
lines changed
3 files changed +166
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+
3
+
4
+
5
+ // 2. Leetcode add two numbers
6
+
7
+
8
+ // Definition for singly-linked list.
9
+ struct ListNode {
10
+ int val;
11
+ ListNode *next;
12
+ ListNode () : val(0 ), next(nullptr ) {}
13
+ ListNode (int x) : val(x), next(nullptr ) {}
14
+ ListNode (int x, ListNode *next) : val(x), next(next) {}
15
+ };
16
+
17
+ class Solution {
18
+ public:
19
+ ListNode* addTwoNumbers (ListNode* l1, ListNode* l2) {
20
+
21
+ ListNode* head = new ListNode ();
22
+ ListNode* current = head;
23
+ int carry = 0 ;
24
+
25
+ while (l1 != nullptr || l2 != nullptr || carry != 0 ) {
26
+ int sum = carry;
27
+ if (l1 != nullptr ) {
28
+ sum += l1->val ;
29
+ l1 = l1->next ;
30
+ }
31
+ if (l2 != nullptr ) {
32
+ sum += l2->val ;
33
+ l2 = l2->next ;
34
+ }
35
+ current->next = new ListNode (sum % 10 );
36
+ carry = sum / 10 ;
37
+ current = current->next ;
38
+ }
39
+
40
+ return head->next ;
41
+ }
42
+ };
43
+
44
+ int main ()
45
+ {
46
+ Solution solution;
47
+
48
+ ListNode* l1 = new ListNode (2 );
49
+ l1->next = new ListNode (4 );
50
+ l1->next ->next = new ListNode (3 );
51
+
52
+ ListNode* l2 = new ListNode (5 );
53
+ l2->next = new ListNode (6 );
54
+ l2->next ->next = new ListNode (4 );
55
+
56
+ ListNode* result = solution.addTwoNumbers (l1, l2);
57
+
58
+ while (result != nullptr ) {
59
+ std::cout << result->val << " " ;
60
+ result = result->next ;
61
+ }
62
+
63
+
64
+ return 0 ;
65
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+
3
+
4
+
5
+ class Solution {
6
+ public:
7
+ int lengthOfLongestSubstring (std::string s) {
8
+
9
+ std::string subString = " " ;
10
+ int max =0 ;
11
+
12
+ for (char c : s)
13
+ {
14
+ if (subString.find (c) == std::string::npos)
15
+ {
16
+ subString += c;
17
+ if (subString.length () > max)
18
+ {
19
+ max = subString.length ();
20
+ }
21
+
22
+ }
23
+ else
24
+ {
25
+ subString = " " ;
26
+ subString += c;
27
+ }
28
+
29
+ }
30
+ return max;
31
+ }
32
+ };
33
+
34
+
35
+
36
+ int main ()
37
+ {
38
+ std::string s = " abcabcbbabcdef" ;
39
+ Solution sol;
40
+ std::cout << sol.lengthOfLongestSubstring (s) << std::endl;
41
+ return 0 ;
42
+ }
Original file line number Diff line number Diff line change
1
+ #include < vector>
2
+ #include < iostream>
3
+ #include < unordered_map>
4
+
5
+
6
+ class Solution {
7
+
8
+ public:
9
+ std::vector<int > twoSum (std::vector<int >& nums, int target) {
10
+
11
+ std::vector<int > result;
12
+
13
+ for (int i = 0 ; i < nums.size (); i++) {
14
+ for (int j = i + 1 ; j < nums.size (); j++) {
15
+ if (nums[i] + nums[j] == target) {
16
+ result.push_back (i);
17
+ result.push_back (j);
18
+ return result;
19
+ }
20
+ }
21
+ }
22
+
23
+ return result;
24
+ }
25
+
26
+ // another solution with unordered map
27
+ std::vector<int > twoSum2 (std::vector<int >& nums, int target) {
28
+
29
+ std::unordered_map<int , int > map;
30
+ std::vector<int > result;
31
+ for (int i = 0 ; i < nums.size (); i++) {
32
+ int complement = target - nums[i];
33
+ if (map.find (complement) != map.end ()) {
34
+ result.push_back (map[complement]);
35
+ result.push_back (i);
36
+ return result;
37
+ }
38
+ map[nums[i]] = i;
39
+ }
40
+
41
+
42
+
43
+ };
44
+
45
+ int main () {
46
+
47
+ Solution solution;
48
+
49
+ std::vector<int > nums = {2 , 7 , 11 , 15 };
50
+ int target = 18 ;
51
+
52
+ std::vector<int > result = solution.twoSum (nums, target);
53
+
54
+ for (auto i : result) {
55
+ std::cout << i << std::endl;
56
+ }
57
+
58
+ return 0 ;
59
+ }
You can’t perform that action at this time.
0 commit comments