forked from kexun/jianzhioffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day07.java
210 lines (158 loc) · 3.38 KB
/
Day07.java
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
package com.offer;
public class Day07 {
public static void main(String[] args) {
Day07 d = new Day07();
// double value = d.power(5, 5);
// System.out.println(value);
// d.print1ToMax(4);
// d.print1ToMax2(6);
int[] array = {
10,4, 2, 50, 6,2, 8, 0, 34
};
d.reorderOddEven(array);
}
/**
* 求一个数值的整数次方 通过递归的方式 实现类似于菲波拿捏数列
* @param base
* @param exp
* @return
*/
public double power(double base, int exp) {
if (exp == 0) {
return 1;
}
if (exp == 1) {
return base;
}
double value = 1.0;
value = power(base, exp >> 1);
value *= value;
if ((exp & 0x1) == 1) {
value *= base;
}
return value;
}
/**
* 给定一个整数n,从1打印到n位最大的数 如1 打印1-9
* @param n
*/
public void print1ToMax(int n) {
char[] array = new char[n];
for (int i = 0; i < n; i++) {
array[i] = '0';
}
while (increaseBy1(array)) {
printNum(array);
System.out.println();
}
}
/**
* 考虑到大数问题,用char数组模拟整数的增加,每次+1
* @param array
* @return
*/
public boolean increaseBy1(char[] array) {
int length = array.length;
int takeOver = 0;
for (int i = length-1; i >= 0; i--) {
int sum = array[i] - '0' + takeOver;
if (i == length - 1) {
sum++;
}
if (sum == 10) {
if (i == 0) {
return false;
} else {
sum = 0;
takeOver = 1;
array[i] = '0';
}
} else {
array[i] = (char) ('0' + sum);
break;
}
}
return true;
}
/**
* 递归打印数字,并且数字不能以0开头
* @param array
*/
public void printNum(char[] array) {
int index = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == '0') {
index++;
} else {
break;
}
}
for (int i = index; i< array.length; i++) {
System.out.print(array[i]);
}
}
/**
* 给定一个整数n,从1打印到n位最大的数 如1 打印1-9 这里通过递归的方式
* @param n
*/
public void print1ToMax2(int n) {
if (n <= 0)
return;
char[] array = new char[n];
for (int i = 0; i < n; i++) {
array[i] = '0';
}
for (int i = 0; i < 10; i++) {
array[0] = (char) ('0' + i);
printRecursively(array, n, 0);
}
}
/**
* 递归调用, 其实就是对n位的数组做全排列 共n的n次方-1 种可能
* @param array
* @param length
* @param index
*/
public void printRecursively(char[] array, int length, int index) {
if (index == length - 1) {
printNum(array);
System.out.println();
return;
}
for (int i = 0; i < 10; i++) {
array[index + 1] = (char) ('0' + i);
printRecursively(array, length, index + 1);
}
}
/**
* 奇偶互换
* @param array
*/
public void reorderOddEven(int[] array) {
if (array.length <= 1) {
return;
}
int length = array.length;
int first = 0;
int second = length - 1;
int firstValue = 0;
int secondValue = 0;
while (first < second) {
firstValue = array[first];
secondValue = array[second];
if ((firstValue & 1) == 0 && (secondValue & 1) == 1) {
array[first++] = secondValue;
array[second--] = firstValue;
}
if ((firstValue & 1) == 1) {
first++;
}
if ((secondValue & 1) == 0) {
second--;
}
}
for (int a : array) {
System.out.print(a);
}
}
}