diff --git a/.gitignore b/.gitignore index adb36c8..93e3946 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.exe \ No newline at end of file +*.exe +*.out \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index a031f9a..a095222 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -1,19 +1,12 @@ { "configurations": [ { - "name": "Win32", + "name": "Linux", "includePath": [ "${workspaceFolder}/**" ], - "defines": [ - "_DEBUG", - "UNICODE", - "_UNICODE" - ], - "compilerPath": "E:\\Applications\\MSYS2\\ucrt64\\bin\\g++.exe", - "cStandard": "c17", - "cppStandard": "c++17", - "intelliSenseMode": "windows-gcc-x64" + "defines": [], + "compilerPath": "/usr/bin/g++" } ], "version": 4 diff --git a/.vscode/launch.json b/.vscode/launch.json index 18b70d7..b66a0ae 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,29 +1,26 @@ { - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "g++.exe - Build and debug active file", - "type": "cppdbg", - "request": "launch", - "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", - "args": [], - "stopAtEntry": false, - "cwd": "${fileDirname}", - "environment": [], - "externalConsole": false, - "MIMode": "gdb", - "miDebuggerPath": "E:\\Applications\\MSYS2\\ucrt64\\bin\\gdb.exe", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ], - "preLaunchTask": "C/C++: g++.exe build active file" + "version": "0.2.0", + "configurations": [ + { + "name": "g++ - Build and debug active file", + "type": "cppdbg", + "request": "launch", + "program": "${fileDirname}/${fileBasenameNoExtension}.out", + "args": [], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true } - ] + ], + "preLaunchTask": "C/C++: g++ build active file", + "miDebuggerPath": "/usr/bin/gdb" + } + ] } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index bfb3b57..164b1d6 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -1,28 +1,28 @@ { - "version": "2.0.0", - "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: g++.exe build active file", - "command": "E:\\Applications\\MSYS2\\ucrt64\\bin\\g++.exe", - "args": [ - "-fdiagnostics-color=always", - "-g", - "${file}", - "-o", - "${fileDirname}\\${fileBasenameNoExtension}.exe" - ], - "options": { - "cwd": "E:\\Applications\\MSYS2\\ucrt64\\bin" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "detail": "compiler: E:\\Applications\\MSYS2\\ucrt64\\bin\\g++.exe" - } - ] + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}.out" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" } \ No newline at end of file diff --git a/10-238.c b/10-238.c new file mode 100644 index 0000000..a30db23 --- /dev/null +++ b/10-238.c @@ -0,0 +1,42 @@ +#include +#include + +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ +int* productExceptSelf(int* nums, int numsSize, int* returnSize) { + int *left = (int *)malloc(numsSize * sizeof(int)); + int *right = (int *)malloc(numsSize * sizeof(int)); + int par_prod = 1; + for (int i = 0; i < numsSize; ++i) { + par_prod *= nums[i]; + left[i] = par_prod; + } + + par_prod = 1; + for (int i = numsSize - 1; i >= 0; --i) { + par_prod *= nums[i]; + right[i] = par_prod; + } + + int *result = (int *)malloc(numsSize * sizeof(int)); + result[0] = right[1]; + result[numsSize - 1] = left[numsSize - 2]; + for (int i = 1; i < numsSize - 1; ++i) { + result[i] = left[i - 1] * right[i + 1]; + } + *returnSize = numsSize; + return result; +} + +int main() { + int nums[] = {1, 2, 3, 4}; + int result_size; + int *result = productExceptSelf(nums, sizeof(nums) / sizeof(int), &result_size); + for (int i = 0; i < result_size; ++i) { + printf("%d ", result[i]); + } + printf("\n"); + free(result); + return 0; +} \ No newline at end of file