Bläddra i källkod

Merge branch '23'

master
linyongji 4 år sedan
förälder
incheckning
574b1d446c
12 ändrade filer med 140 tillägg och 7 borttagningar
  1. +1
    -0
      .gitignore
  2. Binär
      Alogrithm/.vs/Alogrithm/v16/.suo
  3. +3
    -0
      Alogrithm/Alogrithm/Alogrithm.vcxproj
  4. +9
    -0
      Alogrithm/Alogrithm/Alogrithm.vcxproj.filters
  5. +30
    -0
      Alogrithm/Alogrithm/config/23_RestoreIpAddresses.ini
  6. +5
    -0
      Alogrithm/Alogrithm/include/23_RestoreIpAddresses.h
  7. +53
    -0
      Alogrithm/Alogrithm/src/23_RestoreIpAddresses.cpp
  8. +7
    -3
      Alogrithm/Alogrithm/src/main.cpp
  9. +27
    -0
      Alogrithm/UnitTest/UnitTest.cpp
  10. +1
    -1
      Alogrithm/UnitTest/UnitTest.vcxproj
  11. +3
    -3
      Alogrithm/UnitTest/pch.cpp
  12. +1
    -0
      Alogrithm/UnitTest/pch.h

+ 1
- 0
.gitignore Visa fil

@@ -187,3 +187,4 @@ Alogrithm/UnitTest/Debug/UnitTest.tlog/link.15328.delete.1.tlog
*.obj
*.testlog
*.coverage
*.ipch

Binär
Alogrithm/.vs/Alogrithm/v16/.suo Visa fil


+ 3
- 0
Alogrithm/Alogrithm/Alogrithm.vcxproj Visa fil

@@ -155,6 +155,7 @@
<ClCompile Include="src\20_MoveZeroes.cpp" />
<ClCompile Include="src\21_Reverse.cpp" />
<ClCompile Include="src\22_Rotate.cpp" />
<ClCompile Include="src\23_RestoreIpAddresses.cpp" />
<ClCompile Include="src\2_ExcelSheetColumnTitle.cpp" />
<ClCompile Include="src\3_bool IsUgly.cpp" />
<ClCompile Include="src\4_IsPalindrome.cpp" />
@@ -180,6 +181,7 @@
<ClInclude Include="include\20_MoveZeroes.h" />
<ClInclude Include="include\21_Reverse.h" />
<ClInclude Include="include\22_Rotate.h" />
<ClInclude Include="include\23_RestoreIpAddresses.h" />
<ClInclude Include="include\2_ExcelSheetColumnTitle.h" />
<ClInclude Include="include\3_bool IsUgly.h" />
<ClInclude Include="include\4_IsPalindrome.h" />
@@ -204,6 +206,7 @@
<None Include="config\20_MoveZeroes.ini" />
<None Include="config\21_Reverse.ini" />
<None Include="config\22_Rotate.ini" />
<None Include="config\23_RestoreIpAddresses.ini" />
<None Include="config\2_ExcelSheetColumnTiTle.ini" />
<None Include="config\3_bool IsUgly.ini" />
<None Include="config\4_IsPalindrome.ini" />


+ 9
- 0
Alogrithm/Alogrithm/Alogrithm.vcxproj.filters Visa fil

@@ -93,6 +93,9 @@
<ClCompile Include="src\17_WordBreak.cpp">
<Filter>源文件\src</Filter>
</ClCompile>
<ClCompile Include="src\23_RestoreIpAddresses.cpp">
<Filter>源文件\src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\2_ExcelSheetColumnTitle.h">
@@ -161,6 +164,9 @@
<ClInclude Include="include\17_WordBreak.h">
<Filter>头文件\include</Filter>
</ClInclude>
<ClInclude Include="include\23_RestoreIpAddresses.h">
<Filter>头文件\include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="config\3_bool IsUgly.ini">
@@ -229,5 +235,8 @@
<None Include="config\17_WordBreak.ini">
<Filter>资源文件\config</Filter>
</None>
<None Include="config\23_RestoreIpAddresses.ini">
<Filter>资源文件\config</Filter>
</None>
</ItemGroup>
</Project>

+ 30
- 0
Alogrithm/Alogrithm/config/23_RestoreIpAddresses.ini Visa fil

@@ -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

+ 5
- 0
Alogrithm/Alogrithm/include/23_RestoreIpAddresses.h Visa fil

@@ -0,0 +1,5 @@
#pragma once
#include <string.h>
#include <stdlib.h>
char** RestoreIpAddresses(char* s, int* returnSize);
void dfs(char* s, int s_len, char** returnStr, int* returnSize, int step, int index, char* temp);

+ 53
- 0
Alogrithm/Alogrithm/src/23_RestoreIpAddresses.cpp Visa fil

@@ -0,0 +1,53 @@
#include "../include/23_RestoreIpAddresses.h"
//题目:给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式。
// 有效的IP地址正好由四个整数(每个整数位于0到255之间组成),整数之间用 '.' 分隔。
//思路:由题知,一个合法ip地址分为四段,每一段都有三种情况,分别为:一位数、两位数、三位数,
// 这道题可以考虑使用递归的方法,把整个问题拆分为三个分支,步骤为:调用函数查找符合第一段的情况(如果
// 第一段是一位数 或两位数 或三位数 时 所剩字符长度符合合法ip地址要求)则继续使用本函数查找第二段
// 每次查找都会记录查找到了第几段,并把查到的合法ip放在临时字符串,第四段查完时把临时字符串拷贝到返回的字符串
// 数组中去,并记录合法ip的个数
void dfs(char* s, int s_len, char** returnStr,int *returnSize, int step, int index, char* temp) {
//当某一段为1位数时,考虑剩下的字符能否组成合法ip(条件是:大于4-step 且小于 4-step乘3),符合条件走下面:
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);//继续递归,考虑下一段
}
//当某一段为2位数时,考虑剩下的字符能否组成合法ip(条件是:大于4-step 且小于 4-step乘3 且不为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);//继续递归,考虑下一段
}
//当某一段为3位数时,考虑剩下的字符能否组成合法ip(条件是:大于4-step 且 小于 4-step乘3 且 不为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'; //把最后一位的‘.’改成‘\0’
strcpy(returnStr[*returnSize], temp);//拷贝到需要返回的数组中
(*returnSize)++; //合法字符串的个数加1
return;
}
}

char** RestoreIpAddresses(char* s, int* returnSize) {
int s_len = strlen(s); //字符串s的长度
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表示段数,从1到4,当step=5时说明ip的4段都考虑完了,返回。
int index = 0; //index移动访问s,并能够记录访问到了s的第几个字符
dfs(s, s_len, returnStr,returnSize, step, index, temp);
return returnStr;
}

+ 7
- 3
Alogrithm/Alogrithm/src/main.cpp Visa fil

@@ -1,9 +1,13 @@
#include<stdio.h>
#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]);
}

+ 27
- 0
Alogrithm/UnitTest/UnitTest.cpp Visa fil

@@ -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]);
}
}
}
};
}

+ 1
- 1
Alogrithm/UnitTest/UnitTest.vcxproj Visa fil

@@ -103,7 +103,7 @@
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>DebugFull</GenerateDebugInformation>
<AdditionalDependencies>../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)</AdditionalDependencies>
<AdditionalDependencies>../Alogrithm/Debug/*.obj;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">


+ 3
- 3
Alogrithm/UnitTest/pch.cpp Visa fil

@@ -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++; //记录存了多少个元素


+ 1
- 0
Alogrithm/UnitTest/pch.h Visa fil

@@ -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"





Laddar…
Avbryt
Spara