Skip to content

Commit fe0556a

Browse files
committed
添加了zhedahht/ChineseCodingInterviewAppendix书中源码...
1 parent 08b3dab commit fe0556a

64 files changed

Lines changed: 5710 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

011-数值的整数次方/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,26 @@ double PowerNormal(double base, int exponent)
198198
}
199199
```
200200

201-
当然也可以用递归的写法
201+
当然也可以用递归的写法
202+
203+
```cpp
204+
double PowerNormal(double base, int exponent)
205+
{
206+
if(exponent == 0)
207+
{
208+
return 1;
209+
}
210+
else if(exponent == 1)
211+
{
212+
return base;
213+
}
214+
double res = PowerNormal(base, exponent >> 1);
215+
res *= res;
216+
if((exponent & 1) == 1)
217+
{
218+
res *= base;
219+
}
220+
221+
return res;
222+
}
223+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <cmath>
3+
4+
using namespace std;
5+
6+
// µ÷ÊÔ¿ª¹Ø
7+
#define __tmain main
8+
9+
#ifdef __tmain
10+
11+
#define debug cout
12+
13+
#else
14+
15+
#define debug 0 && cout
16+
17+
#endif // __tmain
18+
19+
20+
21+
class Solutin
22+
{
23+
public :
24+
void Print(int n)
25+
{
26+
int max = pow(10, n);
27+
for(int i = 1; i < max; i++)
28+
{
29+
cout <<i <<endl;
30+
}
31+
}
32+
33+
};
34+
35+
36+
int __tmain( )
37+
{
38+
Solutin solu;
39+
40+
int n;
41+
while(cin >> n)
42+
{
43+
44+
solu.Print(n);
45+
}
46+
return 0;
47+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#链接
2+
-------
3+
4+
[牛客剑指Offer题目列表](http://www.nowcoder.com/ta/coding-interviews?page=)
5+
6+
[九度OJ剑指Offer题目列表](http://ac.jobdu.com/hhtproblems.php)
7+
8+
9+
| 题目 | 题解 | 代码 |
10+
| ------------- |:-------------:| -----:|
11+
| [二维数组中的查找](http://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&tqId=11154&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking) |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
// µ÷ÊÔ¿ª¹Ø
6+
#define __tmain main
7+
8+
#ifdef __tmain
9+
10+
#define debug cout
11+
12+
#else
13+
14+
#define debug 0 && cout
15+
16+
#endif // __tmain
17+
18+
19+
20+
21+
int __tmain( )
22+
{
23+
debug <<"test" <<endl;
24+
return 0;
25+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Question Description:
3+
* (Question 105 in <Coding Intervies>) Given an array A[0, 1, …, n-1], please construct an array
4+
* B[0, 1, …, n-1] in which B[i]=A[0]×A[1]×… ×A[i-1]×A[i+1]×…×A[n-1]. No division should be
5+
* involved to solve this problem.
6+
*/
7+
8+
#include <vector>
9+
#include <stdio.h>
10+
11+
using namespace std;
12+
13+
void multiply(const vector<double>& array1, vector<double>& array2)
14+
{
15+
int length1= array1.size();
16+
int length2 = array2.size();
17+
18+
if(length1 == length2 && length2 > 1)
19+
{
20+
array2[0] = 1;
21+
for(int i = 1; i < length1; ++i)
22+
{
23+
array2[i] = array2[i - 1] * array1[i - 1];
24+
}
25+
26+
double temp = 1;
27+
for(int i = length1 - 2; i >= 0; --i)
28+
{
29+
temp *= array1[i + 1];
30+
array2[i] *= temp;
31+
}
32+
}
33+
}
34+
35+
//================= Test Code =================
36+
static bool equalArrays(const vector<double>& array1, const vector<double>& array2)
37+
{
38+
int length1= array1.size();
39+
int length2 = array2.size();
40+
41+
if(length1 != length2)
42+
return false;
43+
44+
for(int i = 0; i < length1; ++i)
45+
{
46+
if(abs(array1[i] - array2[i]) > 0.0000001)
47+
return false;
48+
}
49+
50+
return true;
51+
}
52+
53+
static void test(char* testName, const vector<double>& array1, vector<double>& array2, const vector<double>& expected){
54+
printf("%s Begins: ", testName);
55+
56+
multiply(array1, array2);
57+
if(equalArrays(array2, expected))
58+
printf("Passed.\n");
59+
else
60+
printf("FAILED.\n");
61+
}
62+
63+
static void test1()
64+
{
65+
double array1[] = {1, 2, 3, 4, 5};
66+
double array2[] = {0, 0, 0, 0, 0};
67+
double expected[] = {120, 60, 40, 30, 24};
68+
69+
test("Test1", vector<double>(array1, array1 + sizeof(array1) / sizeof(double)),
70+
vector<double>(array2, array2 + sizeof(array2) / sizeof(double)),
71+
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
72+
}
73+
74+
static void test2()
75+
{
76+
double array1[] = {1, 2, 0, 4, 5};
77+
double array2[] = {0, 0, 0, 0, 0};
78+
double expected[] = {0, 0, 40, 0, 0};
79+
80+
test("Test2", vector<double>(array1, array1 + sizeof(array1) / sizeof(double)),
81+
vector<double>(array2, array2 + sizeof(array2) / sizeof(double)),
82+
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
83+
}
84+
85+
static void test3()
86+
{
87+
double array1[] = {1, 2, 0, 4, 0};
88+
double array2[] = {0, 0, 0, 0, 0};
89+
double expected[] = {0, 0, 0, 0, 0};
90+
91+
test("Test3", vector<double>(array1, array1 + sizeof(array1) / sizeof(double)),
92+
vector<double>(array2, array2 + sizeof(array2) / sizeof(double)),
93+
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
94+
}
95+
96+
static void test4()
97+
{
98+
double array1[] = {1, -2, 3, -4, 5};
99+
double array2[] = {0, 0, 0, 0, 0};
100+
double expected[] = {120, -60, 40, -30, 24};
101+
102+
test("Test4", vector<double>(array1, array1 + sizeof(array1) / sizeof(double)),
103+
vector<double>(array2, array2 + sizeof(array2) / sizeof(double)),
104+
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
105+
}
106+
107+
static void test5()
108+
{
109+
double array1[] = {1, -2};
110+
double array2[] = {0, 0};
111+
double expected[] = {-2, 1};
112+
113+
test("Test5", vector<double>(array1, array1 + sizeof(array1) / sizeof(double)),
114+
vector<double>(array2, array2 + sizeof(array2) / sizeof(double)),
115+
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
116+
}
117+
118+
void main()
119+
{
120+
test1();
121+
test2();
122+
test3();
123+
test4();
124+
test5();
125+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{DB3BCFF8-B1A7-406B-9D13-D55A4602FBAF}</ProjectGuid>
15+
<Keyword>Win32Proj</Keyword>
16+
<RootNamespace>ArrayConstruction</RootNamespace>
17+
</PropertyGroup>
18+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
19+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
20+
<ConfigurationType>Application</ConfigurationType>
21+
<UseDebugLibraries>true</UseDebugLibraries>
22+
<PlatformToolset>v110</PlatformToolset>
23+
<CharacterSet>Unicode</CharacterSet>
24+
</PropertyGroup>
25+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
26+
<ConfigurationType>Application</ConfigurationType>
27+
<UseDebugLibraries>false</UseDebugLibraries>
28+
<PlatformToolset>v110</PlatformToolset>
29+
<WholeProgramOptimization>true</WholeProgramOptimization>
30+
<CharacterSet>Unicode</CharacterSet>
31+
</PropertyGroup>
32+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
33+
<ImportGroup Label="ExtensionSettings">
34+
</ImportGroup>
35+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
36+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
37+
</ImportGroup>
38+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
39+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
40+
</ImportGroup>
41+
<PropertyGroup Label="UserMacros" />
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
43+
<LinkIncremental>true</LinkIncremental>
44+
<OutDir>D:\temp\CodingInterviewBin\</OutDir>
45+
<IntDir>D:\temp\CodingInterviewBin\$(ProjectName)\</IntDir>
46+
</PropertyGroup>
47+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
48+
<LinkIncremental>false</LinkIncremental>
49+
</PropertyGroup>
50+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
51+
<ClCompile>
52+
<PrecompiledHeader>
53+
</PrecompiledHeader>
54+
<WarningLevel>Level3</WarningLevel>
55+
<Optimization>Disabled</Optimization>
56+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
57+
</ClCompile>
58+
<Link>
59+
<SubSystem>Console</SubSystem>
60+
<GenerateDebugInformation>true</GenerateDebugInformation>
61+
</Link>
62+
<BuildLog>
63+
<Path>D:\temp\CodingInterviewBin\$(MSBuildProjectName).log</Path>
64+
</BuildLog>
65+
</ItemDefinitionGroup>
66+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
67+
<ClCompile>
68+
<WarningLevel>Level3</WarningLevel>
69+
<PrecompiledHeader>
70+
</PrecompiledHeader>
71+
<Optimization>MaxSpeed</Optimization>
72+
<FunctionLevelLinking>true</FunctionLevelLinking>
73+
<IntrinsicFunctions>true</IntrinsicFunctions>
74+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
75+
</ClCompile>
76+
<Link>
77+
<SubSystem>Console</SubSystem>
78+
<GenerateDebugInformation>true</GenerateDebugInformation>
79+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
80+
<OptimizeReferences>true</OptimizeReferences>
81+
</Link>
82+
</ItemDefinitionGroup>
83+
<ItemGroup>
84+
<ClCompile Include="ArrayConstruction.cpp" />
85+
</ItemGroup>
86+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
87+
<ImportGroup Label="ExtensionTargets">
88+
</ImportGroup>
89+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="ArrayConstruction.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>

0 commit comments

Comments
 (0)