Skip to content

Commit 81c55d6

Browse files
committed
Add day1 C++ solution for leetcode 989.
1 parent 9aa32ff commit 81c55d6

11 files changed

+459
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Win32",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"defines": [
9+
"_DEBUG",
10+
"UNICODE",
11+
"_UNICODE"
12+
],
13+
"windowsSdkVersion": "10.0.19041.0",
14+
"compilerPath": "C:/Program Files/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/g++.exe",
15+
"cStandard": "c11",
16+
"cppStandard": "c++17",
17+
"intelliSenseMode": "gcc-x86"
18+
}
19+
],
20+
"version": 4
21+
}

91algo/cpp/.vscode/launch.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
"version": "0.2.0",
5+
"configurations": [
6+
{
7+
"name": "g++.exe - Build and debug active file",
8+
"type": "cppdbg",
9+
"request": "launch",
10+
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
11+
"args": [],
12+
"stopAtEntry": false,
13+
"cwd": "${workspaceFolder}",
14+
"environment": [],
15+
"externalConsole": false,
16+
"MIMode": "gdb",
17+
"miDebuggerPath": "gdb",
18+
"setupCommands": [
19+
{ // Display content in STL containers pretty
20+
"description": "Enable pretty-printing for gdb",
21+
"text": "-enable-pretty-printing",
22+
"ignoreFailures": true
23+
}
24+
],
25+
// VS Code debug的日志信息,能清楚gdb加载和运行的各种过程
26+
"logging": {
27+
"moduleLoad": false,
28+
"trace": true,
29+
"engineLogging": true,
30+
"exceptions": true
31+
},
32+
"preLaunchTask": "C/C++: g++.exe build active file"
33+
}
34+
]
35+
}

91algo/cpp/.vscode/settings.json

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"files.associations": {
3+
"*.vue": "vue",
4+
"deque": "cpp",
5+
"string": "cpp",
6+
"vector": "cpp",
7+
"algorithm": "cpp",
8+
"unordered_set": "cpp",
9+
"queue": "cpp",
10+
"iterator": "cpp",
11+
"iostream": "cpp",
12+
"array": "cpp",
13+
"atomic": "cpp",
14+
"*.tcc": "cpp",
15+
"bitset": "cpp",
16+
"cctype": "cpp",
17+
"cfenv": "cpp",
18+
"charconv": "cpp",
19+
"chrono": "cpp",
20+
"cinttypes": "cpp",
21+
"clocale": "cpp",
22+
"cmath": "cpp",
23+
"codecvt": "cpp",
24+
"complex": "cpp",
25+
"condition_variable": "cpp",
26+
"csetjmp": "cpp",
27+
"csignal": "cpp",
28+
"cstdarg": "cpp",
29+
"cstddef": "cpp",
30+
"cstdint": "cpp",
31+
"cstdio": "cpp",
32+
"cstdlib": "cpp",
33+
"cstring": "cpp",
34+
"ctime": "cpp",
35+
"cuchar": "cpp",
36+
"cwchar": "cpp",
37+
"cwctype": "cpp",
38+
"forward_list": "cpp",
39+
"list": "cpp",
40+
"unordered_map": "cpp",
41+
"exception": "cpp",
42+
"functional": "cpp",
43+
"map": "cpp",
44+
"memory": "cpp",
45+
"memory_resource": "cpp",
46+
"numeric": "cpp",
47+
"optional": "cpp",
48+
"random": "cpp",
49+
"ratio": "cpp",
50+
"regex": "cpp",
51+
"set": "cpp",
52+
"string_view": "cpp",
53+
"system_error": "cpp",
54+
"tuple": "cpp",
55+
"type_traits": "cpp",
56+
"utility": "cpp",
57+
"fstream": "cpp",
58+
"future": "cpp",
59+
"initializer_list": "cpp",
60+
"iomanip": "cpp",
61+
"iosfwd": "cpp",
62+
"istream": "cpp",
63+
"limits": "cpp",
64+
"mutex": "cpp",
65+
"new": "cpp",
66+
"ostream": "cpp",
67+
"scoped_allocator": "cpp",
68+
"shared_mutex": "cpp",
69+
"sstream": "cpp",
70+
"stdexcept": "cpp",
71+
"streambuf": "cpp",
72+
"thread": "cpp",
73+
"typeindex": "cpp",
74+
"typeinfo": "cpp",
75+
"valarray": "cpp"
76+
}
77+
}

91algo/cpp/.vscode/tasks.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
//"type": "shell",
6+
"label": "C/C++: g++.exe build active file",
7+
"command": "g++",
8+
"args": [
9+
"-g",
10+
"${file}",
11+
"-o",
12+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
13+
],
14+
"options": {
15+
"cwd": "${workspaceFolder}"
16+
},
17+
"problemMatcher": [
18+
"$gcc"
19+
],
20+
"group": {
21+
"kind": "build",
22+
"isDefault": true
23+
}
24+
}
25+
]
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
vector<int> addToArrayForm(vector<int>& A, int k) {
9+
if (k == 0) return A;
10+
vector<int> res;
11+
int n = A.size();
12+
// 对位相加
13+
int carry = 0;
14+
int sum = 0;
15+
int i = n - 1;
16+
while (k > 0 || i >= 0)
17+
{
18+
sum = carry + (k % 10);
19+
if (i >= 0) // 保证访问A[i]前不越界
20+
sum += A[i];
21+
22+
carry = (sum <= 9) ? 0 : 1;
23+
res.push_back(sum % 10);
24+
k = k / 10;
25+
26+
i--;
27+
}
28+
if (carry == 1) res.push_back(1);
29+
reverse(res.begin(), res.end());
30+
31+
return res;
32+
}
33+
};
34+
35+
// Test
36+
int main()
37+
{
38+
Solution sol;
39+
vector<int> A = {2, 7, 4};
40+
int K = 81;
41+
42+
auto res = sol.addToArrayForm(A, K);
43+
for (auto &num : res)
44+
cout << num << endl;
45+
46+
return 0;
47+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Win32",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"defines": [
9+
"_DEBUG",
10+
"UNICODE",
11+
"_UNICODE"
12+
],
13+
"windowsSdkVersion": "10.0.19041.0",
14+
"compilerPath": "C:/Program Files/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/g++.exe",
15+
"cStandard": "c11",
16+
"cppStandard": "c++17",
17+
"intelliSenseMode": "gcc-x86"
18+
}
19+
],
20+
"version": 4
21+
}

91algo/daily/.vscode/launch.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
"version": "0.2.0",
5+
"configurations": [
6+
{
7+
"name": "g++.exe - Build and debug active file",
8+
"type": "cppdbg",
9+
"request": "launch",
10+
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
11+
"args": [],
12+
"stopAtEntry": false,
13+
"cwd": "${workspaceFolder}",
14+
"environment": [],
15+
"externalConsole": false,
16+
"MIMode": "gdb",
17+
"miDebuggerPath": "gdb",
18+
"setupCommands": [
19+
{ // Display content in STL containers pretty
20+
"description": "Enable pretty-printing for gdb",
21+
"text": "-enable-pretty-printing",
22+
"ignoreFailures": true
23+
}
24+
],
25+
// VS Code debug的日志信息,能清楚gdb加载和运行的各种过程
26+
"logging": {
27+
"moduleLoad": false,
28+
"trace": true,
29+
"engineLogging": true,
30+
"exceptions": true
31+
},
32+
"preLaunchTask": "C/C++: g++.exe build active file"
33+
}
34+
]
35+
}

91algo/daily/.vscode/settings.json

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"files.associations": {
3+
"*.vue": "vue",
4+
"deque": "cpp",
5+
"string": "cpp",
6+
"vector": "cpp",
7+
"algorithm": "cpp",
8+
"unordered_set": "cpp",
9+
"queue": "cpp",
10+
"iterator": "cpp",
11+
"iostream": "cpp",
12+
"array": "cpp",
13+
"atomic": "cpp",
14+
"*.tcc": "cpp",
15+
"bitset": "cpp",
16+
"cctype": "cpp",
17+
"cfenv": "cpp",
18+
"charconv": "cpp",
19+
"chrono": "cpp",
20+
"cinttypes": "cpp",
21+
"clocale": "cpp",
22+
"cmath": "cpp",
23+
"codecvt": "cpp",
24+
"complex": "cpp",
25+
"condition_variable": "cpp",
26+
"csetjmp": "cpp",
27+
"csignal": "cpp",
28+
"cstdarg": "cpp",
29+
"cstddef": "cpp",
30+
"cstdint": "cpp",
31+
"cstdio": "cpp",
32+
"cstdlib": "cpp",
33+
"cstring": "cpp",
34+
"ctime": "cpp",
35+
"cuchar": "cpp",
36+
"cwchar": "cpp",
37+
"cwctype": "cpp",
38+
"forward_list": "cpp",
39+
"list": "cpp",
40+
"unordered_map": "cpp",
41+
"exception": "cpp",
42+
"functional": "cpp",
43+
"map": "cpp",
44+
"memory": "cpp",
45+
"memory_resource": "cpp",
46+
"numeric": "cpp",
47+
"optional": "cpp",
48+
"random": "cpp",
49+
"ratio": "cpp",
50+
"regex": "cpp",
51+
"set": "cpp",
52+
"string_view": "cpp",
53+
"system_error": "cpp",
54+
"tuple": "cpp",
55+
"type_traits": "cpp",
56+
"utility": "cpp",
57+
"fstream": "cpp",
58+
"future": "cpp",
59+
"initializer_list": "cpp",
60+
"iomanip": "cpp",
61+
"iosfwd": "cpp",
62+
"istream": "cpp",
63+
"limits": "cpp",
64+
"mutex": "cpp",
65+
"new": "cpp",
66+
"ostream": "cpp",
67+
"scoped_allocator": "cpp",
68+
"shared_mutex": "cpp",
69+
"sstream": "cpp",
70+
"stdexcept": "cpp",
71+
"streambuf": "cpp",
72+
"thread": "cpp",
73+
"typeindex": "cpp",
74+
"typeinfo": "cpp",
75+
"valarray": "cpp"
76+
}
77+
}

91algo/daily/.vscode/tasks.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
//"type": "shell",
6+
"label": "C/C++: g++.exe build active file",
7+
"command": "g++",
8+
"args": [
9+
"-g",
10+
"${file}",
11+
"-o",
12+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
13+
],
14+
"options": {
15+
"cwd": "${workspaceFolder}"
16+
},
17+
"problemMatcher": [
18+
"$gcc"
19+
],
20+
"group": {
21+
"kind": "build",
22+
"isDefault": true
23+
}
24+
}
25+
]
26+
}

0 commit comments

Comments
 (0)