合并链表
这里就不说了,稍微看下代码应该就可以懂了
递归:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// write your code here
if(l1==NULL||l2==NULL)
return l1==NULL?l2:l1;
ListNode* head=NULL;
if(l1->val > l2->val)
head=l2;
else
head=l1;
if(l1->val>l2->val)
head->next=mergeTwoLists(l1,l2->next);
else
head->next=mergeTwoLists(l1->next,l2);
};
非递归:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// write your code here
if(l1==NULL || l2 == NULL)
return l1==NULL?l2:l1;
ListNode* head=NULL,*cur=NULL,*tmp1=l1,*tmp2=l2;
if(l1->val > l2->val)
{
head=l2;
tmp2=head->next;
}
else
{
head=l1;
tmp1=head->next;
}
cur=head;
while(tmp1&&tmp2)
{
if(tmp1->val > tmp2->val)
{
cur->next=tmp2;
tmp2=tmp2->next;
}
else
{
cur->next=tmp1;
tmp1=tmp1->next;
}
cur=cur->next;
}
if(tmp1==NULL)
cur->next=tmp2;
else
cur->next=tmp1;
return head;
}
};
求1+2+…+n不用循环、乘除法、循环、条件判断、选择相关的关键字
这有很多方法,这只是其中一种
namespace t1{
class test
{
public:
test()
{
a++;
}
int get_a()
{
return a;
}
protected:
static int a;
};
int test::a = 0;
int main()
{
test* t1 = new test[100];
printf("%d", t1->get_a());
return 0;
}
}