题解 | #翻转链表#

翻转链表

https://www.nowcoder.com/practice/f350f14cd22c41aabfa7e54a1b8e8825

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

//解析字符串为整数向量
vector<int> parseCSV(string& input) {
    vector<int> numbers;
    stringstream ss(input);
    string token;
    while (getline(ss, token, ',')) {
        // 将token转换为整数并添加到数组
        numbers.push_back(stoi(token));
    }
    return numbers;
}


int main() 
{
    string s;  //作为字符串接受后解析数据
    vector<int> nums;
    while (cin >> s)
    {
        nums = parseCSV(s);
        int i,j=0;
        i=nums.size();
        
        for(j=0 ; j<=i/2-1 ; j++)
        {
            cout << nums[j] <<","<<nums[i-j-1];
            if(j!=i/2-1) cout<<",";
        }
        
        if( i%2==1&& i!=1) cout <<","<< nums[i/2];
        else if(i%2==1) cout << nums[i/2];
    }
    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

2024-12-26 20:46
复旦大学 C++
国棉17厂丶小王:拿了offer的那个周末晚上去网吧通宵,去网吧不知道玩什么刷了lc的每日一题,然后试着第一次打开了三角洲行动,从此少了一个已经刷了700道题的lc用户,但是烽火地带多了一只🐭🐭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务