File tree 2 files changed +59
-0
lines changed
Algorithm/src/leetCode/subject/number51_100
2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetCode .subject .number51_100 ;
2
+
3
+ /**
4
+ * @author : CodeWater
5
+ * @create :2022-08-05-23:29
6
+ * @Function Description :70. 爬楼梯
7
+ */
8
+ public class _70ClimbingStairs {
9
+ class Solution {
10
+ public int climbStairs (int n ) {
11
+ /**一开始在0阶。每一次走到当前阶,都是由上一步跳到当前阶,而上一步有两种情况,
12
+ 一是跳一步;二是跳两步。相当于求一个斐波那契数列。
13
+ */
14
+ int a = 1 , b = 1 ;
15
+ // 执行n-1次
16
+ while ( --n > 0 ){
17
+ int c = a + b ;
18
+ a = b ;
19
+ b = c ;
20
+ }
21
+ return b ;
22
+ }
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ package leetCode .subject .number51_100 ;
2
+
3
+ /**
4
+ * @author : CodeWater
5
+ * @create :2022-08-05-23:45
6
+ * @Function Description :75. 颜色分类
7
+ */
8
+ public class _75ColorClassification {
9
+ class Solution {
10
+ /**本题方法特殊,背过。仅适用这个题目。本质上是一个双指针算法。要求:
11
+ 1.只扫描一遍
12
+ 2.空间要O(1)
13
+ 做法:ij从左往右扫描,k从右往左扫描。初始:ij在0处,k在最后一个位置
14
+ 扫描的过程中,保证:从0到j-1这个范围是0;从j到i-1范围是1;从k+1到n-1是2。
15
+ 当i》=k时,就可以保证排好序了。
16
+ a[i]=0,交换ij位置上的元素;i++,j++
17
+ a[i]=2,交换ik位置上的元素;k--,但是i不要动,因为不确定交换的元素是不是1,可能是0
18
+ a[i]=1,i++.
19
+ i是++,k是--,所以扫描一遍肯定已经排好序了
20
+ */
21
+ public void sortColors (int [] nums ) {
22
+ for ( int i = 0 , j = 0 , k = nums .length - 1 ; i <= k ; ){
23
+ if ( nums [i ] == 0 ) swap (nums , i ++ , j ++ );
24
+ else if ( nums [i ] == 2 ) swap ( nums , i , k -- );
25
+ else i ++;
26
+ }
27
+ }
28
+
29
+ public void swap ( int [] a , int i , int j ){
30
+ int temp = a [i ];
31
+ a [i ] = a [j ];
32
+ a [j ] = temp ;
33
+ }
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments