leetcode(一)

反转字符串

void reverseString(char* s, int sSize){
	int low = 0,high = sSize-1;
    char temp;
    while(low<=high)
    {
        temp = s[high];
        s[high] = s[low];
        s[low] = temp;
        low++;
        high--;
    }
    return s;
}

两两交换链表中的结点

struct ListNode* swapPairs(struct ListNode* head){
    if(head == NULL || head->next == NULL)
        return head;
    ListNode *p = head;
    ListNode *q = new ListNode(-1);
    ListNode *q2 = new ListNode(-1);
    q2->next = head->next;
    
    while(p!=NULL && p->next != NULL){
        q->next = p->next->next;
			p->next->next = p;
			if (q->next != NULL) {
				if (q->next->next == NULL)
					p->next = q->next;
				else
					p->next = q->next->next;
            }
        	else
                p->next = NULL;
        p=q->next;
    }
    return q2->next;
}

两数之和

int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    int i,j;
    *returnSize = NULL;
    for(i=0;i<numsSize-1;i++)
    {
        for(j=i+1;j<numsSize;j++)
        {
            if(nums[i]+nums[j]==target)
            {
                 returnSize=(int*)malloc(sizeof(int)*2);
                 returnSize[0]=i;
                 returnSize[1]=j;
            }
        }
    }
    return returnSize;
}
//C++
 vector<int> result;
        for(int i=0;i<nums.size();++i)
        {
            for(int j=i+1;j<nums.size();++j)
            {
                if(nums[i]+nums[j] == target)
                {
                    result.push_back(i);
                    result.push_back(j);
                }
            }
        }
        return result;
    }

整数反转

int reverse(int x){
    long int num=0;
    while(x)
    {
        num = 10*num +x%10;
        if(num > INT_MAX || num < INT_MIN)
            return 0;
        x=x/10;
    }
    return num;
}

回文数

bool isPalindrome(int x){
if (x < 0) return false;  
  int div = 1;  
  while (x / div >= 10) {  
    div *= 10;  
  }          
  while (x != 0) {  
    int l = x / div;  
    int r = x % 10;  
    if (l != r) return false;  
    x = (x % div) / 10;  //去掉两边的数
    div /= 100;  
  }  
  return true;  
}

只出现过一次的数字

int singleNumber(int* nums, int numsSize){
int i, t=0;
    for(i=0; i<numsSize;i++)
    {
        t ^= nums[i]; //利用异或的方法
    }
    return t;
}

//多数元素
int majorityElement(int* nums, int numsSize){
   int count = 1;
   int maj = nums[0];
        for (int i = 1; i < numsSize; i++) {
            if (maj == nums[i])
                count++;
            else {
                count--;
                if (count == 0) {
                    maj = nums[i + 1];
                }
            }
        }
        return maj;
}

两数相加

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int len1=1;//记录l1的长度
        int len2=1;//记录l2的长度
        ListNode* p=l1;
        ListNode* q=l2;
        while(p->next!=NULL)//获取l1的长度
        {
            len1++;
            p=p->next;
        }
        while(q->next!=NULL)//获取l2的长度
        {
            len2++;
            q=q->next;
        }
        if(len1>len2)//l1较长,在l2末尾补零
        {
            for(int i=1;i<=len1-len2;i++)
            {
                q->next=new ListNode(0);
                q=q->next;
            }
        }
        else//l2较长,在l1末尾补零
         {
            for(int i=1;i<=len2-len1;i++)
            {
                p->next=new ListNode(0);
                p=p->next;
            }
        }
        p=l1;
        q=l2;
        bool count=false;//记录进位
        ListNode* l3=new ListNode(-1);//存放结果的链表
        ListNode* w=l3;//l3的移动指针
        int i=0;//记录相加结果
        while(p!=NULL&&q!=NULL)
        {
            i=count+p->val+q->val;
            w->next=new ListNode(i%10);
            count=i>=10?true:false;
            w=w->next;
            p=p->next;
            q=q->next;
        }
        if(count)//若最后还有进位
        {
            w->next=new ListNode(1);
            w=w->next;
        }
        return l3->next; 
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值