Leetcode题解(20)

59. Spiral Matrix II

题目

这道题copy网上的代码

 1 class Solution {
 2 private:
 3     int step[4][2];
 4     bool canUse[100][100];
 5 public:
 6     void dfs(int dep, vector<vector<int> > &matrix, int direct, int x, int y)
 7     {
 8         for(int i = 0; i < 4; i++)
 9         {
10             int j = (direct + i) % 4;
11             int tx = x + step[j][0];
12             int ty = y + step[j][1];
13             if (0 <= tx && tx < matrix.size() && 0 <= ty && ty < matrix[0].size() && canUse[tx][ty])
14             {
15                 canUse[tx][ty] = false;
16                 matrix[tx][ty] = dep;
17                 dfs(dep + 1, matrix, j, tx, ty);
18             }
19         }
20     }
21
22     vector<vector<int> > generateMatrix(int n) {
23         // Start typing your C/C++ solution below
24         // DO NOT write int main() function
25         step[0][0] = 0;
26         step[0][1] = 1;
27         step[1][0] = 1;
28         step[1][1] = 0;
29         step[2][0] = 0;
30         step[2][1] = -1;
31         step[3][0] = -1;
32         step[3][1] = 0;
33         vector<vector<int> > ret(n, vector<int>(n));
34         memset(canUse, true, sizeof(canUse));
35         dfs(1, ret, 0, 0, -1);
36
37         return ret;
38     }
39
40 };

----------------------------------------------------------------------------分割线----------------------------------------------------------------------

60. Permutation Sequence

题目

分析:这道题主要考数学推断

在n!个排列中,第一位的元素总是(n-1)!一组出现的,也就说如果p = k / (n-1)!,那么排列的最开始一个元素一定是nums[p]。

假设有n个元素,第K个permutation是
a1, a2, a3, .....   ..., an
那么a1是哪一个数字呢?
那么这里,我们把a1去掉,那么剩下的permutation为
a2, a3, .... .... an, 共计n-1个元素。 n-1个元素共有(n-1)!组排列,那么这里就可以知道
设变量K1 = K
a1 = K1 / (n-1)!
同理,a2的值可以推导为
a2 = K2 / (n-2)!
K2 = K1 % (n-1)!
 .......
a(n-1) = K(n-1) / 1!
K(n-1) = K(n-2) /2!
an = K(n-1)

代码如下:

 1 class Solution {
 2 public:
 3     string getPermutation(int n, int k) {
 4         vector<int> nums(n);
 5         int pCount = 1;
 6         for(int i = 0 ; i < n; ++i) {
 7             nums[i] = i + 1;
 8             pCount *= (i + 1);
 9         }
10
11         k--;
12         string res = "";
13         for(int i = 0 ; i < n; i++) {
14             pCount = pCount/(n-i);
15             int selected = k / pCount;
16             res += (‘0‘ + nums[selected]);
17
18             for(int j = selected; j < n-i-1; j++)
19                 nums[j] = nums[j+1];
20             k = k % pCount;
21         }
22         return res;
23     }
24 };

------------------------------------------------------------------------分割线--------------------------------------------------------------------------

61. Rotate List

题目

代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* rotateRight(ListNode* head, int k) {
12         //如果k值大于链表长度应该怎么处理
13         if(NULL == head)
14             return NULL;
15         ListNode* temp = head;
16         int count = 0;
17         while(temp != NULL)
18         {
19             count++;
20             temp = temp->next;
21         }
22         k = k%count;
23         if(k == 0)
24             return head;
25         ListNode *first,*second;
26         first = head;
27         int i=k;
28         while(i--)
29         {
30             //if(NULL == first)
31             //    return NULL;
32             first = first->next;
33         }
34         second = head;
35         while(first->next != NULL)
36         {
37             first = first->next;
38             second = second->next;
39         }
40         temp = head;
41         head = second->next;
42         first->next = temp;
43         second->next = NULL;
44         return head;
45     }
46 };
时间: 2024-10-24 22:37:32

Leetcode题解(20)的相关文章

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/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

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order tr

leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] co