problemscpp
A collection of my answers to algorithm problems in c++.
lintcode_test.cpp
浏览该文件的文档.
1#include "lintcode.h"
2#include "gtest/gtest.h"
3#include <vector>
4
5using namespace std;
6
7namespace lintcode {
8 namespace license_key_formatting {
9 TEST(license_key_formatting, case1) {
10 auto s = string("5F3Z-2e-9-w");
11 ASSERT_EQ("5F3Z-2E9W", Solution::licenseKeyFormatting(s, 4));
12 }
13
14 TEST(license_key_formatting, case2) {
15 auto s = string("2-5g-3-J");
16 ASSERT_EQ("2-5G-3J", Solution::licenseKeyFormatting(s, 2));
17 }
18 }// namespace license_key_formatting
19
20 namespace distribute_candies {
21 TEST(distribute_candies, case1) {
22 int input[] = {
23 1,
24 1,
25 2,
26 2,
27 3,
28 3,
29 };
30 auto vec = vector(begin(input), end(input));
31 ASSERT_EQ(3, Solution::distributeCandies(vec));
32 }
33
34 TEST(distribute_candies, case2) {
35 int input[] = {1, 1, 2, 2, 3, 3, 4, 5, 6, 6, 7, 8};
36 auto vec = vector(begin(input), end(input));
37 ASSERT_EQ(6, Solution::distributeCandies(vec));
38 }
39 }// namespace distribute_candies
40
41 namespace remove_extra {
42 TEST(remove_extra, case1) {
43 string input = "The sky is blue";
44 ASSERT_EQ("The sky is blue", Solution::removeExtra(input));
45 }
46
47 TEST(remove_extra, case2) {
48 string input = " low ercase ";
49 ASSERT_EQ("low ercase", Solution::removeExtra(input));
50 }
51 }// namespace remove_extra
52
53 namespace character_deletion {
54 TEST(character_deletion, case1) {
55 auto str = string("They are students");
56 auto sub = string("aeiou");
57 ASSERT_EQ("Thy r stdnts", Solution::CharacterDeletion(str, sub));
58 }
59 }// namespace character_deletion
60
61 namespace judge_circle {
62 TEST(judge_circle, case1) {
63 auto str = string("UD");
64 ASSERT_TRUE(Solution::judgeCircle(str));
65 }
66
67 TEST(judge_circle, case2) {
68 auto str = string("LL");
69 ASSERT_FALSE(Solution::judgeCircle(str));
70 }
71 }// namespace judge_circle
72
73 namespace convert {
74 TEST(convert, case1) {
75 ASSERT_EQ("1C", Solution::convert(3));
76 }
77
78 TEST(convert, case2) {
79 ASSERT_EQ("1AB", Solution::convert(28));
80 }
81
82 TEST(convert, case3) {
83 ASSERT_EQ("1424501424501425MN", Solution::convert(1000000000000000000));
84 }
85
86 TEST(convert, case4) {
87 ASSERT_EQ("1ZZ", Solution::convert(702));
88 }
89 }// namespace convert
90
91 namespace min_path_sum {
92 TEST(min_path_sum, case1) {
93 int input[3][3] = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
94 auto vec = vector<vector<int>>();
95 vec.resize(3);
96 for(int i = 0; i < 3; i++) {
97 const auto to_add = vector(begin(input[i]), end(input[i]));
98 vec[i] = to_add;
99 }
100 auto sol = Solution();
101 ASSERT_EQ(7, sol.minPathSum(vec));
102 }
103
104 TEST(min_path_sum, case2) {
105 int input[1][3] = {{1, 3, 2}};
106 auto vec = vector<vector<int>>();
107 vec.resize(1);
108 for(int i = 0; i < 1; i++) {
109 const auto to_add = vector(begin(input[i]), end(input[i]));
110 vec[i] = to_add;
111 }
112 auto sol = Solution();
113 ASSERT_EQ(6, sol.minPathSum(vec));
114 }
115
116 TEST(min_path_sum, case3) {
117 int input[8][8] = {{1, 4, 8, 6, 2, 2, 1, 7}, {4, 7, 3, 1, 4, 5, 5, 1}, {8, 8, 2, 1, 1, 8, 0, 1}, {8, 9, 2, 9, 8, 0, 8, 9}, {5, 7, 5, 7, 1, 8, 5, 5}, {7, 0, 9, 4, 5, 6, 5, 6}, {4, 9, 9, 7, 9, 1, 9, 0}};
118 auto vec = vector<vector<int>>();
119 vec.resize(8);
120 for(int i = 0; i < 8; i++) {
121 const auto to_add = vector(begin(input[i]), end(input[i]));
122 vec[i] = to_add;
123 }
124 auto sol = Solution();
125 ASSERT_EQ(37, sol.minPathSum(vec));
126 }
127 }// namespace min_path_sum
128
129 namespace digit_counts {
130 TEST(digit_counts, case1) {
131 ASSERT_EQ(1, Solution::digitCounts(1, 1));
132 }
133
134 TEST(digit_counts, case2) {
135 ASSERT_EQ(5, Solution::digitCounts(1, 12));
136 }
137
138 TEST(digit_counts, case3) {
139 ASSERT_EQ(2, Solution::digitCounts(0, 10));
140 }
141 }// namespace digit_counts
142}// namespace lintcode
TEST(license_key_formatting, case1)
TEST(distribute_candies, case1)
TEST(remove_extra, case1)
TEST(character_deletion, case1)
TEST(judge_circle, case1)
TEST(convert, case1)
TEST(min_path_sum, case1)
TEST(digit_counts, case1)
int vec[100010]
Definition: pat.cpp:5095
static string licenseKeyFormatting(string &, int)
Definition: lintcode.cpp:18
static int distributeCandies(vector< int > &candies)
Definition: lintcode.cpp:56
static string removeExtra(string &s)
Definition: lintcode.cpp:86
static string CharacterDeletion(string &str, string &sub)
Definition: lintcode.cpp:107
static bool judgeCircle(string &moves)
Definition: lintcode.cpp:123
static string convert(long long index)
Definition: lintcode.cpp:213
static int digitCounts(int k, int n)
Definition: lintcode.cpp:279