#include <string>
#include <iostream>
#include <stack>
using namespace std;
/*將字符串中的大寫字母轉換成小寫字母*/
void RevertBig2Small(string &str){
if(str.empty())
return;
for(int i = 0 ;i<str.length();i++){
if((str[i]>'A')&& (str[i] < 'Z')){
str[i] +=32;/*小寫字母(a-z)的ASCII碼比大些字母(A-Z)的ASCII碼大32*/
}
}
}
/*字符串反轉輸出,不增加內存的基礎上實現,原地反轉
輸入:["h","e","l","l","o"]
輸出:["o","l","l","e","h"]
解題思路:將字符串二分,然後從兩頭往中間進行交換
*/
void RevertChar(string &str){
if(str.empty())
return;
char tmp;
for(int i = 0; i < str.length()/2;i++){
tmp=str[i];
str[i]=str[str.length()-1-i];
str[str.length()-1-i]=tmp;
}
}
/*給定一個字符串,逐個翻轉字符串中的每個單詞。
示例:
輸入: “the sky is blue”,
輸出: “blue is sky the”.
使用 O(1) 空間複雜度的原地解法
思路:首先將原字符串逆轉,然後挨個逆轉連續的子串,特別要注意,多個空格在一起的情況
*/
void WordRevert(string &str){
if(str.empty())
return;
RevertChar(str);
//str.clear();
string tempStr = "";
for(int i=0;i<str.length() ;i++){
if(str[i]==' ')
continue;
string word;
/*確定單詞*/
while((str[i] != ' ') && (i<str.length())){
word+=str[i++];
}
word +=' ';/*單詞間增加空格*/
RevertChar(word);/*反轉單個單詞*/
tempStr+=word;
}
str=tempStr.substr(0);/*重新從臨時str中獲取整個子串內容*/
}
/*實現 strStr() 函數:strstr(str1,str2) 函數用於判斷字符串str2是否是str1的子串。如果是,則該函數返回str2在str1中首次出現的地址;否則,返回NULL
思路就是:就是將子串從區間的第一個字符開始比較,如果有就返回,否則返回NULL*/
char* StrStr(char *str,char* subStr){
for(int i = 0; i <= strlen(str);i++,str++){
char *p=str;
for(char *q=subStr;;p++,q++){
if(*q == '\0')
return str;
if(*p != *q)
break;
}
}
return NULL;
}
/*逆序構造單鏈表
例如:輸入數據:1 2 3 4 5 6,構造單鏈表:6->5->4->3->2->1。
輸入 -1 鏈表結束
思路:pre指針,cur指針,cur賦當前值,
**/
class ConstructPostList{
public:
struct ListNode{
string data;
struct ListNode *next;
};
ConstructPostList(){
head.next = NULL;
}
void CreatePostList(string &str){
struct ListNode *pre = NULL;/*第一次的話表示做後一個節點,第一次設置爲空*/
for(int i =0; i<str.length();i++)
{
struct ListNode *node=new(ListNode);
node->data = str[i];
node->next = pre;
pre = node;
}
head.next=pre;
}
void CreateNormalList(string &str){
ListNode *next=&head;
for(int i = 0; i < str.length();i++){
ListNode *cur = new(ListNode);
cur->data=str[i];
next->next=cur;
next = cur;
}
}
void ShowList(ListNode *h){
struct ListNode *node = h->next;
while(node){
cout<<node->data<<" ";
node=node->next;
}
cout<<""<<endl;
}
void DestroyList(){
struct ListNode *node = head.next;
int i = 0;
while(node){
ListNode *cur=node;
node=cur->next;
delete cur;
i++;
}
cout<<"Destroy list,node num="<<i<<endl;
}
/*鏈表反轉*/
ListNode* ListRevert(ListNode *head){
if(NULL == head|| head->next == NULL){
return head;
}
ListNode *pre = NULL;
ListNode *next = NULL;
while(head){
next = head->next;
head->next = pre;
head = next;
}
return pre;
}
ListNode* reverseByRecursion(ListNode *head)
{
//第一個條件是判斷異常,第二個條件是結束判斷
if(head == NULL || head->next == NULL)
return head;
ListNode *newHead = reverseByRecursion(head->next);
head->next->next = head;
head->next = NULL;
return newHead; //返回新鏈表的頭指針
}
ListNode* GetList(){
return &head;
}
private:
ListNode head;
};
struct Node{
int data;
struct Node *next;
Node(){
data=0;
next=NULL;
}
};
class List{
public:
List(){
head = new(Node);
head->data = 0;
head->next= NULL;
}
Node* CreateList(int num){
Node *cur=head;
for(int i = 0; i<num;i++){
Node* node=new(Node);
node->data=i;
node->next = NULL;
cur->next = node;
cur = node;
}
return head;
}
Node* AddTail(int value){
if(!head){
return head;
}
Node *cur=head;
Node *next = cur->next;
while(next){
cur = next;
next = next->next;
}
Node *node = new(Node);
node->data = value;
node->next=NULL;
cur->next = node;
return head;
}
Node *RevertList(Node* head){
if(!head->next){
return head;
}
Node *pre,*cur,*next;
pre=head;
head->next=NULL;
cur=head->next;
while(cur){
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
head=pre;
return head;
}
bool IsLoopList(Node *head){
Node *fast=head->next->next;
Node *slow=head->next;
while(fast && slow){
fast=fast->next->next;
slow=slow->next;
if(fast == slow){
return true;
}
}
return false;
}
void Show(Node *h){
Node *cur = h->next;
while(cur){
cout<<cur->data<<" ";
cur= cur->next;
}
cout<<endl;
}
private:
Node *head;
};
int main()
{
List l;
Node* head=l.CreateList(10);
l.Show(head);
l.Show(l.RevertList(head));
return 0;
}