【LeetCode23-29+518】 K个有序子链,交换链表顺序(指针调换),Vector删除元素,移位运算符>>,DP算法

来源:互联网 发布:森田疗法 知乎 编辑:程序博客网 时间:2024/06/06 13:20

23.连接K个有序子链,在21题的基础上加一个循环就好了……

class Solution {public:    ListNode* mergeKLists(vector<ListNode*>& lists) {        ListNode result(0);        ListNode *temp=&result;        for(int i=0;i<lists.size();i++){            temp->next=mergeTwoLists(temp->next,lists[i]);        }        return result.next;    }        ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {          ListNode result(0);          ListNode *temp;temp = &result;      //  result.val = l1->val > l2->val ? l2->val : l1->val;          while (l1 && l2) {              if (l1->val < l2->val) {                  temp->next = l1;                  l1 = l1->next;              }              else {                  temp->next = l2;                  l2 = l2->next;              }              temp = temp->next;          }          temp->next = l1 ? l1 : l2;//很重要,把空的都解决了。。。          return result.next;      }  };



24.每两个交换单链表顺序


//解题感受是,指针得画个图才能弄得清,不然容易出错…

Given 1->2->3->4, you should return the list as 2->1->4->3.

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution {public:    ListNode* swapPairs(ListNode* head) {                if(!head||!head->next)return head;                       ListNode result(0);        ListNode *temp=&result;     //   head->next = swapPairs(result->next);    //    result->next = head;         //   return result;                        while(head&&head->next){            temp->next=head->next;            //指针指针指针,和值传参不一样,得画个图……            //比如用temp指向了head的一位,然后修改temp的下一位的时候其实把head的下一位也修改了,所以要考虑顺序……            //head往后挪一位相当于原来的挪两位…            head->next=temp->next->next;//顺序真的非常重要            temp=temp->next;            temp->next=head;            temp=temp->next;            head=head->next;                    }        return result.next;                    }        /*    ListNode* swapPairs(ListNode* head) {    ListNode **pp = &head, *a, *b;    while ((a = *pp) && (b = a->next)) {        a->next = b->next;        b->next = a;        *pp = b;        pp = &(a->next);    }    return head;}*/};

25.类似于24,每K个交换单链表顺序


Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5


//做的时候用了vector存储了ListNode的地址……

//用到了迭代的思想,看着会舒服很多

//23ms,击败38%

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* reverseKGroup(ListNode* head, int k) {        //试试用迭代                ListNode result(0);        ListNode *temp=&result;        vector<ListNode*>T;        ListNode *origin=head;//origin的作用是在修改了head之后还能返回之前的…之所以不用T[0]是因为T可能为空        for(int i=0;i<k;i++){if(!head)return origin;T.push_back(head);head=head->next;}        for(int i=0;i<k;i++){temp->next=T[k-1-i];temp=temp->next;}        temp->next=reverseKGroup(head,k);        return result.next;    }};


26.删除有序数组里重复的数据并返回数组大小

//相对挺容易的,但要注意Vector删除元素的用法

class Solution {public:    int removeDuplicates(vector<int>& nums) {      //  unordered_map<int,int> mapping;     //   int result=0;    //    for(int i=0;i<nums.size();i++){   //        if(mapping.find(nums[i])==mapping.end()){mapping[nums[i]]=0;result++;}  //     } //      return result;                int length=nums.size();        for(int i=0;i<length-1;i++){            if(nums[i]==nums[i+1]){               vector<int>::iterator it = nums.begin()+i+1; nums.erase(it);//删除元素              i--; length--;            }        }        return length;                //删除元素的用法        /*   std::vector<int>::iterator it = vec.begin()+10;vec.erase(it);*/    }};


27.删除数组里特定数值并返回长度

//知道怎么删除Vector数组里指定位置元素就很简单了

class Solution {public:    int removeElement(vector<int>& nums, int val) {        int length=nums.size();        for(int i=0;i<length;i++){            if(nums[i]==val){                vector<int>::iterator it=nums.begin()+i;                nums.erase(it);                length--;                i--;            }        }        return length;    }};


28.字符串中第一次出现制定字符串位置

//判断字符串是否一样还是用==,不要想太多…………

class Solution {public:    int strStr(string haystack, string needle) {        int result=-1;        if(needle=="")return 0;        int length=needle.size();        if(haystack.size()<length)return -1;        for(int i=0;i<haystack.size()-length+1;i++){            if(haystack[i]==needle[0]){                if(needle==haystack.substr(i,length))return i;            }        }        return result;    }};


29.实现整数的出发,不能用乘除和模运算(需要用到移位运算法)

typedef long long ll;#define MAX 2147483647class Solution {public:int divide(int n, int d) {    ll result=0;    ll n_=abs(ll(n));    ll d_=abs(ll(d));        while(n_>=d_){        int m=1;        ll temp=d_;//本质上的除法又实现了一遍        while((temp<<1)<n_){m<<=1;temp<<=1;}//注意<<=才是真正移位了!!        result+=m;        n_-=temp;    }    if((n<0&&d>0)||(n>0&&d<0))result= -result;    if(result>MAX||result<-MAX-1)return MAX;    return result;}};









518,给出找零钱所有策略个数


Example 1:

Input: amount = 5, coins = [1, 2, 5]Output: 4Explanation: there are four ways to make up the amount:5=55=2+2+15=2+1+1+15=1+1+1+1+1

Example 2:

Input: amount = 3, coins = [2]Output: 0Explanation: the amount of 3 cannot be made up just with coins of 2.

Example 3:

Input: amount = 10, coins = [10] Output: 1


//DP算法……

class Solution {public:int change(int amount, vector<int>& coins) {    unordered_map <int,int>mapping;    mapping[0]=1;    for(int i=0;i<coins.size();i++){        for(int j=coins[i];j<=amount;j++)        mapping[j]+=mapping[j-coins[i]];    }    return mapping[amount];}};


0 0