Skip to content

Commit 6c05c5b

Browse files
committed
code_offer
1 parent 1c655db commit 6c05c5b

File tree

92 files changed

+12910
-36
lines changed

Some content is hidden

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

92 files changed

+12910
-36
lines changed

Search/17.letter-combinations-of-a-phone-number.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,18 @@
3232
* in any order you want.
3333
*
3434
*/
35-
class Solution
36-
{
37-
public:
35+
class Solution {
36+
public:
3837
vector<string> letterCombinations(string digits)
3938
{
4039
if (digits.empty())
4140
return {};
42-
vector<string> an{""};
43-
string dict[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
44-
for (int i = 0; i < digits.size(); i++)
45-
{
41+
vector<string> an{ "" };
42+
string dict[] = { "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
43+
for (int i = 0; i < digits.size(); i++) {
4644
vector<string> t;
4745
string str = dict[digits[i] - '2'];
48-
for (int j = 0; j < str.size(); j++)
49-
{
46+
for (int j = 0; j < str.size(); j++) {
5047
for (string s : an)
5148
t.push_back(s + str[j]);
5249
}

Search/[O]39.combination-sum.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,73 +14,69 @@
1414
* Given a set of candidate numbers (candidates) (without duplicates) and a
1515
* target number (target), find all unique combinations in candidates where the
1616
* candidate numbers sums to target.
17-
*
17+
*
1818
* The same repeated number may be chosen from candidates unlimited number of
1919
* times.
20-
*
20+
*
2121
* Note:
22-
*
23-
*
22+
*
23+
*
2424
* All numbers (including target) will be positive integers.
2525
* The solution set must not contain duplicate combinations.
26-
*
27-
*
26+
*
27+
*
2828
* Example 1:
29-
*
30-
*
29+
*
30+
*
3131
* Input: candidates = [2,3,6,7], target = 7,
3232
* A solution set is:
3333
* [
3434
* ⁠ [7],
3535
* ⁠ [2,2,3]
3636
* ]
37-
*
38-
*
37+
*
38+
*
3939
* Example 2:
40-
*
41-
*
40+
*
41+
*
4242
* Input: candidates = [2,3,5], target = 8,
4343
* A solution set is:
4444
* [
4545
* [2,2,2,2],
4646
* [2,3,3],
4747
* [3,5]
4848
* ]
49-
*
50-
*
49+
*
50+
*
5151
*/
5252

5353
/*
5454
Runtime: 92 ms
5555
Memory Usage: 21.8 MB
5656
*/
57-
class Solution
58-
{
59-
public:
60-
void search(vector<int> &candidates, int pos, int target, vector<int> temp, vector<vector<int>> &an)
57+
class Solution {
58+
public:
59+
void search(vector<int>& candidates, int pos, int target, vector<int> temp,
60+
vector<vector<int>>& an)
6161
{
62-
if (target == 0)
63-
{
62+
if (target == 0) {
6463
an.push_back(temp);
6564
return;
6665
}
6766
if (pos >= candidates.size())
6867
return;
6968
int c = target / candidates[pos];
70-
for (int i = 0; i <= c; i++)
71-
{
72-
for (int j = 0; j < i; j++)
73-
{
69+
for (int i = 0; i <= c; i++) {
70+
for (int j = 0; j < i; j++) {
7471
temp.push_back(candidates[pos]);
7572
}
7673
search(candidates, pos + 1, target - i * candidates[pos], temp, an);
77-
for (int j = 0; j < i; j++)
78-
{
74+
for (int j = 0; j < i; j++) {
7975
temp.pop_back();
8076
}
8177
}
8278
}
83-
vector<vector<int>> combinationSum(vector<int> &candidates, int target)
79+
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
8480
{
8581
sort(candidates.begin(), candidates.end());
8682
vector<int> temp;

code_offer/10_Fibonacci.cpp

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*******************************************************************
2+
Copyright(c) 2016, Harry He
3+
All rights reserved.
4+
5+
Distributed under the BSD license.
6+
(See accompanying file LICENSE.txt at
7+
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
8+
*******************************************************************/
9+
10+
//==================================================================
11+
// 《剑指Offer——名企面试官精讲典型编程题》代码
12+
// 作者:何海涛
13+
//==================================================================
14+
15+
// 面试题10:斐波那契数列
16+
// 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项。
17+
18+
#include <cstdio>
19+
20+
// ====================方法1:递归====================
21+
long long Fibonacci_Solution1(unsigned int n)
22+
{
23+
if (n <= 0)
24+
return 0;
25+
26+
if (n == 1)
27+
return 1;
28+
29+
return Fibonacci_Solution1(n - 1) + Fibonacci_Solution1(n - 2);
30+
}
31+
32+
// ====================方法2:循环====================
33+
long long Fibonacci_Solution2(unsigned n)
34+
{
35+
int result[2] = { 0, 1 };
36+
if (n < 2)
37+
return result[n];
38+
39+
long long fibNMinusOne = 1;
40+
long long fibNMinusTwo = 0;
41+
long long fibN = 0;
42+
for (unsigned int i = 2; i <= n; ++i) {
43+
fibN = fibNMinusOne + fibNMinusTwo;
44+
45+
fibNMinusTwo = fibNMinusOne;
46+
fibNMinusOne = fibN;
47+
}
48+
49+
return fibN;
50+
}
51+
52+
// ====================方法3:基于矩阵乘法====================
53+
#include <cassert>
54+
55+
struct Matrix2By2 {
56+
Matrix2By2(
57+
long long m00 = 0,
58+
long long m01 = 0,
59+
long long m10 = 0,
60+
long long m11 = 0)
61+
: m_00(m00)
62+
, m_01(m01)
63+
, m_10(m10)
64+
, m_11(m11)
65+
{
66+
}
67+
68+
long long m_00;
69+
long long m_01;
70+
long long m_10;
71+
long long m_11;
72+
};
73+
74+
Matrix2By2 MatrixMultiply(
75+
const Matrix2By2& matrix1,
76+
const Matrix2By2& matrix2)
77+
{
78+
return Matrix2By2(
79+
matrix1.m_00 * matrix2.m_00 + matrix1.m_01 * matrix2.m_10,
80+
matrix1.m_00 * matrix2.m_01 + matrix1.m_01 * matrix2.m_11,
81+
matrix1.m_10 * matrix2.m_00 + matrix1.m_11 * matrix2.m_10,
82+
matrix1.m_10 * matrix2.m_01 + matrix1.m_11 * matrix2.m_11);
83+
}
84+
85+
Matrix2By2 MatrixPower(unsigned int n)
86+
{
87+
assert(n > 0);
88+
89+
Matrix2By2 matrix;
90+
if (n == 1) {
91+
matrix = Matrix2By2(1, 1, 1, 0);
92+
} else if (n % 2 == 0) {
93+
matrix = MatrixPower(n / 2);
94+
matrix = MatrixMultiply(matrix, matrix);
95+
} else if (n % 2 == 1) {
96+
matrix = MatrixPower((n - 1) / 2);
97+
matrix = MatrixMultiply(matrix, matrix);
98+
matrix = MatrixMultiply(matrix, Matrix2By2(1, 1, 1, 0));
99+
}
100+
101+
return matrix;
102+
}
103+
104+
long long Fibonacci_Solution3(unsigned int n)
105+
{
106+
int result[2] = { 0, 1 };
107+
if (n < 2)
108+
return result[n];
109+
110+
Matrix2By2 PowerNMinus2 = MatrixPower(n - 1);
111+
return PowerNMinus2.m_00;
112+
}
113+
114+
// ====================测试代码====================
115+
void Test(int n, int expected)
116+
{
117+
if (Fibonacci_Solution1(n) == expected)
118+
printf("Test for %d in solution1 passed.\n", n);
119+
else
120+
printf("Test for %d in solution1 failed.\n", n);
121+
122+
if (Fibonacci_Solution2(n) == expected)
123+
printf("Test for %d in solution2 passed.\n", n);
124+
else
125+
printf("Test for %d in solution2 failed.\n", n);
126+
127+
if (Fibonacci_Solution3(n) == expected)
128+
printf("Test for %d in solution3 passed.\n", n);
129+
else
130+
printf("Test for %d in solution3 failed.\n", n);
131+
}
132+
133+
int main(int argc, char* argv[])
134+
{
135+
Test(0, 0);
136+
Test(1, 1);
137+
Test(2, 1);
138+
Test(3, 2);
139+
Test(4, 3);
140+
Test(5, 5);
141+
Test(6, 8);
142+
Test(7, 13);
143+
Test(8, 21);
144+
Test(9, 34);
145+
Test(10, 55);
146+
147+
Test(40, 102334155);
148+
149+
return 0;
150+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*******************************************************************
2+
Copyright(c) 2016, Harry He
3+
All rights reserved.
4+
5+
Distributed under the BSD license.
6+
(See accompanying file LICENSE.txt at
7+
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
8+
*******************************************************************/
9+
10+
//==================================================================
11+
// 《剑指Offer——名企面试官精讲典型编程题》代码
12+
// 作者:何海涛
13+
//==================================================================
14+
15+
// 面试题11:旋转数组的最小数字
16+
// 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
17+
// 输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如数组
18+
// {3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为1。
19+
20+
#include <cstdio>
21+
#include <exception>
22+
23+
int MinInOrder(int* numbers, int index1, int index2);
24+
25+
int Min(int* numbers, int length)
26+
{
27+
if (numbers == nullptr || length <= 0)
28+
throw new std::exception("Invalid parameters");
29+
30+
int index1 = 0;
31+
int index2 = length - 1;
32+
int indexMid = index1;
33+
while (numbers[index1] >= numbers[index2]) {
34+
// 如果index1和index2指向相邻的两个数,
35+
// 则index1指向第一个递增子数组的最后一个数字,
36+
// index2指向第二个子数组的第一个数字,也就是数组中的最小数字
37+
if (index2 - index1 == 1) {
38+
indexMid = index2;
39+
break;
40+
}
41+
42+
// 如果下标为index1、index2和indexMid指向的三个数字相等,
43+
// 则只能顺序查找
44+
indexMid = (index1 + index2) / 2;
45+
if (numbers[index1] == numbers[index2] && numbers[indexMid] == numbers[index1])
46+
return MinInOrder(numbers, index1, index2);
47+
48+
// 缩小查找范围
49+
if (numbers[indexMid] >= numbers[index1])
50+
index1 = indexMid;
51+
else if (numbers[indexMid] <= numbers[index2])
52+
index2 = indexMid;
53+
}
54+
55+
return numbers[indexMid];
56+
}
57+
58+
int MinInOrder(int* numbers, int index1, int index2)
59+
{
60+
int result = numbers[index1];
61+
for (int i = index1 + 1; i <= index2; ++i) {
62+
if (result > numbers[i])
63+
result = numbers[i];
64+
}
65+
66+
return result;
67+
}
68+
69+
// ====================测试代码====================
70+
void Test(int* numbers, int length, int expected)
71+
{
72+
int result = 0;
73+
try {
74+
result = Min(numbers, length);
75+
76+
for (int i = 0; i < length; ++i)
77+
printf("%d ", numbers[i]);
78+
79+
if (result == expected)
80+
printf("\tpassed\n");
81+
else
82+
printf("\tfailed\n");
83+
} catch (...) {
84+
if (numbers == nullptr)
85+
printf("Test passed.\n");
86+
else
87+
printf("Test failed.\n");
88+
}
89+
}
90+
91+
int main(int argc, char* argv[])
92+
{
93+
// 典型输入,单调升序的数组的一个旋转
94+
int array1[] = { 3, 4, 5, 1, 2 };
95+
Test(array1, sizeof(array1) / sizeof(int), 1);
96+
97+
// 有重复数字,并且重复的数字刚好的最小的数字
98+
int array2[] = { 3, 4, 5, 1, 1, 2 };
99+
Test(array2, sizeof(array2) / sizeof(int), 1);
100+
101+
// 有重复数字,但重复的数字不是第一个数字和最后一个数字
102+
int array3[] = { 3, 4, 5, 1, 2, 2 };
103+
Test(array3, sizeof(array3) / sizeof(int), 1);
104+
105+
// 有重复的数字,并且重复的数字刚好是第一个数字和最后一个数字
106+
int array4[] = { 1, 0, 1, 1, 1 };
107+
Test(array4, sizeof(array4) / sizeof(int), 0);
108+
109+
// 单调升序数组,旋转0个元素,也就是单调升序数组本身
110+
int array5[] = { 1, 2, 3, 4, 5 };
111+
Test(array5, sizeof(array5) / sizeof(int), 1);
112+
113+
// 数组中只有一个数字
114+
int array6[] = { 2 };
115+
Test(array6, sizeof(array6) / sizeof(int), 2);
116+
117+
// 输入nullptr
118+
Test(nullptr, 0, 0);
119+
120+
return 0;
121+
}

0 commit comments

Comments
 (0)