头像

时过境迁




离线:16小时前



/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> printListReversingly(ListNode* head) {
        vector<int> v;
        while(head)
        {
            v.push_back(head->val);
            head = head->next;
        }


        return vector<int>(v.rbegin(), v.rend()) ;
        }
};



void reOrderArray(int *array, int length) {
    int arr[length];
    int k = 0;
    for(int i = 0; i < length; ++i)
    {
        if(array[i] % 2 == 1)
        {
            arr[k++] = array[i];
        }
    }
    for(int j = 0; j < length; ++j)
    {
        if(array[j] % 2 == 0)
        {
            arr[k++] = array[j];
        }
    }

    for(int z = 0; z < length; ++z)
    {
        array[z] = arr[z];
    }
}


活动打卡代码 AcWing 35. 反转链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseList(struct ListNode* head) {
    struct ListNode * p1, *p2, *p3, *ans;
    p1 = head;

    if(head)
    {
        p2 = head->next;
        p1->next = NULL;
        while(p2)
        {
            p3 = p2->next;  
            p2->next = p1;
            p1 = p2;
            p2 = p3;
        }
    }

    ans = p1;
    return ans;
}



/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* merge(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *ans = NULL, *l3 = NULL;
    ans = (struct ListNode *)malloc(sizeof(struct ListNode));
    ans->val = -1;
    ans->next = NULL;
    l3 = ans;
    while(l1 && l2)
    {
        if(l1->val <= l2->val)
        {
            l3->next = l1;
            l1 = l1->next;
            l3 = l3->next;
        }
        else
        {
            l3->next = l2;
            l2 = l2->next;
            l3 = l3->next;
        }
    }
    if(l1)
    {
        l3->next = l1;
    }
    else if(l2)
    {
        l3->next = l2;
    }
    return ans->next;

}



int getMissingNumber(int* nums, int numsSize) {
    int ans;
    int flag = 0;           //判断是否在数组内出现
    for(int i = 0; i < numsSize; ++i)
    {
        if(nums[i] != i)
        {
            flag = 1;       
            ans = i;
            break;
        }
    }
    if(flag == 0)           //数组内都有出现,则为长度
    {
        ans = numsSize;
    }
    return ans;
}



int getNumberOfK(int* nums, int numsSize, int k) {
    int ans = 0;
    for(int i = 0; i < numsSize; ++i)
    {
        if(nums[i] == k)
        {
            ans++;
        }
    }
    return ans;
}



class Solution {
public:
    int NumberOf1(int n) {
        int ans = 0;
        for(int i = 0; i < 32; ++i)
        {
            if((n & (1<<i)) == (1<<i))
            {
                ans++;
                // cout<<i<<" ";
            }
        }
        return ans;
    }
};



class Solution {
public:
    int strToInt(string str) {
        int flag = 1;           //判断符号
        int len = str.size();
        long long ans = 0;      
        int i = 0;     //下标


        while(i < len && str[i] == ' ')
        {
            i++;
        }

        if(str[i] == '-')
        {
            flag = -1;
            i++;
        }
        else if(str[i] == '+')
        {
            i++;
        }

        // cout<<i<<endl;

        // for( ; i < len;)
        // {
        //     if(str[i] >= '0' && str[i] <= '9')
        //     {
        //         ans *= 10;
        //         ans += str[i] - '0';
        //         ++i;
        //     }
        // }
        while(i < len && str[i] >= '0' && str[i] <= '9')
        {
            ans *= 10;
            ans += str[i] - '0';
            i++;
        }
        if(flag == -1)
        {
            ans *= -1;
        }

        if(ans > INT_MAX)
        {
            ans = INT_MAX;
        }
        if(ans < INT_MIN)
        {
            ans = INT_MIN;
        }
        return (int)ans;

    }
};



/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
void deleteNode(struct ListNode* node) {
    struct ListNode *node_next = node->next;
    node->val = node_next->val;
    node->next = node_next->next;

}


活动打卡代码 AcWing 84. 求1+2+…+n

class Solution {
public:
    int getSum(int n) {
        if(n % 2 == 0)
        {
            return n/2*(n+1);  //n为偶数,/2可以整除
        }
        return (n+1)/2*n;   //n为奇数的情况
    }
};