add prob 10

This commit is contained in:
2025-08-22 00:22:53 +08:00
parent eb2426e4de
commit febe7547bf
5 changed files with 96 additions and 63 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
*.exe
*.exe
*.out

View File

@@ -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

49
.vscode/launch.json vendored
View File

@@ -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"
}
]
}

52
.vscode/tasks.json vendored
View File

@@ -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"
}

42
10-238.c Normal file
View 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;
}