File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- 学习笔记
1+ 学习笔记
2+ 第一周的学习内容:数组、链表、跳表、栈、队列、双端队列、循环队列
3+ 第一周的练习中,学会了解题思路的突破,不会只想一种方法来解题,而是想几种方法,寻找最优的解题思路,并背诵。
4+ 学会了5次做题法。
5+ 经过一周的训练,算法的思路有了极大的提升。
Original file line number Diff line number Diff line change 1+ //1.暴力法思路(目前我的解题思路只有这种):数组向右移动,每移动一次,最后位置的元素都放到0位置上,因此,此算法的时间复杂度为O(n*k);空间复杂度为O(1)符合题干
2+ class Solution {
3+ public void rotate (int [] nums , int k ) {
4+ int temp , pre ;
5+ for (int i = 0 ; i < k ;i ++){
6+ pre = nums [nums .length -1 ];//每向右移动一位,都要指向最后一个位置
7+ for (int j = 0 ; j < nums .length ; j ++){//数组的元素每向右移动一次,整个数组的元素都要往后移动一位,最后一位数组的元素放到第一位
8+ //交换位置
9+ temp = nums [j ];
10+ nums [j ]= pre ;
11+ pre = temp ;
12+ }
13+ }
14+ }
15+ }
16+
17+
18+ //2.使用反转(背诵的解题方法)
19+ //原理:
20+ //原始数组 : 1 2 3 4 5 6 7
21+ //反转所有数字后 : 7 6 5 4 3 2 1
22+ //反转前 k 个数字后 : 5 6 7 4 3 2 1
23+ //反转后 n-k 个数字后 : 5 6 7 1 2 3 4 --> 结果
24+
25+ public class Solution {
26+ public void rotate (int [] nums , int k ) {
27+ k %= nums .length ;
28+ reverse (nums , 0 , nums .length - 1 );//反转整个数组
29+ reverse (nums , 0 , k - 1 );//反转前k个数组
30+ reverse (nums , k , nums .length - 1 );//反转后K个数组
31+ }
32+ //反转方法
33+ //nums 数组
34+ //start:开始位置
35+ //end:结束位置
36+ public void reverse (int [] nums , int start , int end ) {
37+ while (start < end ) {
38+ int temp = nums [start ];
39+ nums [start ] = nums [end ];
40+ nums [end ] = temp ;
41+ start ++;
42+ end --;
43+ }
44+ }
45+ }
Original file line number Diff line number Diff line change 1+ //暴力方法(方法可行,但是不满足题干):1.使用两个数组,一个数组是原来的数组,一个是新的数组
2+ //双指针法:(快慢指针)
3+
4+ class Solution {
5+
6+ public int removeDuplicates (int [] nums ) {
7+ int i =0 ;
8+ for (int j = 1 ;j <nums .length ;j ++){
9+ if (nums [i ]!=nums [j ]){
10+ i ++;
11+ nums [i ]=nums [j ];
12+ }
13+ }
14+ return i +1 ;
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments