diff --git a/1-two-sum/1-two-sum.cs b/1-two-sum/1-two-sum.cs new file mode 100644 index 0000000..f10906e --- /dev/null +++ b/1-two-sum/1-two-sum.cs @@ -0,0 +1,14 @@ +public class Solution { + public int[] TwoSum(int[] nums, int target) { + Dictionary map = new Dictionary(); + for (int i = 0; i < nums.Length; i++) { + int complement = target - nums[i]; + if (map.ContainsKey(complement)) { + return new int[] { map[complement], i }; + } + map[nums[i]] = i; + } + throw new Exception("No two sum solution"); +} + +} \ No newline at end of file diff --git a/1-two-sum/1-two-sum.js b/1-two-sum/1-two-sum.js new file mode 100644 index 0000000..c8eba31 --- /dev/null +++ b/1-two-sum/1-two-sum.js @@ -0,0 +1,15 @@ +const twoSum = function (nums, target) +{ + //search of all Array + for (let i = 0; i < nums.length; i++) + { + for (let j = i + 1; j < nums.length; j++) + { + // if (nums[j] === target - nums[i]) + if(nums[i] + nums[j] === target) + { + return [i, j]; + } + } + } + }; \ No newline at end of file diff --git a/1-two-sum/1-two-sum.php b/1-two-sum/1-two-sum.php new file mode 100644 index 0000000..a1e96d4 --- /dev/null +++ b/1-two-sum/1-two-sum.php @@ -0,0 +1,23 @@ + \ No newline at end of file diff --git a/1-two-sum/1-two-sum.rb b/1-two-sum/1-two-sum.rb new file mode 100644 index 0000000..b9c59e8 --- /dev/null +++ b/1-two-sum/1-two-sum.rb @@ -0,0 +1,8 @@ +def two_sum(nums, target) + hash = {} + nums.each_with_index do |num, idx| + complement = target - num + return [hash[complement], idx] if hash.key?(complement) + hash[num] = idx + end +end \ No newline at end of file diff --git a/2-add-two-numbers/2-add-two-numbers.cs b/2-add-two-numbers/2-add-two-numbers.cs new file mode 100644 index 0000000..60a03f6 --- /dev/null +++ b/2-add-two-numbers/2-add-two-numbers.cs @@ -0,0 +1,39 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int x) { val = x; } + * } + */ +public class Solution +{ + public ListNode AddTwoNumbers(ListNode l1, ListNode l2) + { + ListNode dummy = new ListNode(0); + ListNode current = dummy; + int carry = 0; + + while (l1 != null || l2 != null || carry != 0) + { + int sum = carry; + if (l1 != null) + { + sum += l1.val; + l1 = l1.next; + } + if (l2 != null) + { + sum += l2.val; + l2 = l2.next; + } + + carry = sum / 10; + sum = sum % 10; + current.next = new ListNode(sum); + current = current.next; + } + + return dummy.next; + } +} \ No newline at end of file diff --git a/2-add-two-numbers/2-add-two-numbers.java b/2-add-two-numbers/2-add-two-numbers.java new file mode 100644 index 0000000..ae864e0 --- /dev/null +++ b/2-add-two-numbers/2-add-two-numbers.java @@ -0,0 +1,36 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + ListNode dummy = new ListNode(0); + ListNode current = dummy; + int carry = 0; + + while (l1 != null || l2 != null || carry != 0) { + int sum = carry; + if (l1 != null) { + sum += l1.val; + l1 = l1.next; + } + if (l2 != null) { + sum += l2.val; + l2 = l2.next; + } + + carry = sum / 10; + sum = sum % 10; + current.next = new ListNode(sum); + current = current.next; + } + + return dummy.next; + } +} \ No newline at end of file diff --git a/2-add-two-numbers/2-add-two-numbers.js b/2-add-two-numbers/2-add-two-numbers.js new file mode 100644 index 0000000..84505b7 --- /dev/null +++ b/2-add-two-numbers/2-add-two-numbers.js @@ -0,0 +1,36 @@ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ +var addTwoNumbers = function(l1, l2) { + let dummy = new ListNode(0); + let current = dummy; + let carry = 0; + + while (l1 !== null || l2 !== null || carry !== 0) { + let sum = carry; + if (l1 !== null) { + sum += l1.val; + l1 = l1.next; + } + if (l2 !== null) { + sum += l2.val; + l2 = l2.next; + } + + carry = Math.floor(sum / 10); + sum = sum % 10; + current.next = new ListNode(sum); + current = current.next; + } + + return dummy.next; +}; \ No newline at end of file diff --git a/2-add-two-numbers/2-add-two-numbers.php b/2-add-two-numbers/2-add-two-numbers.php new file mode 100644 index 0000000..f32b781 --- /dev/null +++ b/2-add-two-numbers/2-add-two-numbers.php @@ -0,0 +1,46 @@ +val = $val; + * $this->next = $next; + * } + * } + */ +class Solution { + /** + * @param ListNode $l1 + * @param ListNode $l2 + * @return ListNode + */ + function addTwoNumbers($l1, $l2) { + $dummy = new ListNode(0); + $current = $dummy; + $carry = 0; + + while ($l1 !== null || $l2 !== null || $carry !== 0) { + $sum = $carry; + if ($l1 !== null) { + $sum += $l1->val; + $l1 = $l1->next; + } + if ($l2 !== null) { + $sum += $l2->val; + $l2 = $l2->next; + } + + $carry = intdiv($sum, 10); + $sum = $sum % 10; + $current->next = new ListNode($sum); + $current = $current->next; + } + + return $dummy->next; + } +} + +?> \ No newline at end of file diff --git a/2-add-two-numbers/2-add-two-numbers.rb b/2-add-two-numbers/2-add-two-numbers.rb new file mode 100644 index 0000000..728dc52 --- /dev/null +++ b/2-add-two-numbers/2-add-two-numbers.rb @@ -0,0 +1,36 @@ +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val = 0, _next = nil) +# @val = val +# @next = _next +# end +# end + +# @param {ListNode} l1 +# @param {ListNode} l2 +# @return {ListNode} +def add_two_numbers(l1, l2) + dummy = ListNode.new(0) + current = dummy + carry = 0 + + while l1 || l2 || carry != 0 + sum = carry + if l1 + sum += l1.val + l1 = l1.next + end + if l2 + sum += l2.val + l2 = l2.next + end + + carry = sum / 10 + sum = sum % 10 + current.next = ListNode.new(sum) + current = current.next + end + + dummy.next +end \ No newline at end of file diff --git a/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.cs b/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.cs new file mode 100644 index 0000000..bbbc6f2 --- /dev/null +++ b/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.cs @@ -0,0 +1,22 @@ +public class Solution +{ + public int LengthOfLongestSubstring(string s) + { + int n = s.Length; + HashSet set = new HashSet(); + int maxLength = 0, i = 0, j = 0; + while (i < n && j < n) + { + if (!set.Contains(s[j])) + { + set.Add(s[j++]); + maxLength = Math.Max(maxLength, j - i); + } + else + { + set.Remove(s[i++]); + } + } + return maxLength; + } +} \ No newline at end of file diff --git a/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.java b/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.java new file mode 100644 index 0000000..b86dd3a --- /dev/null +++ b/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.java @@ -0,0 +1,16 @@ +class Solution { + public int lengthOfLongestSubstring(String s) { + int n = s.length(); + Set set = new HashSet<>(); + int maxLength = 0, i = 0, j = 0; + while (i < n && j < n) { + if (!set.contains(s.charAt(j))) { + set.add(s.charAt(j++)); + maxLength = Math.max(maxLength, j - i); + } else { + set.remove(s.charAt(i++)); + } + } + return maxLength; + } +} \ No newline at end of file diff --git a/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.js b/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.js new file mode 100644 index 0000000..1c9277e --- /dev/null +++ b/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.js @@ -0,0 +1,14 @@ +var lengthOfLongestSubstring = function(s) { + let n = s.length; + let set = new Set(); + let maxLength = 0, i = 0, j = 0; + while (i < n && j < n) { + if (!set.has(s.charAt(j))) { + set.add(s.charAt(j++)); + maxLength = Math.max(maxLength, j - i); + } else { + set.delete(s.charAt(i++)); + } + } + return maxLength; +}; \ No newline at end of file diff --git a/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.php b/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.php new file mode 100644 index 0000000..ea8e77c --- /dev/null +++ b/3-longest-substring-without-repeating-characters/3-longest-substring-without-repeating-characters.php @@ -0,0 +1,28 @@ + \ No newline at end of file