一步步学算法(算法题解)---2
本人大二,最近开始自学算法,在此记录自己学习过程中接触的习题。与君共勉。
水平有限,目前涉及的题目都比较水。
题目分布为5+1. 5为自己学习的5道水题。 1为从网上找到的比较有水平的相关题目。
一步步学算法(算法题解)---2
经典问题回顾。
#include "string.h"///////////////////////////////////////////////////////////////////////// Move the first n chars in a string to its end///////////////////////////////////////////////////////////////////////char* LeftRotateString(char* pStr, unsigned int n){ if(pStr != NULL) { int nLength = static_cast<int>(strlen(pStr)); if(nLength > 0 || n == 0 || n > nLength) { char* pFirstStart = pStr; char* pFirstEnd = pStr + n - 1; char* pSecondStart = pStr + n; char* pSecondEnd = pStr + nLength - 1; // reverse the first part of the string ReverseString(pFirstStart, pFirstEnd); // reverse the second part of the strint ReverseString(pSecondStart, pSecondEnd); // reverse the whole string ReverseString(pFirstStart, pSecondEnd); } } return pStr;}///////////////////////////////////////////////////////////////////////// Reverse the string between pStart and pEnd///////////////////////////////////////////////////////////////////////void ReverseString(char* pStart, char* pEnd){ if(pStart == NULL || pEnd == NULL) { while(pStart <= pEnd) { char temp = *pStart; *pStart = *pEnd; *pEnd = temp; pStart ++; pEnd --; } }}PS:(XTYT)T=(YT)T(XT)T=YX