提交 a5f10cec 编写于 作者: cosmicing's avatar cosmicing

上传新文件

上级 c151bf5c
#include <iostream>
#include <string>
#include <vector>
#include<algorithm>
#include<sstream>
#include <cstring>
using namespace std;
//Definition for singly-linked list.
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
ListNode *detectCycle(ListNode *head)
{
if (head == nullptr) return nullptr;
auto fast = head;
auto slow = head;
auto entry = head;
while (fast->next and fast->next->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
{
while (slow != entry)
{
slow = slow->next;
entry = entry->next;
}
return entry;
}
}
return nullptr;
}
ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
ListNode *p = list1;
ListNode *q = list2;
ListNode *resultList;
ListNode *insertBegin = nullptr, *insertEnd = nullptr;
while(p != nullptr) {
if(p->next && p->next->val == a) {
insertBegin = p;
}
if(p->val == b && p->next) {
insertEnd = p->next;
}
cout << p->val << endl;
p = p->next;
}
if(insertBegin == nullptr) {
resultList = list2;
} else {
cout << "insertBegin val:" << insertBegin->val << endl;
resultList = list1;
insertBegin->next = list2;
}
while(q->next != nullptr) {
q = q->next;
cout << q->val << endl;
}
if(insertEnd != nullptr) {
cout << "insertEnd val:" << insertEnd->val << endl;
}
q->next = insertEnd;
return resultList;
}
};
void printList(ListNode *pNode) {
cout << "LinkList traversal:" << endl;
while(pNode != nullptr) {
cout << pNode->val << " ";
pNode = pNode->next;
}
cout << endl;
}
void printList(ListNode *pNode, const int size) {
cout << "LinkList traversal:" << endl;
int cnt = size;
while(pNode != nullptr && cnt--) {
cout << pNode->val << " ";
pNode = pNode->next;
}
cout << endl;
}
void trimLeftTrailingSpaces(string &input) {
input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) {
return !isspace(ch);
}));
}
void trimRightTrailingSpaces(string &input) {
input.erase(find_if(input.rbegin(), input.rend(), [](int ch) {
return !isspace(ch);
}).base(), input.end());
}
void split(const string& s, vector<string>& v, const string& c)
{
v.clear();
string::size_type pos1 = 0, pos2 = s.find(c);
while (string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length()){
v.push_back(s.substr(pos1));
}
}
string StrProcess(string str) {
int pos1 = str.find("[");
int pos2 = str.find("]");
string newStr = str.substr(pos1+1, pos2-pos1-1);
//newStr.erase(std::remove(newStr.begin(), newStr.end(), ','), newStr.end());
return newStr;
}
vector<string> stringToVector(string input) {
vector<string> inputVector;
trimLeftTrailingSpaces(input);
trimRightTrailingSpaces(input);
input = input.substr(0, input.length());//list1 = [0,1,2,3,4,5],变为:0,1,2,3,4,5;
string item;
stringstream ss;
ss.str(input);
getline(ss, item, ',');
inputVector.push_back(item);
while (true) {
if (!getline(ss, item, ',')) {
break;
}
trimLeftTrailingSpaces(item);
trimRightTrailingSpaces(item);
//cout << item << endl;
inputVector.push_back(item);
}
return inputVector;
}
vector<int> stringToIntegerVector(string input) {
vector<int> output;
trimLeftTrailingSpaces(input);
trimRightTrailingSpaces(input);
input = input.substr(0, input.length());
stringstream ss;
ss.str(input);
string item;
char delim = ',';
while (getline(ss, item, delim)) {
output.push_back(stoi(item));
}
for(auto ch : output) {
cout << ch << " ";
}
cout << endl;
return output;
}
string integerVectorToString(vector<int> list, int length = -1) {
if (length == -1) {
length = list.size();
}
if (length == 0) {
return "[]";
}
string result;
for(int index = 0; index < length; index++) {
int number = list[index];
result += to_string(number) + ", ";
}
return "[" + result.substr(0, result.length() - 2) + "]";
}
ListNode* stringToListNode(string input) {
// Generate list from the input
vector<int> list = stringToIntegerVector(input);
// Now convert that list into linked list
ListNode* dummyRoot = new ListNode(0);
ListNode* ptr = dummyRoot;
for(int item : list) {
ptr->next = new ListNode(item);
ptr = ptr->next;
}
ptr = dummyRoot->next;
delete dummyRoot;
return ptr;
}
ListNode* stringToCycleListNode(string input) {
// Generate list from the input
vector<int> list = stringToIntegerVector(input);
// Now convert that list into linked list
ListNode* dummyRoot = new ListNode(0);
ListNode* ptr = dummyRoot;
ListNode* entry = dummyRoot;
for(int item : list) {
ptr->next = new ListNode(item);
ptr = ptr->next;
if(item == 2) {
entry = ptr;
}
}
ptr->next = entry;
ptr = dummyRoot->next;
delete dummyRoot;
return ptr;
}
bool hasCycle(ListNode *head)
{
if (head == nullptr) return false;
auto fast = head;
auto slow = head;
while (fast->next != nullptr and fast->next->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
string listNodeToString(ListNode* node) {
if (node == nullptr) {
return "[]";
}
string result;
while (node) {
//result += to_string(node->val) + ", ";
result += to_string(node->val) + ",";
node = node->next;
}
return "[" + result.substr(0, result.length() - 1) + "]";
}
int main() {
string line;
string inputline;
string list1;
string list2;
int a, b;
while (getline(cin, line)) {
vector<string> sv;
split(line,sv,", ");
for (const auto& s : sv) {
cout << s << endl;
if(strstr(s.c_str(), "list1") != NULL) {
list1 = StrProcess(s);
}
if(strstr(s.c_str(), "list2") != NULL) {
list2 = StrProcess(s);
}
if(strstr(s.c_str(), "a =") != NULL) {
string t = s.substr(s.find_last_of('=') + 2, s.length() - s.find_last_of('=') - 1);
//cout << t << endl;
a = stoi(t);
}
if(strstr(s.c_str(), "b =") != NULL) {
string t = s.substr(s.find_last_of('=') + 2, s.length() - s.find_last_of('=') - 1);
//cout << t << endl;
b = stoi(t);
}
}
cout << list1 << endl;
cout << list2 << endl;
ListNode* LinkList1 = stringToListNode(list1);
printList(LinkList1);
ListNode* LinkList2 = stringToListNode(list2);
printList(LinkList2);
//ListNode* ResultList = Solution().mergeInBetween(LinkList1, 0, 0, LinkList2);
ListNode* ResultList = Solution().mergeInBetween(LinkList1, a, b, LinkList2);
//printList(ResultList);
string out = listNodeToString(ResultList);
cout << out << endl;
#if 0
inputline = StrProcess(line);
cout << inputline << endl;
inputline = StrProcess(line);
cout << inputline << endl;
//ListNode* head = stringToListNode(inputline);
ListNode* head = stringToCycleListNode(inputline);
//printList(head);
printList(head,15);
if(hasCycle(head)) {
cout << "This is a cycle list" << endl;
} else {
cout << "This is not a cycle list" << endl;
}
ListNode* ret = Solution().detectCycle(head);
cout << "Entry node val:" << ret->val << endl;
return 0;
/*
string out = listNodeToString(ret);
cout << out << endl;
*/
#endif
}
return 0;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册