Java编程题收集
Xplorist Lv6

Java编程题收集

reference-site-list

steps

生成1-9的随机不重复数组

  • 这个题是我自己编的,在以前写数独的时候经常写这种1到9的不重复随机数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static int[] getRandomArray() {
int[] arr = new int[9];
int[] temp = new int[9];
// 初始化临时数组
for (int i = 0; i < temp.length; i++) {
temp[i] = i + 1;
}
for (int i = 0; i < arr.length; i++) {
int random = new Random().nextInt(temp.length);
if (temp[random] == 0) {
i--;
continue;
}
arr[i] = temp[random];
temp[random] = 0;
}
return arr;
}

解释:

  • 难点:在于实现不重复,使用两个数组,临时数组是为了生成1到9的数字,然后生成随机数,将随机数存入结果数组,然后将临时数组上该位置的数变成0,下次取到临时数组中的0就代表这个随机数已经存在了,重新生成随机数,这样就实现了不重复。
  • 缺点: 这样实现不重复的效果,效率不高,只是现在CUP的速度太快了,感觉不出来很慢。

两个数交换位置

  • 很多地方都会用到,记得大学那会儿的教数据结构的美女老师教的空间换时间,时间换空间

  • 最常见的空间换时间

1
2
3
4
5
6
int a = 3;
int b = 2;

int temp = a;
a = b;
b = temp;
  • 时间换空间
1
2
3
4
5
6
int a = 3;
int b = 2;

a = a + b;
b = a - b;
a = a - b;

解释:用一个变量缓存两个数的和,然后用和减去原来的一个数b得到了a再赋值给b,只要将结果算出来再看代码,其实也很简单。

  • 利用异或
1
2
3
4
5
6
int a = 3;
int b = 2;

a = a ^ b;
b = a ^ b;
a = a ^ b;

解释:异或的神奇特性,二进制数a,b; a = 11, b = 10, c = a ^ b = 01, c ^ a = 01 ^ 11 = 10 = b;看懂了没,异或的结果C再和之前的一个数a进行异或就会得到另外一个数b,注意:异或是二进制数的运算哦。

冒泡排序

  • 冒泡排序,大学时学C语言的时候第一个算法题,经典
1
2
3
4
5
6
7
8
9
10
11
public static void bubbleSort(int[] arr) {
for (int i = arr.length; i > 0; i--) {
for (int j = 0; j < i - 1; j++) {
if (arr[j] > arr[j + 1]) {
arr[j] = arr[j] + arr[j + 1];
arr[j + 1] = arr[j] - arr[j + 1];
arr[j] = arr[j] - arr[j + 1];
}
}
}
}

解释:

  • 假设是4, 1, 7, 9 这四个数,用冒泡排序,由小到大排序,相邻两个数比较,将最大的数移动到右边,模型想清楚了,然后就知道该怎么写代码了。
  • 第一次比较是从第一个数字开始,和它后面的一个数比较,然后第2个数和它后面的第3个数比较,只掉第n-1个数和第n个数比较。这就是内部循环要做的事,
  • 找出一个最大数算一次大循环,然后就知道外部循环要怎么写,次数是递减的。
  • 总之就是外部循环控制内部循环的最大循环次数是递减的,内部循环从0递增。

选择排序

  • 选择排序,我做冒泡排序时候自己就想出来过这种更直接简单的排序,个人觉得比冒泡直观,反而觉得冒泡比较绕
1
2
3
4
5
6
7
8
9
10
11
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
}
}
}
  • 选择排序就是将第一个数依次后他后面的其他数进行比较,然后把最小的数放在第一位,这样就找到了第一个最小的数,然后就从第二个数开始依次后它后面的其他数比较,这种思考明显比冒泡那种每次都进行相邻比较要简单清晰
  • 外层循环的变量就是控制当前是第几个数和它后面的数在进行比较,里面的数就是外面的数的后面一个递增,模型简单代码看着也简单。

递归实现1 + 2 + 3 + … + 100

  • 递增相加是递归最原始的题目,然后是求阶乘,然后是斐波拉契数列,兔子问题,走楼梯,汉诺塔
1
2
3
4
5
6
public static int recurse(int num) {
if (num <= 1) {
return 1;
}
return num + recurse(num - 1);
}
  • 递归,就是两个点,最底层的一个停止标志,然后就是变化的规则,先只想它和它挨着的一种情况,然后就套进来,思路就清晰了,递归就像是俄罗斯套娃。

生成100个1到100的随机数,去重后并进行从小到大的排序

  • 去重问题
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
public static void deduplicateAndSort() {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
int random = new Random().nextInt(100) + 1;
list.add(random);
}
System.out.println("randomList: " + Arrays.toString(list.toArray()));
// 不使用set进行去重,新创建一个list,新list中只保留和旧list对比后,新list中不存在的数
List<Integer> newList = new ArrayList<>();
for (Integer item : list) {
if (!newList.contains(item)) {
newList.add(item);
}
}
// 使用sort去排序
/*
newList.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
*/
// 不使用sort排序
for (int i = 0; i < newList.size() - 1; i++) {
for (int j = i + 1; j < newList.size(); j++) {
int valI = newList.get(i);
int valJ = newList.get(j);
if (valI > valJ) {
newList.set(i, valJ);
newList.set(j, valI);
}
}
}
System.out.println("result: " + Arrays.toString(newList.toArray()));
}

去重,核心在于比较,

  • 方案1:不添加另外的集合来存储的话,就是类似选择排序一样,挨着和后面的数进行比较,找到和当前的参照数相同的数,把后那个数去掉,然后集合长度变化,注意循环变量此时要自减1。
  • 方案2:添加另外一个集合来存储不重复的数,遍历原集合,每个数判断一下是否在新集合中,如果在就不存,就保证了新集合中的数没有重复,就是原集合去重后的结果了。
    方案1是不添加新的内存空间,省空间。方案2是添加空间。有点类似空间换时间,时间换空间那味了。
 评论
// variables $gt-color-main := #6190e8 $gt-color-sub := #a1a1a1 $gt-color-loader := #999999 $gt-color-error := #ff3860 $gt-color-hr := #e9e9e9 $gt-color-comment-txt := #333333 $gt-color-link-active := #333333 $gt-color-btn := #ffffff $gt-size-base := 16px // default font-size $gt-size-border-radius := 5px $gt-breakpoint-mobile := 479px $gt-mask-z-index := 9999 // functions & mixins clearfix() { &:before &:after { display table clear both content "" } } em($px, $base-size = $gt-size-base) { u = unit($px) if (u is 'px') { unit($px / $base-size, 'em') } else { unit($px, u) } } mobile() { @media (max-width $gt-breakpoint-mobile) { {block} } } // variables - calculated $gt-size-loader-dot := em(6px) $gt-size-loader := em(28px) $gt-size-avatar := em(50px) $gt-size-avatar-mobi := em(32px) // styles // Put everything under container to avoid style conflicts .comments-container { .gt-container { box-sizing border-box * { box-sizing border-box } font-size $gt-size-base // common a { color $gt-color-main &:hover { color lighten($gt-color-main, 20%) border-color lighten($gt-color-main, 20%) } &.is--active { color $gt-color-link-active cursor default !important &:hover { color $gt-color-link-active } } } .hide { display none !important } // icons .gt-svg { display inline-block width em(16px) height em(16px) vertical-align sub svg { width 100% height 100% fill $gt-color-main } } .gt-ico { display inline-block &-text { margin-left em(5px) } &-github { width 100% height 100% .gt-svg { width 100% height 100% } svg { fill inherit } } } // loader .gt-spinner { position relative &::before { position absolute top 3px box-sizing border-box width em(12px) height em(12px) margin-top em(-3px) margin-left em(-6px) border 1px solid $gt-color-btn border-top-color $gt-color-main border-radius 50% animation gt-kf-rotate 0.6s linear infinite content '' } } .gt-loader { position relative display inline-block width $gt-size-loader height $gt-size-loader font-style normal // font-size: $gt-size-loader line-height $gt-size-loader border 1px solid $gt-color-loader border-radius 50% animation ease gt-kf-rotate 1.5s infinite &:before { position absolute top 0 left 50% display block width $gt-size-loader-dot height $gt-size-loader-dot margin-top -($gt-size-loader-dot / 2) margin-left -($gt-size-loader-dot / 2) background-color $gt-color-loader border-radius 50% content '' } } // avatar .gt-avatar { display inline-block width $gt-size-avatar height $gt-size-avatar +mobile() { width $gt-size-avatar-mobi height $gt-size-avatar-mobi } img { width 100% height auto border-radius 3px } &-github { width $gt-size-avatar - em(2px) height $gt-size-avatar - em(2px) cursor pointer +mobile() { width $gt-size-avatar-mobi - em(2px) height $gt-size-avatar-mobi - em(2px) } } } // button .gt-btn { display inline-block padding em(12px) em(20px) color $gt-color-btn font-size em(12px) line-height 1 white-space nowrap text-decoration none background-color $gt-color-main border 1px solid $gt-color-main border-radius $gt-size-border-radius outline none cursor pointer &-text { font-weight 400 } &-loading { position relative display inline-block width em(12px) height em(16px) margin-left em(8px) vertical-align top } &.is--disable { cursor not-allowed opacity 0.5 } &-login { margin-right 0 } &-preview { color $gt-color-main background-color var(--background-color) &:hover { background-color var(--second-background-color) } } &-public { &:hover { background-color lighten($gt-color-main, 20%) border-color lighten($gt-color-main, 20%) } } } } &-loadmore // loadmore /* error */ .gt-error { margin em(10px) color $gt-color-error text-align center } // initing .gt-initing { padding em(20px) 0 text-align center &-text { margin em(10px) auto font-size 92% } } // no int .gt-no-init { padding em(20px) 0 text-align center } // link .gt-link { border-bottom 1px dotted $gt-color-main &-counts &-project { text-decoration none } } // meta .gt-meta { position relative z-index 10 margin em(20px) 0 padding em(16px) 0 font-size em(16px) border-bottom 1px solid $gt-color-hr clearfix() } .gt-counts { margin 0 em(10px) 0 0 color var(--default-text-color) } .gt-user { float right margin 0 font-size 92% &-pic { width 16px height 16px margin-right em(8px) vertical-align top } &-inner { display inline-block cursor pointer .gt-user-name { color var(--default-text-color) } } .gt-ico { margin 0 0 0 em(5px) svg { fill var(--default-text-color) } } .is--poping { .gt-ico { svg { fill $gt-color-main } } } } .gt-version { margin-left em(6px) color $gt-color-sub } .gt-copyright { margin 0 em(15px) em(8px) padding-top em(8px) border-top 1px solid var(--border-color) } // popup .gt-popup { position absolute top em(38px) right 0 display inline-block padding em(10px) 0 font-size em(14px) letter-spacing 0.5px background var(--background-color) border 1px solid var(--border-color) .gt-action { position relative display block margin em(8px) 0 padding 0 em(18px) text-decoration none cursor pointer &.is--active { &:before { position absolute top em(7px) left em(8px) width em(4px) height em(4px) background $gt-color-main content '' } } } } // header .gt-header { position relative display flex &-comment { flex 1 margin-left em(20px) +mobile() { margin-left em(14px) } } &-textarea { display block box-sizing border-box width 100% min-height em(82px) max-height em(240px) padding em(12px) color var(--default-text-color) font-size em(14px) word-wrap break-word background-color var(--fourth-text-color) border 1px solid var(--border-color) border-radius $gt-size-border-radius outline none transition all 0.25s ease resize vertical &:hover { background-color var(--background-color) } } &-preview { padding em(12px) background-color var(--background-color) border 1px solid var(--border-color) border-radius $gt-size-border-radius } &-controls { position relative margin em(12px) 0 0 clearfix() +mobile() { margin 0 } &-tip { color $gt-color-main font-size em(14px) text-decoration none vertical-align sub +mobile() { display none } } .gt-btn { float right margin-left em(20px) +mobile() { float none width 100% margin em(12px) 0 0 } } } } &:after { position fixed top 0 right 0 bottom 100% left 0 opacity 0 content '' } &.gt-input-focused { position relative &:after { position fixed top 0 right 0 bottom 0 left 0 z-index $gt-mask-z-index background #000 opacity 0.6 transition opacity 0.3s, bottom 0s content '' } .gt-header-comment { z-index $gt-mask-z-index + 1 } } // comments .gt-comments { padding-top em(20px) &-null { text-align center } &-controls { margin em(20px) 0 text-align center } } // comment .gt-comment { position relative display flex padding em(10px) 0 &-content { flex 1 margin-left em(20px) padding em(12px) em(16px) overflow auto background-color var(--second-background-color) transition all ease 0.25s +mobile() { margin-left em(14px) padding em(10px) em(12px) } } &-header { position relative margin-bottom em(8px) font-size em(14px) } &-block-1 { float right width em(32px) height em(22px) } &-block-2 { float right width em(64px) height em(22px) } &-username { color $gt-color-main font-weight 500 text-decoration none &:hover { text-decoration underline } } &-text { margin-left em(8px) color $gt-color-sub } &-date { margin-left em(8px) color $gt-color-sub } &-like &-edit &-reply { position absolute height em(22px) &:hover { cursor pointer } } &-like { top 0 right em(32px) } &-edit &-reply { top 0 right 0 } &-body { // color: $gt-color-comment-txt !important color var(--second-text-color) !important .email-hidden-toggle a { display inline-block height 12px padding 0 9px color #444d56 font-weight 600 font-size 12px line-height 6px text-decoration none vertical-align middle background #dfe2e5 border-radius 1px &:hover { background-color #c6cbd1 } } .email-hidden-reply { display none white-space pre-wrap .email-signature-reply { margin 15px 0 padding 0 15px color #586069 border-left 4px solid #dfe2e5 } } .email-hidden-reply.expanded { display block } } &-admin { .gt-comment-content { background-color var(--fourth-text-color) } } } @keyframes gt-kf-rotate { 0% { transform rotate(0) } 100% { transform rotate(360deg) } } }