Skip to content

Commit aefa6f4

Browse files
committed
first commit
0 parents  commit aefa6f4

8 files changed

+233
-0
lines changed

.gitignore

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# IDE
12+
.idea
13+
14+
# OS generated files
15+
.DS_Store
16+
.DS_Store?
17+
._*
18+
.Spotlight-V100
19+
ehthumbs.db
20+
Icon?
21+
Thumbs.db
22+
23+
# Babel ES6 compiles files
24+
dist
25+
26+
# Directory for instrumented libs generated by jscoverage/JSCover
27+
lib-cov
28+
29+
# Coverage directory used by tools like istanbul
30+
coverage
31+
32+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
33+
.grunt
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (http://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directory
42+
node_modules
43+
44+
# Optional npm cache directory
45+
.npm
46+
47+
# Optional REPL history
48+
.node_repl_history
49+
50+
# .env
51+
.env
52+
53+
#vs code
54+
.vscode

Algorithms/C/1-Two-Sum.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 1-Two Sum
2+
3+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
4+
5+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
6+
7+
**Example:**
8+
```
9+
Given nums = [2, 7, 11, 15], target = 9,
10+
11+
Because nums[0] + nums[1] = 2 + 7 = 9,
12+
return [0, 1].
13+
```
14+
15+
- Run Time: 102 ms
16+
```c
17+
/**
18+
* Note: The returned array must be malloced, assume caller calls free().
19+
*/
20+
int *twoSum(int *nums, int numsSize, int target){
21+
int i=0,j=0;
22+
int *arr=(int *)malloc(2*sizeof(int));
23+
for (i=0; i < numsSize;i++){
24+
for (j=1+i; j < numsSize;j++){
25+
if(nums[i]+nums[j]==target){
26+
arr[0]=i;
27+
arr[1]=j;
28+
return arr;
29+
}
30+
}
31+
}
32+
return arr;
33+
}
34+
```

Algorithms/C/344-Reverse-String.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 344-Reverse String
2+
3+
Write a function that takes a string as input and returns the string reversed.
4+
5+
**Example:**
6+
7+
Given s = "hello", return "olleh".
8+
9+
```c
10+
char* reverseString(char* s) {
11+
int i=strlen(s)-1,j=0;
12+
char* str=(char*)malloc(strlen(s)+1);
13+
for(;i>=0;i--)
14+
str[j++]=s[i];
15+
str[i]='\0';
16+
return str;
17+
}
18+
```
19+
20+
```c
21+
char* reverseString(char* s) {
22+
int i=strlen(s)-1;
23+
char* str=(char*)malloc(strlen(s)+1);
24+
while(i>=0){
25+
*str=s[i--];
26+
str++;
27+
}
28+
*str='\0';
29+
str-=strlen(s);
30+
31+
return str;
32+
}
33+
```

Algorithms/C/a.out

8.44 KB
Binary file not shown.

Algorithms/C/test.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<stdio.h>
2+
#include <stdlib.h>
3+
int *twoSum(int *nums, int numsSize, int target);
4+
int main()
5+
{
6+
int n[]={1,2,4};
7+
int *p = (int *)malloc(2 * sizeof(int));
8+
p=twoSum(n, 3, 3);
9+
printf("%d",p[0]);
10+
}
11+
/**
12+
* Note: The returned array must be malloced, assume caller calls free().
13+
*/
14+
int *twoSum(int *nums, int numsSize, int target){
15+
int i=0,j=0;
16+
int *arr=(int *)malloc(2*sizeof(int));
17+
for (; i < numsSize;i++){
18+
for (; j < numsSize;j++){
19+
if(nums[i]+nums[j]==target){
20+
arr[0]=i;
21+
arr[1]=j;
22+
return arr;
23+
}
24+
}
25+
}
26+
return arr;
27+
}

Algorithms/Java/1-Two-Sum.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 1-Two Sum
2+
3+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
4+
5+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
6+
7+
**Example:**
8+
```
9+
Given nums = [2, 7, 11, 15], target = 9,
10+
11+
Because nums[0] + nums[1] = 2 + 7 = 9,
12+
return [0, 1].
13+
```
14+
15+
##### Solution 1.
16+
17+
此方法最直覺用雙迴圈下去一序做比較
18+
19+
- 暴力、窮舉
20+
- Run Time: 41 ms
21+
- 時間複雜度 O(n<sup>2</sup>)
22+
- 空間複雜度 O(1)
23+
```java
24+
class Solution {
25+
public int[] twoSum(int[] nums, int target) {
26+
for(int i=0;i<nums.length;i++){
27+
for(int j=i+1;j<nums.length;j++){
28+
if(nums[i]+nums[j]==target){
29+
int arr[]={i,j};
30+
return arr;
31+
}
32+
}
33+
}
34+
return nums;
35+
}
36+
}
37+
```
38+
39+
##### Solution 2.
40+
41+
42+
43+
- One-pass Hash Table
44+
- Run Time: 8 ms
45+
- 時間複雜度 O(n)
46+
- 空間複雜度 O(n)
47+
```java
48+
class Solution {
49+
public int[] twoSum(int[] nums, int target) {
50+
Map<Integer,Integer> map=new HashMap<>();
51+
int arr[]=new int [2];
52+
for(int i=0;i<nums.length;i++) {
53+
if(map.containsKey(target-nums[i])) {
54+
arr[0]=map.get(target-nums[i]);
55+
arr[1]=i;
56+
return arr;
57+
}else {
58+
map.put(nums[i],i);
59+
}
60+
}
61+
return arr;
62+
}
63+
}
64+
```

Algorithms/Java/344-Reverse-String.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 344-Reverse String
2+
3+
Write a function that takes a string as input and returns the string reversed.
4+
5+
**Example:**
6+
7+
Given s = "hello", return "olleh".
8+
9+
10+
```java
11+
class Solution {
12+
public String reverseString(String s) {
13+
return new StringBuffer(s).reverse().toString();
14+
}
15+
}
16+
```

READEME.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Leetcode
2+
3+
solutions:
4+
5+
344. [Reverse String](https://leetcode.com/problems/reverse-string/description/) (Easy) [C](/Algorithms/C/344-Reverse-String.md) [Java](/Algorithms/Java/344-Reverse-String.md)

0 commit comments

Comments
 (0)