add prob 10
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
*.exe
|
*.exe
|
||||||
|
*.out
|
||||||
13
.vscode/c_cpp_properties.json
vendored
13
.vscode/c_cpp_properties.json
vendored
@@ -1,19 +1,12 @@
|
|||||||
{
|
{
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "Win32",
|
"name": "Linux",
|
||||||
"includePath": [
|
"includePath": [
|
||||||
"${workspaceFolder}/**"
|
"${workspaceFolder}/**"
|
||||||
],
|
],
|
||||||
"defines": [
|
"defines": [],
|
||||||
"_DEBUG",
|
"compilerPath": "/usr/bin/g++"
|
||||||
"UNICODE",
|
|
||||||
"_UNICODE"
|
|
||||||
],
|
|
||||||
"compilerPath": "E:\\Applications\\MSYS2\\ucrt64\\bin\\g++.exe",
|
|
||||||
"cStandard": "c17",
|
|
||||||
"cppStandard": "c++17",
|
|
||||||
"intelliSenseMode": "windows-gcc-x64"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"version": 4
|
"version": 4
|
||||||
|
|||||||
11
.vscode/launch.json
vendored
11
.vscode/launch.json
vendored
@@ -1,21 +1,17 @@
|
|||||||
{
|
{
|
||||||
// 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",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "g++.exe - Build and debug active file",
|
"name": "g++ - Build and debug active file",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
|
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
|
||||||
"args": [],
|
"args": [],
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"cwd": "${fileDirname}",
|
"cwd": "${fileDirname}",
|
||||||
"environment": [],
|
"environment": [],
|
||||||
"externalConsole": false,
|
"externalConsole": false,
|
||||||
"MIMode": "gdb",
|
"MIMode": "gdb",
|
||||||
"miDebuggerPath": "E:\\Applications\\MSYS2\\ucrt64\\bin\\gdb.exe",
|
|
||||||
"setupCommands": [
|
"setupCommands": [
|
||||||
{
|
{
|
||||||
"description": "Enable pretty-printing for gdb",
|
"description": "Enable pretty-printing for gdb",
|
||||||
@@ -23,7 +19,8 @@
|
|||||||
"ignoreFailures": true
|
"ignoreFailures": true
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"preLaunchTask": "C/C++: g++.exe build active file"
|
"preLaunchTask": "C/C++: g++ build active file",
|
||||||
|
"miDebuggerPath": "/usr/bin/gdb"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
14
.vscode/tasks.json
vendored
14
.vscode/tasks.json
vendored
@@ -1,19 +1,18 @@
|
|||||||
{
|
{
|
||||||
"version": "2.0.0",
|
|
||||||
"tasks": [
|
"tasks": [
|
||||||
{
|
{
|
||||||
"type": "cppbuild",
|
"type": "cppbuild",
|
||||||
"label": "C/C++: g++.exe build active file",
|
"label": "C/C++: g++ build active file",
|
||||||
"command": "E:\\Applications\\MSYS2\\ucrt64\\bin\\g++.exe",
|
"command": "/usr/bin/g++",
|
||||||
"args": [
|
"args": [
|
||||||
"-fdiagnostics-color=always",
|
"-fdiagnostics-color=always",
|
||||||
"-g",
|
"-g",
|
||||||
"${file}",
|
"${file}",
|
||||||
"-o",
|
"-o",
|
||||||
"${fileDirname}\\${fileBasenameNoExtension}.exe"
|
"${fileDirname}/${fileBasenameNoExtension}.out"
|
||||||
],
|
],
|
||||||
"options": {
|
"options": {
|
||||||
"cwd": "E:\\Applications\\MSYS2\\ucrt64\\bin"
|
"cwd": "${fileDirname}"
|
||||||
},
|
},
|
||||||
"problemMatcher": [
|
"problemMatcher": [
|
||||||
"$gcc"
|
"$gcc"
|
||||||
@@ -22,7 +21,8 @@
|
|||||||
"kind": "build",
|
"kind": "build",
|
||||||
"isDefault": true
|
"isDefault": true
|
||||||
},
|
},
|
||||||
"detail": "compiler: E:\\Applications\\MSYS2\\ucrt64\\bin\\g++.exe"
|
"detail": "Task generated by Debugger."
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"version": "2.0.0"
|
||||||
}
|
}
|
||||||
42
10-238.c
Normal file
42
10-238.c
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user