File tree Expand file tree Collapse file tree 2 files changed +45
-30
lines changed Expand file tree Collapse file tree 2 files changed +45
-30
lines changed Original file line number Diff line number Diff line change 5
5
6
6
### :pencil2 : 描述
7
7
8
- ** 当前机器人位于左上角的位置,每次只能向下或者向右移动一位,求到达网格的右下角有多少种独特的方式 **
8
+ ** 给定一个链表,把链表中的每一个数都向右移动 k 位。其中 k 是非负数。图中给了很详细的每步变化。 **
9
9
### :pencil2 : 题目实例
10
10
11
11
<a href =" https://github.com/wuqinqiang/ " >
14
14
15
15
****
16
16
### :pencil2 : 题目分析
17
-
18
- ** 这是一个典型的动态规划题型,对于动态规划,之前写过一篇文章,这两天会根据自己的理解,重新再写一篇。动态规划最重要的两步:1.定义状态。2动态转移公式。因为开始的时候向下或者向右都是一条路径。所以 dp[ 0] [ i ] 和 dp[ i] [ 0 ] 都等于1。然后推导出转移方程。这个转移方程也是根据每次只能向下或者向右移动推导出的。**
19
-
20
- ``` php
21
- dp[$i][$j] 表示从(0,0)到(i,j)路径总数。
22
-
23
- dp[$i][$j]=$dp[$i-1][$j]+$dp[$i][$j-1] 转移方程
24
17
18
+ ** 先说下思路,其实整体只需要两步,第一步先将这个链表闭环。什么意思呢,拿 demo1 来说,本来节点5的 next 指向的是 null,现在把它变为 5->next=1。这样的话就形成了闭环。第二步当然就是计算新链表的头和尾分别是哪个节点(头结点的上一个节点一定是新链表的尾节点),本质上就是在合适的地方截断这个闭环的链表,然后头尾相连。**
19
+
20
+
21
+
22
+ ** 新的链表头部位置必然在 n-k 处 (n是链表节点个数),那尾结点就必然在 n-k-1 处,但是这样只考虑到 k<n 的情况。如果 k>=n,那么计算头节点应该是 (n-k%n)处,尾节点即(n-k%n-1)处。剩下的就是代码的处理**
25
23
26
- ```
27
24
### :pencil2 : 最终实现代码
28
25
29
26
``` php
30
27
/**
31
- * @param Integer $m
32
- * @param Integer $n
33
- * @return Integer
34
- */
35
- function uniquePaths($m, $n) {
36
-
37
- for($i=0;$i<$m;$i++){
38
- $dp[$i][0]=1;
39
- }
40
-
41
- for($j=0;$j<$n;$j++){
42
- $dp[0][$j]=1;
43
- }
44
-
45
- for($i=1;$i<$m;$i++){
46
- for($j=1;$j<$n;$j++){
47
- $dp[$i][$j]=$dp[$i-1][$j]+$dp[$i][$j-1];
48
- }
49
- }
50
- return $dp[$m-1][$n-1];
51
- }
28
+ * Definition for a singly-linked list.
29
+ * class ListNode {
30
+ * public $val = 0;
31
+ * public $next = null;
32
+ * function __construct($val) { $this->val = $val; }
33
+ * }
34
+ */
35
+ class Solution {
36
+
37
+ /**
38
+ * @param ListNode $head
39
+ * @param Integer $k
40
+ * @return ListNode
41
+ */
42
+ function rotateRight($head, $k) {
43
+ if($head==null) return null;
44
+ if($head->next==null) return $head;
45
+ $old_tail=$head;
46
+ $n=1;
47
+ //获取节点个数
48
+ while($old_tail->next !=null){
49
+ $old_tail=$old_tail->next;
50
+ $n++;
51
+ }
52
+ $old_tail->next=$head;
53
+ $new_tail=$head;
54
+ //获取尾结点
55
+ for($i=0;$i<$n-$k%$n-1;$i++){
56
+ $new_tail=$new_tail->next;
57
+ }
58
+ //头结点
59
+ $new_head=$new_tail->next;
60
+ //尾结点的next指向null
61
+ $new_tail->next=null;
62
+
63
+ return $new_head;
64
+ }
65
+ }
52
66
```
53
67
****
54
68
Original file line number Diff line number Diff line change 71
71
- [ Leetcode55] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/55.md )
72
72
- [ Leetcode56] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/56.md )
73
73
- [ Leetcode59] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/59.md )
74
+ - [ Leetcode61] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/61.md )
74
75
- [ Leetcode62] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/62.md )
75
76
- [ Leetcode63] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/63.md )
76
77
- [ Leetcode64] ( https://github.com/wuqinqiang/Lettcode-php/blob/master/51-100/64.md )
You can’t perform that action at this time.
0 commit comments