-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathArithmeticTwo.java
More file actions
356 lines (229 loc) · 7.76 KB
/
ArithmeticTwo.java
File metadata and controls
356 lines (229 loc) · 7.76 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package basic;
/**
* Created by zxc on 2018/3/23.
*/
public class ArithmeticTwo {
public static void main(String[] args) {
int[] arrays = {5, 5, 6, 4, 4, 6, 3};
//singleNumber(arrays);
drawStar();
}
/**
* 画星星
*/
public static void drawStar() {
// 我要画5行的星星
int row = 5;
for (int i = 1; i <= 5; i++) {
// 空格数等于最大行数 - 当前行数
for (int j = 1; j <= row - i; j++) {
System.out.print(" ");
}
// 星星数等于(当前行数*2-1)
for (int j = 1; j <= i * 2 - 1; j++) {
System.out.print("*");
}
// 每画一行就换一次行
System.out.println();
}
}
/**
* 找出数组的单个数字
*
* @param nums
* @return
*/
public static void singleNumber(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int count = countNumber(nums, nums[i]);
// 如果该元素只出现一次,那么就是它了!
if (count == 1) {
System.out.println("关注公众号:Java3y--->单一的元素是:" + nums[i]);
return;
}
}
}
/**
* 找出每个元素出现的次数
*
* @param nums 数组
* @param value 想知道出现次数的元素
*/
public static int countNumber(int[] nums, int value) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (value == nums[i]) {
count++;
}
}
// 返回该元素出现的次数
return count;
}
/**
* 找出数组的单个数字
*
* @param nums
* @param numsSize
* @return
*/
public static int singleNumber(int[] nums, int numsSize) {
// 第一个数和数组后面的数做^运算,留下的必然是单个数字
int k = nums[0];
for (int i = 1; i < numsSize; i++) {
k = (k ^ nums[i]);
}
return k;
}
/**
* 移动元素0到数组最后
*
* @param arrays
*/
public static void moveZero(int[] arrays) {
// 记录该数组有多少个0元素
int zero = 0;
for (int i = 0; i < arrays.length; i++) {
// 只要元素不为0,那么就往前面移动
if (arrays[i] != 0) {
arrays[i - zero] = arrays[i];
} else {
// 如果为0,那么zero ++
zero++;
}
}
// 1. 前面已经将非0的元素移动到数组的前面了
// 2. 将为0的元素填满数组,填充的位置就从length - zero开始
int j = arrays.length - zero;
while (j < arrays.length) {
arrays[j] = 0;
j++;
}
System.out.println("关注公众号:Java3y---->" + arrays);
}
/**
* 移动元素0到数组最后
*
* @param arrays
*/
public static void moveZero2(int[] arrays) {
// 在j前面的元素都不是0
int j = 0;
for (int i = 0; i < arrays.length; i++) {
if (arrays[i] != 0) {
// 跟j进行交换,保证j的前面都不是0
int temp = arrays[i];
arrays[i] = arrays[j];
arrays[j] = temp;
j++;
}
}
// 直至i遍历完毕后,j前面都不是0,j-i都是0(这就完成我们的任务了)
System.out.println("关注公众号:Java3y---->" + arrays);
}
/**
* 找到缺失的数字
*
* @param arrays
*/
public static void missingNumber(int[] arrays) {
// 定义要填充到新数组的数字(随意)
int randomNumber = 89898980;
// 创建一个新的数组(比已缺失的数组多一个长度)
int[] newArrays = new int[arrays.length + 1];
// 填充特殊的数字进新数组中
for (int i = 0; i < newArrays.length; i++) {
// 随意填充数组到新数组中
newArrays[i] = randomNumber;
}
// 遍历题目的数组并使用index替代掉新数组的元素
for (int i = 0; i < arrays.length; i++) {
// 题目数组的值[0,1,2,3,4,...n]其中有一个缺失
int index = arrays[i];
// 重新填充到新数组上,index对应着题目数组的值
newArrays[index] = 3333333;
}
// 遍历新数组,只要还有值为89898980,那么那个就是缺失的数字
for (int i = 0; i < newArrays.length; i++) {
if (newArrays[i] == randomNumber) {
System.out.println("关注公众号:Java3y---->缺失的数字是:" + i);
}
}
}
/**
* 利用等差公式找到缺失的数字
*
* @param arrays
*/
public static void missingNumber2(int[] arrays) {
// 套用等差求和公式
int sum = (arrays[0] + arrays[arrays.length - 1]) * (arrays.length + 1) / 2;
// 遍历数组,得出的sum减去数组每一位元素,最后即是缺失的数字
for (int value : arrays) {
sum = sum - value;
}
System.out.println("关注公众号:Java3y---->缺失的数字是:" + sum);
}
/**
* 删除下标为k的元素
*/
public static void deleteK() {
//固定的常量(比数组元素的个数要大)
int N = 10;
int[] arrays = new int[N];
//对数组进行初始化
for (int i = 0; i < 8; i++) {
arrays[i] = i;
}
//要删除下标
int k = 7;
for (int i = k; i < N - 1; i++) {
arrays[i] = arrays[i + 1];
}
System.out.println("公众号:Java3y" + arrays);
}
/**
* 找出常用的数字:
* 给你一个长度为n的数组,其中有一个数字出现的次数至少为n/2,找出这个数字
*/
public static void findMajorityElement(int[] arrays) {
//构建一个静态栈
int[] stack = new int[arrays.length];
// 栈的front指针
int front = -1;
// 遍历给出的数组
for (int i = 0; i < arrays.length; i++) {
// 判断该栈为空,那么直接将元素入栈
if (front == -1) {
stack[++front] = arrays[i];
} else if (stack[front] == arrays[i]) { // 该元素是否与栈的元素一致-->继续入栈
stack[++front] = arrays[i];
} else {
// 只要不一致,就出栈
front--;
}
}
// 只要该数字出现次数大于数组长度的2/1,那么留下来的数字肯定在栈顶中
System.out.println("关注公众号:Java3y--->" + stack[0]);
}
public static void findMajorityElement2(int[] arrays) {
// 装载栈的元素
int candidate = -1;
// 栈的大小(长度)
int count = 0;
// 遍历给出的数组
for (int i = 0; i < arrays.length; i++) {
// 判断该栈为空,那么直接将元素入栈
if (count == 0) {
candidate = arrays[i];
count++;
} else if (candidate == arrays[i]) { // 该元素是否与栈的元素一致-->入栈(栈多一个元素)
count++;
} else {
// 只要不一致-->出栈(栈少一个元素)
count--;
}
}
// 只要该数字出现次数大于数组长度的2/1,那么留下来的数字肯定在栈顶中
System.out.println("关注公众号:Java3y--->" + candidate);
}
}