Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions 1-two-sum/1-two-sum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Solution {
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> map = new Dictionary<int, int>();
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");
}

}
15 changes: 15 additions & 0 deletions 1-two-sum/1-two-sum.js
Original file line number Diff line number Diff line change
@@ -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];
}
}
}
};
25 changes: 25 additions & 0 deletions 1-two-sum/1-two-sum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php


class Solution {

/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer[]
*/
function twoSum($nums, $target) {
$map = [];
for ($i = 0; $i < count($nums); $i++) {
$complement = $target - $nums[$i];
if (isset($map[$complement])) {
return [$map[$complement], $i];
}
$map[$nums[$i]] = $i;
}
}

}


?>
8 changes: 8 additions & 0 deletions 1-two-sum/1-two-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
d = {}
for i, num in enumerate(nums):
if target - num in d:
return [d[target - num], i]
d[num] = i
return []
8 changes: 8 additions & 0 deletions 1-two-sum/1-two-sum.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions 2-add-two-numbers/2-add-two-numbers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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;
}
}
36 changes: 36 additions & 0 deletions 2-add-two-numbers/2-add-two-numbers.js
Original file line number Diff line number Diff line change
@@ -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;
};
48 changes: 48 additions & 0 deletions 2-add-two-numbers/2-add-two-numbers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php


/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->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;
}
}


?>
27 changes: 27 additions & 0 deletions 2-add-two-numbers/2-add-two-numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode(0)
current = dummy
carry = 0

while l1 or l2 or carry:
sum = carry
if l1:
sum += l1.val
l1 = l1.next
if l2:
sum += l2.val
l2 = l2.next

carry = sum // 10
sum = sum % 10
current.next = ListNode(sum)
current = current.next

return dummy.next
36 changes: 36 additions & 0 deletions 2-add-two-numbers/2-add-two-numbers.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php


class Solution {

/**
* @param String $s
* @return Integer
*/
function lengthOfLongestSubstring($s) {
$n = strlen($s);
$set = array();
$maxLength = 0;
$i = 0;
$j = 0;
while ($i < $n && $j < $n) {
if (!in_array($s[$j], $set)) {
$set[] = $s[$j++];
$maxLength = max($maxLength, $j - $i);
} else {
unset($set[array_search($s[$i++], $set)]);
}
}
return $maxLength;
}

}


?>