第1组

1.反转链表LeetCode206Easy

分析:从前往后扫,一个一个扫即可。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
       ListNode *new_head=NULL; 
         while(head){
             ListNode *next=head->next;
             head->next=new_head;
             new_head=head;
             head=next;
        }
        return new_head;
    }
};
View Code

2求子集LeetCode78Medium

分析:位运算回溯

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>>result;
        int number=1<<nums.size();
        for(int i=0;i<number;i++){
            vector<int> temp;
            for(int j=0;j<nums.size();j++){
                if(i&1<<j){
                    temp.push_back(nums[j]);
                }
                
            }
            result.push_back(temp);
        }
        return result;
    }
};
View Code

猜你喜欢

转载自www.cnblogs.com/zfc888/p/10544434.html