Skip to content

Commit 33ab46f

Browse files
牛客网剑指offer
1 parent dbe806f commit 33ab46f

File tree

272 files changed

+5482
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

272 files changed

+5482
-0
lines changed

jianzhioffer/src/jz_offer/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### IDEA ###
2+
.idea/
3+
*.iml
4+
target/
5+
*/target/
6+
*/*.iml
7+
*/.gradle/
8+
*/out/
9+
out/
10+
### Java ###
11+
.settings/
12+
.classpath
13+
.project
14+
bin/
15+
build/

jianzhioffer/src/jz_offer/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 David
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

jianzhioffer/src/jz_offer/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 牛客网剑指Offer题解
2+
3+
#### 介绍
4+
牛客网剑指Offer题解(java实现)
5+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package jz_offer.all.JZ1;
2+
3+
public class Solution {
4+
//暴力法
5+
/*public boolean Find(int target, int [][] array) {
6+
if(array.length==0)
7+
return false;
8+
for(int i=0;i<array.length;i++){
9+
for(int j=0;j<array[0].length;j++){
10+
if(array[i][j]==target)
11+
return true;
12+
}
13+
}
14+
return false;
15+
}*/
16+
17+
//时间复杂度相比暴力法较低
18+
public boolean Find(int target, int[][] array) {
19+
int row = array.length;
20+
if (row == 0)
21+
return false;
22+
int col = array[0].length;
23+
if (col == 0)
24+
return false;
25+
int rows = row - 1;
26+
int cols = 0;
27+
while (rows >= 0 && cols < col) {
28+
if (target < array[rows][cols])
29+
rows--;
30+
else if (target > array[rows][cols])
31+
cols++;
32+
else
33+
return true;
34+
}
35+
return false;
36+
}
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
题目描述:
2+
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,
3+
每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,
4+
判断数组中是否含有该整数。
5+
6+
输入:7,[[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]] 返回:true
7+
8+
思路:
9+
1、时间复杂度:算法中基本操作的执行次数
10+
2、一种暴力求解
11+
3、另一种由于行从左到右递增,列从上到下递增,可以用这个性质。这样时间复杂度就变成行+列的次数,
12+
时间复杂度大大减小
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package jz_offer.all.JZ10;
2+
3+
public class Solution {
4+
public int RectCover(int target) {
5+
if (target == 1 || target == 2)
6+
return target;
7+
int one = 1;
8+
int two = 2;
9+
int result = 0;
10+
for (int i = 2; i < target; i++) {
11+
result = one + two;
12+
one = two;
13+
two = result;
14+
}
15+
return result;
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
题目描述:
2+
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。
3+
请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
4+
5+
比如n=3时,2*3的矩形块有3种覆盖方法:
6+
7+
思路:
8+
1、找规律,可以用递归或者递推,一般使用递推。因为递归的时间复杂度比较高O(2^n)
9+
2、写出n=1到5时的情况,就可以找出规律
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package jz_offer.all.JZ11;
2+
3+
public class Solution {
4+
public int NumberOf1(int n) {
5+
int count = 0;
6+
if (n == 0)
7+
return 0;
8+
while (n != 0) {
9+
if (n < 0)
10+
++count;
11+
n = (n << 1);
12+
}
13+
return count;
14+
}
15+
16+
//另一种方法,返回整数的二进制数,然后进行判断
17+
//数值用==比较,字符串用equals比较,字符char用=='1'
18+
/*public int NumberOf1(int n) {
19+
String str=Integer.toBinaryString(n);
20+
int count=0;
21+
for(int i=0;i<str.length();i++){
22+
if(str.charAt(i)=='1')
23+
++count;
24+
}
25+
return count;
26+
}*/
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
题目描述:输入一个整数,输出该数32位二进制表示中1的个数。其中负数用补码表示。
2+
3+
思路:
4+
1、正数的原码、反码、补码是一样的,负数则有区别
5+
计算机的整数计算用反码表示,因为反码可以表示加减,范围则是-128-127
6+
采用反码计算,可以轻松得到答案
7+
符号位为0,为正;符号位为1,为负
8+
负数的反码就是原码取反,补码就是反码加1
9+
原码首位为0是正数,首位为1是负数,因此这道题可以通过左移判断
10+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package jz_offer.all.JZ12;
2+
3+
public class Solution {
4+
public double Power1(double base, int exponent) {
5+
return Math.pow(base, exponent);
6+
}
7+
8+
public double Power2(double base, int exponent) {
9+
double pow = 1.00;
10+
if (base == 0 && exponent != 0)
11+
return 0.00;
12+
if (exponent == 0 && base != 0)
13+
return 1.00;
14+
if (exponent > 0) {
15+
for (int i = 0; i < exponent; i++)
16+
pow *= base;
17+
} else if (exponent < 0) {
18+
for (int i = 0; i < Math.abs(exponent); i++) {
19+
pow /= base;
20+
}
21+
}
22+
return pow;
23+
}
24+
}

0 commit comments

Comments
 (0)