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
34 changes: 34 additions & 0 deletions 0002-add-two-numbers/0002-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 0002-add-two-numbers/0002-add-two-numbers.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
36 changes: 36 additions & 0 deletions 0002-add-two-numbers/0002-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;
};
46 changes: 46 additions & 0 deletions 0002-add-two-numbers/0002-add-two-numbers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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 0002-add-two-numbers/0002-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 0002-add-two-numbers/0002-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
38 changes: 38 additions & 0 deletions 0006-zigzag-conversion/0006-zigzag-conversion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public String convert(String s, int numRows) {
//Define StringBuilders
StringBuilder[] sbs = new StringBuilder[numRows];
for(int i = 0; i < numRows; i++)
{
sbs[i] = new StringBuilder();
}

//Define Variables
char[] arr = s.toCharArray();
int n = arr.length;
int index = 0;

//Tranverse zig-zag
while(index < n)
{
//Go down
for(int j = 0; j < numRows && index < n; j++)
{
sbs[j].append(arr[index++]);
}
//Go up Before Start
for(int j = numRows - 2; j > 0 && index < n; j--)
{
sbs[j].append(arr[index++]);
}
}

//Combine all stringbuilders into one
StringBuilder res = sbs[0];
for(int i = 1; i < numRows; i++)
{
res.append(sbs[i].toString());
}
return res.toString();
}
}
20 changes: 20 additions & 0 deletions 0006-zigzag-conversion/0006-zigzag-conversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function convert(s, numRows) {
if (numRows === 1 || numRows >= s.length) {
return s;
}

let rows = Array.from({ length: numRows }, () => '');
let idx = 0, step = 1;

for (let c of s) {
rows[idx] += c;
if (idx === 0) {
step = 1;
} else if (idx === numRows - 1) {
step = -1;
}
idx += step;
}

return rows.join('');
}
34 changes: 34 additions & 0 deletions 0006-zigzag-conversion/0006-zigzag-conversion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

class Solution {

/**
* @param String $s
* @param Integer $numRows
* @return String
*/
function convert($s, $numRows) {
if ($numRows == 1 || $numRows >= strlen($s)) {
return $s;
}

$rows = array_fill(0, $numRows, '');
$idx = 0;
$step = 1;

for ($i = 0; $i < strlen($s); $i++) {
$rows[$idx] .= $s[$i];
if ($idx == 0) {
$step = 1;
} else if ($idx == $numRows - 1) {
$step = -1;
}
$idx += $step;
}

return implode('', $rows);
}

}

?>
15 changes: 15 additions & 0 deletions 0006-zigzag-conversion/0006-zigzag-conversion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def convert(s, numRows)
return s if numRows == 1 || numRows >= s.length
rows = Array.new(numRows) { "" }
idx, step = 0, 1
s.each_char do |c|
rows[idx] += c
if idx == 0
step = 1
elsif idx == numRows - 1
step = -1
end
idx += step
end
rows.join("")
end
22 changes: 22 additions & 0 deletions 11-container-with-most-water/11-container-with-most-water.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Solution
{
public int MaxArea(int[] height)
{
int left = 0;
int right = height.Length - 1;
int maxArea = 0;
while (left < right)
{
maxArea = Math.Max(maxArea, Math.Min(height[left], height[right]) * (right - left));
if (height[left] < height[right])
{
left++;
}
else
{
right--;
}
}
return maxArea;
}
}
16 changes: 16 additions & 0 deletions 11-container-with-most-water/11-container-with-most-water.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int maxArea = 0;
while (left < right) {
maxArea = Math.max(maxArea, Math.min(height[left], height[right]) * (right - left));
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}
}
Loading