做题经验

1.  题目:将字符串“326”,“+326”,“-326”转换成对应的数字输出:326,+326,-326

1)  

#include "stdafx.h"
#include <iostream>
#include <stdio.h>

int main(int argc, char* argv[])
{
	using namespace std;
	//int X = 3, Y = 10;
	char str[3][10] = { "326", "+326", "-326" };
	int  str2num[3] = { 0, 0, 0 };
	char b[] = "+326";
	char c[] = "-326";

	cout << sizeof(str2num) << " " << sizeof(int);
	cout << strlen(str[0]) << endl;
	cout << "a0" << " " << str[0][0] << endl;
	cout << "a1" << " " << str[0][1] << endl;
	cout << "a2" << " " << str[0][2] << endl;
	cout << str[0][3] << endl;
	

	cout << "10^0" << " " << pow(10, 0) << endl;
	for (int i = 0; i < 3; i++)
	{
		for (int j = (strlen(str[i]) - 1); j >= 0; j--)
		{
			if (str[i][j] == '+' || str[i][j] == '-')
			{
				if (str[i][j] == '-')
					str2num[i] = -str2num[i];
			}
			else
				str2num[i] += (int(str[i][j]) - 48) * pow(10, (strlen(str[i]) - 1 - j));

		}

	}
	for (int i = 0; i < sizeof(str2num) / sizeof(int); i++)
		cout << str2num[i] << endl;
	system("pause");
	return 0;
	}
2)

#include "stdafx.h"
#include<stdio.h>  
#include<string.h>
#include <iostream>

int StringInt(const char *str)
{
	int i, length, value, flag;
	length = strlen(str);
	flag = 1;
	value = *(str + length - 1) - 48;      //个位  

	for (i = length - 2; i >= 0; --i)
	{
		if (*(str + i) == '+')
			break;
		else if (*(str + i) == '-')
			value = -value;
		else
		{
			flag *= 10;
			value += (*(str + i) - 48)*flag;
		}
	}

	return value;
}

int main(void)
{
	printf("%d\n", StringInt("326"));
	printf("%d\n", StringInt("+326"));
	printf("%d\n", StringInt("-326"));
	system("pause");

	return 0;
}

输入一个链表,反转链表后,输出链表的所有元素。

//第一种方法是:非递归方法
/*
struct ListNode {
     int val;
     struct ListNode *next;
     ListNode(int x) :
             val(x), next(NULL) {
     }
};*/
class Solution {
public :
     ListNode* ReverseList(ListNode* pHead) {
         
         if (pHead==NULL)  return NULL; //注意程序鲁棒性
         
         ListNode* pNode=pHead; //当前指针
         ListNode* pReverseHead=NULL; //新链表的头指针
         ListNode* pPrev=NULL; //当前指针的前一个结点
         
         while (pNode!=NULL){ //当前结点不为空时才执行
             ListNode* pNext=pNode->next; //链断开之前一定要保存断开位置后边的结点
             
             if (pNext==NULL) //当pNext为空时,说明当前结点为尾节点
                 pReverseHead=pNode;
  
             pNode->next=pPrev; //指针反转
             pPrev=pNode;
             pNode=pNext;
         }
         return pReverseHead;
     }
}
 
//第二种方法是:递归方法 /*
struct ListNode {
     int val;
     struct ListNode *next;
     ListNode( int x) :
             val(x), next(NULL) {
     }
};*/
class Solution {
public :
     ListNode* ReverseList(ListNode* pHead) {
         //如果链表为空或者链表中只有一个元素
         if (pHead==NULL||pHead->next==NULL)  return pHead;
         
         //先反转后面的链表,走到链表的末端结点
         ListNode* pReverseNode=ReverseList(pHead->next);
         
         //再将当前节点设置为后面节点的后续节点
         pHead->next->next=pHead;
         pHead->next=NULL;
         
         return pReverseNode;
         
     }
};
递归的方法其实是非常巧的,它利用递归走到链表的末端,然后再更新每一个node的next 值 ,实现链表的反转。而newhead 的值没有发生改变,为该链表的最后一个结点,所以,反转后,我们可以得到新链表的head。

注意关于链表问题的常见注意点的思考:

1、如果输入的头结点是 NULL,或者整个链表只有一个结点的时候

2、链表断裂的考虑



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值