Repository navigation
Expand file tree
/
Copy patharray2.java
More file actions
182 lines (163 loc) · 5.21 KB
/
Copy patharray2.java
File metadata and controls
182 lines (163 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.util.Arrays;
import java.util.Scanner;
public class array2 {
//杨辉三角
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入n的数字:");
int n = scan.nextInt();
int array[][] = new int[n][];
for (int i = 0; i < array.length; i++) {
array[i] = new int[i + 1];
for (int j = 0; j < array[i].length; j++) {
if (j == 0 || j == array[i].length - 1) {
array[i][j] = 1;
} else {
array[i][j] = array[i - 1][j] + array[i - 1][j - 1];
}
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
//判断数组有连续三个奇数
public static boolean func4(int[] array) {
int count = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 != 0) {
count++;
if (count == 3) {
return true;
}
} else {
count = 0;
}
}
return false;
}
public static void main9(String[] args) {
int[] array = {1, 2, 3, 2, 1, 2, 2, 2};
System.out.println(func4(array));
}
//
public int majorityElement(int[] array) {
int ret = array[0];
int count = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == ret) {
count++;
} else {
count--;
}
if (count == 0) {
ret = array[i + 1];
}
}
return ret;
}
public static void main8(String[] args) {
int[] array = {1, 2, 3, 2, 1, 2, 2, 2};
}
public static int fun3(int[] array) {
int ret = array[0];
for (int i = 1; i < array.length; i++) {
ret = ret ^ array[i];
}
return ret;
}
public static void main7(String[] args) {
int[] array = {1, 2, 3, 2, 1};
fun3(array);
System.out.println(fun3(array));
}
public static void func1(int[] array) {
int i = 0;
int j = array.length - 1;
while (i < j) {
while (i < j && array[i] % 2 != 0) {
i++;
}
while (i < j && array[j] % 2 == 0) {
j--;
}
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
public static void main6(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7};
func1(array);
System.out.println(Arrays.toString(array));
}
public static void transform(int[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = array[i] * 2;
}
}
public static void main5(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7};
transform(array);
System.out.println(Arrays.toString(array));
}
public static void main4(String[] args) {
//不规则的二维数组
int[][] array1 = new int[2][];
array1[0] = new int[3];
array1[1] = new int[4];
}
public static void main3(String[] args) {
int[][] array1 = new int[2][3];
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
System.out.print(array1[i][j] + " ");
}
System.out.println();
}
System.out.println();
for (int[] arr : array1) {
for (int x : arr) {
System.out.print(x + " ");
}
System.out.println();
}
// System.out.println();
// System.out.println(Arrays.toString(array1));//地址
// System.out.println(Arrays.deepToString(array1));//数组
}
public static void main2(String[] args) {
//定义二维数组
int[][] array1 = new int[2][3];
int[][] array2 = new int[][]{{1, 2, 3}, {4, 5, 6}};
int[][] array3 = {{1, 2, 3}, {4, 5, 6}};
}
//冒泡排序法
public static void bubbleSort(int[] array) {
//i控制趟数
for (int i = 0; i < array.length - 1; i++) {
//优化:检查一躺之后,是否有序了?
int flg = 1;
//array.lengh-1-i 优化的是 每一趟比较的次数
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
flg = -1;
}
}
if (flg == 1) {
return;
}
}
}
public static void main1(String[] args) {
int[] array2 = {1, 2, 3, 4, 5, 6, 7};
bubbleSort(array2);
System.out.println(Arrays.toString(array2));
}
}