diff --git a/.gitignore b/.gitignore
index d96f1cc..c8967e2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -187,3 +187,4 @@ Alogrithm/UnitTest/Debug/UnitTest.tlog/link.15328.delete.1.tlog
*.obj
*.testlog
*.coverage
+*.ipch
diff --git a/Alogrithm/.vs/Alogrithm/v16/.suo b/Alogrithm/.vs/Alogrithm/v16/.suo
index 3c63209..0f4c655 100644
Binary files a/Alogrithm/.vs/Alogrithm/v16/.suo and b/Alogrithm/.vs/Alogrithm/v16/.suo differ
diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj b/Alogrithm/Alogrithm/Alogrithm.vcxproj
index 92b69b2..7c3f8ef 100644
--- a/Alogrithm/Alogrithm/Alogrithm.vcxproj
+++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj
@@ -155,6 +155,7 @@
+
@@ -180,6 +181,7 @@
+
@@ -204,6 +206,7 @@
+
diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters
index a09d990..5ba3eae 100644
--- a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters
+++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters
@@ -93,6 +93,9 @@
源文件\src
+
+ 源文件\src
+
@@ -161,6 +164,9 @@
头文件\include
+
+ 头文件\include
+
@@ -229,5 +235,8 @@
资源文件\config
+
+ 资源文件\config
+
\ No newline at end of file
diff --git a/Alogrithm/Alogrithm/config/23_RestoreIpAddresses.ini b/Alogrithm/Alogrithm/config/23_RestoreIpAddresses.ini
new file mode 100644
index 0000000..6e53bfd
--- /dev/null
+++ b/Alogrithm/Alogrithm/config/23_RestoreIpAddresses.ini
@@ -0,0 +1,30 @@
+[Test1]
+Input=25525511135
+Output=255.255.11.135,255.255.111.35
+[Test2]
+Input=1111
+Output=1.1.1.1
+[Test3]
+Input=101010101010
+Output=
+[Test4]
+Input=11111111
+Output=1.1.111.111,1.11.11.111,1.11.111.11,1.111.1.111,1.111.11.11,1.111.111.1,11.1.11.111,11.1.111.11,11.11.1.111,11.11.11.11,11.11.111.1,11.111.1.11,11.111.11.1,111.1.1.111,111.1.11.11,111.1.111.1,111.11.1.11,111.11.11.1,111.111.1.1
+[Test5]
+Input=1234567
+Output=1.23.45.67,1.234.5.67,1.234.56.7,12.3.45.67,12.34.5.67,12.34.56.7,123.4.5.67,123.4.56.7,123.45.6.7
+[Test6]
+Input=00000
+Output=
+[Test7]
+Input=123456789101112
+Output=
+[Test8]
+Input=00
+Output=
+[Test9]
+Input=1921681125
+Output=19.216.81.125,192.16.81.125,192.168.1.125,192.168.11.25,192.168.112.5
+[Test10]
+Input=255255255255
+Output=255.255.255.255
diff --git a/Alogrithm/Alogrithm/include/23_RestoreIpAddresses.h b/Alogrithm/Alogrithm/include/23_RestoreIpAddresses.h
new file mode 100644
index 0000000..4467481
--- /dev/null
+++ b/Alogrithm/Alogrithm/include/23_RestoreIpAddresses.h
@@ -0,0 +1,5 @@
+#pragma once
+#include
+#include
+char** RestoreIpAddresses(char* s, int* returnSize);
+void dfs(char* s, int s_len, char** returnStr, int* returnSize, int step, int index, char* temp);
\ No newline at end of file
diff --git a/Alogrithm/Alogrithm/src/23_RestoreIpAddresses.cpp b/Alogrithm/Alogrithm/src/23_RestoreIpAddresses.cpp
new file mode 100644
index 0000000..7acdaff
--- /dev/null
+++ b/Alogrithm/Alogrithm/src/23_RestoreIpAddresses.cpp
@@ -0,0 +1,53 @@
+#include "../include/23_RestoreIpAddresses.h"
+//ĿһֵַֻԭпܵIPַʽ
+// ЧIPַĸÿλ0255֮ɣ֮ '.' ָ
+//˼·֪һϷipַΪĶΣÿһζֱΪһλλλ
+// ԿʹõݹķΪ֧Ϊúҷϵһε
+// һһλ λ λ ʱ ʣַȷϺϷipַҪʹñҵڶ
+// ÿβҶ¼ҵ˵ڼΣѲ鵽ĺϷipʱַĶβʱʱַصַ
+// ȥ¼Ϸipĸ
+void dfs(char* s, int s_len, char** returnStr,int *returnSize, int step, int index, char* temp) {
+ //ijһΪ1λʱʣµַܷɺϷipǣ4-step С 4-step3棺
+ if ((s_len - index - 1 >= 4 - step) && (s_len - index - 1 <= (4 - step) * 3)) {
+ temp[index + step - 1] = s[index]; //ѷϵ1ַŵʱַĵstep
+ temp[index + step ] = '.'; //'.',һνһ
+ dfs(s, s_len, returnStr, returnSize, step + 1, index + 1, temp);//ݹ飬һ
+ }
+ //ijһΪ2λʱʣµַܷɺϷipǣ4-step С 4-step3 ҲΪ0ͷ棺
+ if ((s_len - index - 2 >= 4 - step) && (s_len - index - 2 <= (4 - step) * 3) && s[index] != '0') {
+ temp[index + step - 1] = s[index]; //ѷϵ2ַŵʱַĵstep
+ temp[index + step] = s[index+1];
+ temp[index + step + 1] = '.'; //'.',һνһ
+ dfs(s, s_len, returnStr, returnSize, step + 1, index + 2, temp);//ݹ飬һ
+ }
+ //ijһΪ3λʱʣµַܷɺϷipǣ4-step С 4-step3 Ϊ0ͷ ַɵ
+ //ֲܴ255棺
+ if ((s_len - index - 3 >= 4 - step) && (s_len - index - 3 <= (4 - step) * 3) && s[index] != '0' && (s[index] - '0') * 100 + (s[index + 1] - '0') * 10 + s[index + 2] - '0' <= 255) {
+ temp[index + step - 1] = s[index]; //ѷϵ3ַŵʱַĵstep
+ temp[index + step] = s[index + 1];
+ temp[index + step + 1] = s[index + 2];
+ temp[index + step + 2] = '.'; //'.',һνһ
+ dfs(s, s_len, returnStr, returnSize, step + 1, index + 3, temp);//ݹ飬һ
+ }
+ //step = 5ʱ˵ĶѾ꣬ôtempеַ
+ if (step == 5) {
+ returnStr[*returnSize] = (char*)malloc(sizeof(char) * (s_len + 4));
+ temp[index + step - 2] = '\0'; //һλġ.ijɡ\0
+ strcpy(returnStr[*returnSize], temp);//Ҫص
+ (*returnSize)++; //Ϸַĸ1
+ return;
+ }
+}
+
+char** RestoreIpAddresses(char* s, int* returnSize) {
+ int s_len = strlen(s); //ַsij
+ if (s_len < 4 || s_len>12)//ȲϷֱӷNULL
+ return NULL;
+ char** returnStr = (char**)malloc(sizeof(char*) * 81);//ΪغϷipٿռ
+ char* temp = (char*)malloc(sizeof(char) * (s_len + 4));//һʱ洢õĺϷip
+ *returnSize = 0; //غϷipĸ
+ int step = 1; //stepʾ14step=5ʱ˵ip4ζˣء
+ int index = 0; //indexƶsܹ¼ʵsĵڼַ
+ dfs(s, s_len, returnStr,returnSize, step, index, temp);
+ return returnStr;
+}
\ No newline at end of file
diff --git a/Alogrithm/Alogrithm/src/main.cpp b/Alogrithm/Alogrithm/src/main.cpp
index e9e0524..de59cee 100644
--- a/Alogrithm/Alogrithm/src/main.cpp
+++ b/Alogrithm/Alogrithm/src/main.cpp
@@ -1,9 +1,13 @@
#include
-#include "../include/22_Rotate.h"
+#include "../include/23_RestoreIpAddresses.h"
int main()
{
- int arr[] = { 1,2,3,4,5,6,7};
- Rotate3(arr, 7,3);
+ char str[] = "11112345";
+ int m = 0;
+ char** res;
+ res = RestoreIpAddresses(str, &m);
+ for (int i = 0; i < m; i++)
+ printf("%s\n", res[i]);
}
diff --git a/Alogrithm/UnitTest/UnitTest.cpp b/Alogrithm/UnitTest/UnitTest.cpp
index a193300..6e6d57e 100644
--- a/Alogrithm/UnitTest/UnitTest.cpp
+++ b/Alogrithm/UnitTest/UnitTest.cpp
@@ -24,6 +24,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
#define FileName_20 "../Alogrithm/config/20_MoveZeroes.ini"
#define FileName_21 "../Alogrithm/config/21_Reverse.ini"
#define FileName_22 "../Alogrithm/config/22_Rotate.ini"
+#define FileName_23 "../Alogrithm/config/23_RestoreIpAddresses.ini"
@@ -318,6 +319,7 @@ namespace UnitTest
}
}
};
+
TEST_CLASS(UnitTest_17)
{
TEST_METHOD(TestMethode1)
@@ -435,4 +437,29 @@ namespace UnitTest
}
}
};
+ TEST_CLASS(UnitTest_23)
+ {
+ TEST_METHOD(TestMethode1)
+ {
+ char Section_Name[100][10] = { 0 };
+ int Section_Count = CalcCount(100, Section_Name, FileName_23);
+ CString Input, Output;
+ char Str_char[20];
+ char** return_char ;
+ int return_len = 0;
+ char output_char[100][50];
+ int output_len = 0;
+ for (int i = 0; i < Section_Count; i++) {
+ GetPrivateProfileString(Section_Name[i], "Input", " ", Input.GetBuffer(20), 20, FileName_23);
+ GetPrivateProfileString(Section_Name[i], "Output", " ", Output.GetBuffer(1024), 1024, FileName_23);
+ output_len = str_device2(Output, output_char);
+ strcpy(Str_char, Input);
+ return_char = RestoreIpAddresses(Str_char, &return_len);
+ Assert::AreEqual(return_len, output_len); //жϸǷ
+ for (int j = 0; j < return_len; j++) { //ѭȽ
+ Assert::AreEqual(return_char[j], output_char[j]);
+ }
+ }
+ }
+ };
}
diff --git a/Alogrithm/UnitTest/UnitTest.vcxproj b/Alogrithm/UnitTest/UnitTest.vcxproj
index d9f9aff..3468908 100644
--- a/Alogrithm/UnitTest/UnitTest.vcxproj
+++ b/Alogrithm/UnitTest/UnitTest.vcxproj
@@ -103,7 +103,7 @@
Windows
$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)
DebugFull
- ../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;../Alogrithm/Debug/5_MinDepth.obj;../Alogrithm/Debug/6_ContainsDuplicate.obj;../Alogrithm/Debug/7_MaxDepth.obj;../Alogrithm/Debug/8_HammingWeight.obj;../Alogrithm/Debug/9_AddBinary.obj;../Alogrithm/Debug/10_BinaryTreePaths.obj;../Alogrithm/Debug/11_CanWinNim.obj;../Alogrithm/Debug/12_IsValid.obj;../Alogrithm/Debug/13_MyAtoi.obj;../Alogrithm/Debug/14_SingleNumber.obj;../Alogrithm/Debug/15_WordPattern.obj;../Alogrithm/Debug/16_ReverseBits.obj;../Alogrithm/Debug/17_WordBreak.obj;../Alogrithm/Debug/18_PlusOne.obj;../Alogrithm/Debug/19_MySqrt.obj;../Alogrithm/Debug/20_MoveZeroes.obj;../Alogrithm/Debug/21_Reverse.obj;../Alogrithm/Debug/22_Rotate.obj;%(AdditionalDependencies)
+ ../Alogrithm/Debug/*.obj;%(AdditionalDependencies)
diff --git a/Alogrithm/UnitTest/pch.cpp b/Alogrithm/UnitTest/pch.cpp
index 119f44f..ac075ff 100644
--- a/Alogrithm/UnitTest/pch.cpp
+++ b/Alogrithm/UnitTest/pch.cpp
@@ -92,14 +92,14 @@ int str_device3(CString str, char* *return_str)
{
int value_count = 0;
char* token; //存放被切割后的第一个子串
- char Section_value[500] = { 0 };//存放nums转换成string类型的结果
- memset(Section_value, 0, sizeof(char) * 500);
+ char Section_value[1024] = { 0 };//存放nums转换成string类型的结果
+ memset(Section_value, 0, sizeof(char) * 1024);
strcpy(Section_value, str);//将CString类型的字符串转换成char类型,方便后面切割字符串
//获得切割到的第一个字符串
token = strtok(Section_value, ",");
/* 继续获取其他的子字符串 */
while (token != NULL) {
- return_str[value_count] = (char*)malloc(sizeof(char) * 100);
+ return_str[value_count] = (char*)malloc(sizeof(char) * 1024);
strcpy(return_str[value_count], token);
token = strtok(NULL, ",");
value_count++; //记录存了多少个元素
diff --git a/Alogrithm/UnitTest/pch.h b/Alogrithm/UnitTest/pch.h
index 712d5ad..40fdd28 100644
--- a/Alogrithm/UnitTest/pch.h
+++ b/Alogrithm/UnitTest/pch.h
@@ -35,6 +35,7 @@
#include"../Alogrithm/include/20_MoveZeroes.h"
#include"../Alogrithm/include/21_Reverse.h"
#include"../Alogrithm/include/22_Rotate.h"
+#include"../Alogrithm/include/23_RestoreIpAddresses.h"