Skip to content

Commit f0f7db5

Browse files
committed
init commit
0 parents  commit f0f7db5

19 files changed

+515
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

cpp/1.two-sum.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode id=1 lang=cpp
3+
*
4+
* [1] Two Sum
5+
*/
6+
7+
// @lc code=start
8+
class Solution
9+
{
10+
public:
11+
vector<int> twoSum(vector<int> &nums, int target)
12+
{
13+
std::cout << "twoSum" << std::endl;
14+
std::unordered_map<int, int> map;
15+
for (int i = 0; i < nums.size(); i++)
16+
{
17+
map[nums[i]] = i;
18+
}
19+
20+
for (int i = 0; i < nums.size(); i++)
21+
{
22+
int complement = target - nums[i];
23+
if (map.find(complement) != map.end() && map[complement] != i)
24+
{
25+
return {i, map[complement]};
26+
}
27+
}
28+
return {};
29+
}
30+
};
31+
// @lc code=end

cpp/13.roman-to-integer.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* @lc app=leetcode id=13 lang=cpp
3+
*
4+
* [13] Roman to Integer
5+
*/
6+
#include <iostream>
7+
#include <string>
8+
// @lc code=start
9+
class Solution
10+
{
11+
public:
12+
int romanToInt(string s)
13+
{
14+
std::cout >> s;
15+
return 0;
16+
}
17+
};
18+
// @lc code=end

cpp/20.valid-parentheses.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @lc app=leetcode id=20 lang=cpp
3+
*
4+
* [20] Valid Parentheses
5+
*/
6+
#include <stdio.h>
7+
#include <string>
8+
// @lc code=start
9+
class Solution
10+
{
11+
public:
12+
bool isValid(string s)
13+
{
14+
}
15+
};
16+
// @lc code=end

cpp/21.merge-two-sorted-lists.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* @lc app=leetcode id=21 lang=cpp
3+
*
4+
* [21] Merge Two Sorted Lists
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* struct ListNode {
11+
* int val;
12+
* ListNode *next;
13+
* ListNode() : val(0), next(nullptr) {}
14+
* ListNode(int x) : val(x), next(nullptr) {}
15+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
16+
* };
17+
*/
18+
19+
#include <iostream>
20+
21+
class Solution
22+
{
23+
public:
24+
ListNode *mergeTwoLists(ListNode *list1, ListNode *list2)
25+
{
26+
27+
if (list1 == nullptr)
28+
return list2;
29+
if (list2 == nullptr)
30+
return list1;
31+
32+
ListNode *head = nullptr;
33+
ListNode *tail = nullptr;
34+
35+
while (list1 != nullptr && list2 != nullptr)
36+
{
37+
if (list1->val < list2->val)
38+
{
39+
if (head == nullptr)
40+
{
41+
head = list1;
42+
tail = list1;
43+
}
44+
else
45+
{
46+
tail->next = list1;
47+
tail = tail->next;
48+
}
49+
list1 = list1->next;
50+
}
51+
else
52+
{
53+
if (head == nullptr)
54+
{
55+
head = list2;
56+
tail = list2;
57+
}
58+
else
59+
{
60+
tail->next = list2;
61+
tail = tail->next;
62+
}
63+
list2 = list2->next;
64+
}
65+
}
66+
67+
if (list1 == nullptr)
68+
{
69+
tail->next = list2;
70+
}
71+
else
72+
{
73+
tail->next = list1;
74+
}
75+
76+
return head;
77+
}
78+
};
79+
// @lc code=end

java/20.valid-parentheses.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Stack;
2+
3+
/*
4+
* @lc app=leetcode id=20 lang=java
5+
*
6+
* [20] Valid Parentheses
7+
*/
8+
9+
// @lc code=start
10+
class Solution {
11+
public boolean isValid(String s) {
12+
Stack<Character> stack = new Stack<>();
13+
for (char c : s.toCharArray()) {
14+
// if the character is a closing bracket, check if the stack is empty or not
15+
// if the stack is empty, it means the string is not valid
16+
if (c == '(' || c == '[' || c == '{') {
17+
stack.push(c);
18+
} else {
19+
if (stack.isEmpty()) {
20+
return false;
21+
}
22+
char top = stack.pop();
23+
if (c == ')' && top != '(') {
24+
return false;
25+
}
26+
if (c == ']' && top != '[') {
27+
return false;
28+
}
29+
if (c == '}' && top != '{') {
30+
return false;
31+
}
32+
}
33+
}
34+
return stack.isEmpty();
35+
36+
}
37+
}
38+
// @lc code=end

java/21.merge-two-sorted-lists.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
/*
3+
* @lc app=leetcode id=21 lang=java
4+
*
5+
* [21] Merge Two Sorted Lists
6+
*/
7+
import java.util.*;
8+
9+
// @lc code=start
10+
/**
11+
* Definition for singly-linked list.
12+
* public class ListNode {
13+
* int val;
14+
* ListNode next;
15+
* ListNode() {}
16+
* ListNode(int val) { this.val = val; }
17+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
18+
* }
19+
*/
20+
class Solution {
21+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
22+
23+
}
24+
}
25+
// @lc code=end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @lc app=leetcode id=387 lang=java
3+
*
4+
* [387] First Unique Character in a String
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int firstUniqChar(String s) {
10+
11+
}
12+
}
13+
// @lc code=end
14+

js/13.roman-to-integer.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode id=13 lang=javascript
3+
*
4+
* [13] Roman to Integer
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @return {number}
11+
*/
12+
var romanToInt = function (s) {
13+
const map = {
14+
I: 1,
15+
V: 5,
16+
X: 10,
17+
L: 50,
18+
C: 100,
19+
D: 500,
20+
M: 1000,
21+
};
22+
let result = 0;
23+
for (let i = 0; i < s.length; i++) {
24+
const cur = s[i];
25+
const next = s[i + 1];
26+
if (map[cur] < map[next]) {
27+
result -= map[cur];
28+
} else {
29+
result += map[cur];
30+
}
31+
}
32+
return result;
33+
};
34+
// @lc code=end

js/14.longest-common-prefix.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode id=14 lang=javascript
3+
*
4+
* [14] Longest Common Prefix
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string[]} strs
10+
* @return {string}
11+
*/
12+
var longestCommonPrefix = function (strs) {
13+
if (strs.length === 0) return "";
14+
if (strs.length === 1) return strs[0];
15+
let prefix = strs[0];
16+
for (let i = 1; i < strs.length; i++) {
17+
while (strs[i].indexOf(prefix) !== 0) {
18+
prefix = prefix.slice(0, prefix.length - 1);
19+
if (prefix.length === 0) return "";
20+
}
21+
}
22+
return prefix;
23+
};
24+
// @lc code=end

0 commit comments

Comments
 (0)