---
# System prepended metadata

title: 計算機概論 - 作業 3 程式語言十八般武藝
tags: [報告, 計算機概論]

---

---
title: 計算機概論 - 作業 3 程式語言十八般武藝
---

# 作業 3 程式語言十八般武藝

> 作者 : 李信緯
> 學號 : D1211450
> e-mail : mdwiwi0130@gmil.com
> [name=naiye130]
> [time=Fri, Oct 13, 2023 11:39 PM]

### 使用題目
:::success
#### Two Sum
https://leetcode.com/problems/two-sum/
:::

### C
```C=
int* twoSum(int* nums, int  N, int target, int* returnSize){
    int *arr = malloc(2*sizeof(int));
    *returnSize = 2;
    for(int i=0; i < N-1; i++){
        for(int j=i+1; j < N; j++){
            if(nums[i] + nums[j] == target){
                arr[0] = i; arr[1] = j;
                return arr;
            }
        }
    }
    return arr;
}
```
### CPP
```CPP=
class Solution {
public:
    int x,y;
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> n;
        for(int i=0;i<nums.size();i++){
            for(int j=i+1;j<nums.size();j++){
                if(nums[i]+nums[j] == target)
                    return {i,j};
            }
        }
        return {};
    }
};
```
### C#
```csharp=
using System;
using System.Collections.Generic;

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;
        }
        return new int[0];
    }
}
```
### JAVA
```java=
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{};
    }
}
```
### JAVASCRIPT
```javascript=

var twoSum = function(nums, target) {

    const respuesta = new Map();

    for (let i = 0; i < nums.length; i++) {
      const complemento = target - nums[i];
      if (respuesta.has(complemento)) {
        return [i, respuesta.get(complemento)];
      } else {
        respuesta.set(nums[i], i);
      }
    }
    return null;
};
```
### PYTHON
```python=
class Solution(object):
    def twoSum(self, nums, target):
        seen = {}
        for i in range(len(nums)):
            diff = target - nums[i]
            if diff in seen:
                return [seen[diff], i]
            else:
                seen[nums[i]] = i
```
### GO
```go=
func twoSum(nums []int, target int) []int {
    for i := 0; i < len(nums)-1; i++ {
        for j := i+1; j < len(nums); j++ {
            if nums[i] + nums[j] == target {
                return []int{i, j}
            }
        }
    }
    return []int{}
}
```
###### tags: `計算機概論` `報告`

{%hackmd @naiye130/__style %}