first commit
This commit is contained in:
7
l33tcode_addtwonumbers/CMakeLists.txt
Normal file
7
l33tcode_addtwonumbers/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
#include(${CMAKE_CURRENT_LIST_DIR}/C/CMakeLists.txt)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/Cpp/CMakeLists.txt)
|
||||
if (!WIN32)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/Fortran/CMakeLists.txt)
|
||||
endif()
|
||||
33
l33tcode_addtwonumbers/Cpp/CMakeLists.txt
Normal file
33
l33tcode_addtwonumbers/Cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# Modify only these if one source file!
|
||||
project(Cppl33tcode_addtwonumbers)
|
||||
set(CURRENT_PROJECT_CODE_NAME l33tcode_addtwonumbers)
|
||||
set(FILE_EXT cpp)
|
||||
# End
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
# We want all the warnings and as errors enabled
|
||||
if (MSVC)
|
||||
# warning level 4 and all warnings as errors
|
||||
add_compile_options(/W4 /WX)
|
||||
else()
|
||||
# lots of warnings and all warnings as errors
|
||||
add_compile_options(-Wall -Wextra -pedantic -Werror)
|
||||
endif()
|
||||
|
||||
add_executable(${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/${CURRENT_PROJECT_CODE_NAME}.${FILE_EXT})
|
||||
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_INCLUDES}
|
||||
)
|
||||
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_LIBS}
|
||||
)
|
||||
|
||||
|
||||
126
l33tcode_addtwonumbers/Cpp/leetcode_addtwonumbers.cpp
Normal file
126
l33tcode_addtwonumbers/Cpp/leetcode_addtwonumbers.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
// AddTwoNumbers.cpp : This file contains the 'main' function. Program execution begins and ends there.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode* next;
|
||||
ListNode() : val(0), next(nullptr) {}
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||
|
||||
void print()
|
||||
{
|
||||
ListNode* head = this;
|
||||
while (head)
|
||||
{
|
||||
printf("(%d)->", head->val);
|
||||
head = head->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
private:
|
||||
static inline void iterate(ListNode*& l1, ListNode*& l2)
|
||||
{
|
||||
l1 = l1->next;
|
||||
l2 = l2->next;
|
||||
}
|
||||
|
||||
static inline void iterate(ListNode*& l)
|
||||
{
|
||||
l = l->next;
|
||||
}
|
||||
|
||||
public:
|
||||
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
|
||||
ListNode* left = l1, *right = l2;
|
||||
ListNode* toRet = new ListNode();
|
||||
ListNode* current = toRet, *prev = nullptr;
|
||||
|
||||
int carryover = 0;
|
||||
|
||||
short val = (left->val + right->val);
|
||||
current->val = val % 10;
|
||||
carryover = val >= 10 ? 1 : 0;
|
||||
current->next = nullptr;
|
||||
prev = current;
|
||||
iterate(current);
|
||||
|
||||
iterate(left, right);
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (left && right)
|
||||
{
|
||||
short val = (carryover + left->val + right->val);
|
||||
carryover = val >= 10 ? 1 : 0;
|
||||
current = new ListNode(val % 10, nullptr);
|
||||
prev->next = current;
|
||||
prev = current;
|
||||
iterate(current);
|
||||
|
||||
iterate(left, right);
|
||||
}
|
||||
else if (left || right)
|
||||
{
|
||||
ListNode** toAdd = left ? &left : &right;
|
||||
while (*toAdd)
|
||||
{
|
||||
short val = (carryover + (*toAdd)->val);
|
||||
carryover = val >= 10 ? 1 : 0;
|
||||
current = new ListNode(val % 10, nullptr);
|
||||
|
||||
prev->next = current;
|
||||
prev = current;
|
||||
iterate(current);
|
||||
|
||||
iterate(*toAdd);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (carryover)
|
||||
{
|
||||
current = new ListNode(1, nullptr);
|
||||
|
||||
prev->next = current;
|
||||
prev = current;
|
||||
iterate(current);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return toRet;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
ListNode num1(9, new ListNode(9, nullptr)); // 32
|
||||
ListNode num2(9, new ListNode(9, new ListNode(9, nullptr))); // 54
|
||||
|
||||
num1.print();
|
||||
num2.print();
|
||||
|
||||
Solution s;
|
||||
s.addTwoNumbers(&num1, &num2)->print();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
|
||||
// Debug program: F5 or Debug > Start Debugging menu
|
||||
|
||||
// Tips for Getting Started:
|
||||
// 1. Use the Solution Explorer window to add/manage files
|
||||
// 2. Use the Team Explorer window to connect to source control
|
||||
// 3. Use the Output window to see build output and other messages
|
||||
// 4. Use the Error List window to view errors
|
||||
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
|
||||
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
|
||||
25
l33tcode_addtwonumbers/Rust/leetcode_addtwonumbers.rs
Normal file
25
l33tcode_addtwonumbers/Rust/leetcode_addtwonumbers.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
// Definition for singly-linked list.
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ListNode {
|
||||
pub val: i32,
|
||||
pub next: Option<Box<ListNode>>
|
||||
}
|
||||
|
||||
impl ListNode {
|
||||
#[inline]
|
||||
fn new(val: i32) -> Self {
|
||||
ListNode {
|
||||
next: None,
|
||||
val
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Solution {
|
||||
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, World!");
|
||||
}
|
||||
Reference in New Issue
Block a user