LeetCode的medium题集合(C++实现)十

1 Permutation Sequence

The set [1,2,3,…,n] contains a total of n! unique permutations.Given n and k, return the kth permutation sequence.

使用Next Permutation循环k次可以得到序列,但leetcode上提交会出现时间超过限制。下面采用数学法:

在n!个排列中,第一位元素相同的排列总是有(n?1)!个,如果p=k/(n?1)!,那么排列的第一位元素一定是nums[p]。

假设有n个元素,第K个permutation是

a1,a2,a3,........,an

设变量K1=K,利用上面的推断可知:

a1=K1/(n?1)!

a2=K2/(n?2)!

K2=K1

…….

an?1=Kn?1/1!

Kn?1=Kn?2/2!

an=Kn?1

string getPermutation(int n, int k) {
         vector<int> nums(n);
        int pCount = 1;
        for(int i = 0 ; i < n; ++i)
        {
            nums[i] = i + 1;
            pCount *= (i + 1);
        }  

        k--;
        string res = "";
        for(int i = 0 ; i < n; i++)
        {
            pCount = pCount/(n-i);
            int selected = k / pCount;
            res += (‘0‘ + nums[selected]);  

            for(int j = selected; j < n-i-1; j++)
                nums[j] = nums[j+1];
            k = k % pCount;
        }
        return res;
    }

2 Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative.For example:

Given 1->2->3->4->5->NULL and k = 2,

return 4->5->1->2->3->NULL.

先从头指针开始向后遍历直到链表尾端,用变量count记录链表长度。然后将k对count取模,将头指针向后移动count-k-1,将它的下个指针指向

NULL,将尾指针指向头指针。

ListNode* rotateRight(ListNode* head, int k) {
        if(head==NULL) return NULL;
        ListNode* last=head;
        ListNode* mid=head;
        ListNode* tail=head;
        int count=0;
        while(last!=NULL)
        {
            tail=last;
            last=last->next;
            count++;
        }
        k%=count;
        if(k==0) return head;
        int end=count-k;
        while(--end>0)
        {
            mid=mid->next;
        }
         ListNode* res=mid->next;
         mid->next=NULL;
         tail->next=head;
         return res;
    }
时间: 2024-07-31 01:48:35

LeetCode的medium题集合(C++实现)十的相关文章

LeetCode的medium题集合(C++实现)十五

1 Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1],

LeetCode的medium题集合(C++实现)十四

1 Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red,

LeetCode的medium题集合(C++实现)十二

1 Sqrt(x) Implement int sqrt(int x).Compute and return the square root of x. 因为这里都是整数,可以直接采用二分法求解 int mySqrt(int x) { if (x <= 1) return x; int begin = 1, end =x, mid = 0; while (begin<=end) { mid = (begin + end) / 2; if (mid<x / mid) //不要用乘法判断,否

LeetCode的medium题集合(C++实现)十六

1 Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of

LeetCode的medium题集合(C++实现)十一

1 Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked

LeetCode的medium题集合(C++实现)五

1 Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. 可以采用递归的方法解决这个问题,当找到一组数之和

LeetCode的medium题集合(C++实现)六

1 Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative. 该题实际就是利用字符串来解决大数的乘法问题.为了计算方便先将两组数翻转,将字符串转化为整数利用两个循环逐位相乘,将结果保存.然后逐位解决进位问题. s

LeetCode的medium题集合(C++实现)四

1 Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending or

LeetCode的medium题集合(C++实现)九

1 Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index. 利用两指针法