最近两个周在忙项目,所以一直没有刷题。今天接着来,走起。。。。
Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
public static int[] plusOne(int[] digits) {
int[] digits_new = new int[digits.length + 1];
int c = 1;
int i = digits.length - 1;
for (; i >= 0; i--) {
if ((digits[i] + c) > 9) {
digits_new[i + 1] = 0;
digits[i] = 0;
} else {
digits_new[i + 1] = c + digits[i];
digits[i] = c + digits[i];
c = 0;
}
}
if (c == 1) {
digits_new[0] = 1;
return digits_new;
} else {
return digits;
}
}
public static void plusOneTest() {
int[] digits = {0};
int[] digitis_new = plusOne(digits);
System.out.println(Arrays.toString(digitis_new));
}
Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
public static int climbStairs(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
int step_count = 1; //n个1
int two_max_count = n / 2;
for (int i = 1; i <= two_max_count; i++) {
step_count += calcSteps(n - i, i);
}
return step_count;
}
//解法二
public static int climbStairs1(int n) {
if (n == 1 || n == 2) {
return n;
}
return climbStairs1(n - 1) + climbStairs1(n - 2);
}
public static int calcSteps(int m, int n) {
BigInteger steps = BigInteger.valueOf(m);
BigInteger j = BigInteger.valueOf(n);
for (int i = 1; i < n; i++) {
steps = steps.multiply(BigInteger.valueOf(--m));
j = j.multiply(BigInteger.valueOf(n - i));
}
steps = steps.divide(j);
return steps.intValue();
}
public static void climbStairsTest() {
int n = 44;
int steps = climbStairs(n);
System.out.println("steps : " + steps);
steps = climbStairs1(n);
System.out.println("steps : " + steps);
}
Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
Note:
For a given n, a gray code sequence is not uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.
格雷码数学公式: 整数n的格雷码为 n^(n/2)
public static List<Integer> grayCode(int n) {
List<Integer> grayCode = new ArrayList<Integer>();
int value = 1;
for (int i = 0; i < n; i++) {
value *= 2;
}
for (int i = 0; i < value; i++) {
grayCode.add(i ^ (i / 2));
}
return grayCode;
}
public static void grayCodeTest() {
int n = 2;
List<Integer> list = grayCode(n);
System.out.println(list);
}
Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
click to show follow up.
Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
public static void setZeroes(int[][] matrix) {
int[] row = new int[matrix.length];
int[] col = new int[matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
row[i] = 1;
col[j] = 1;
}
}
}
//清除行
for (int i = 0; i < matrix.length; i++) {
if (row[i] == 1) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 0;
}
}
}
//清除列
for (int j = 0; j < matrix[0].length; j++) {
if (col[j] == 1) {
for (int i = 0; i < matrix.length; i++) {
matrix[i][j] = 0;
}
}
}
}
public static void setZeroesTest() {
int[][] matrix = {
{0, 0, 5, 8, 3, 8},
{9, 4, 1, 9, 9, 5},
{0, 4, 3, 0, 2, 7},
{1, 6, 0, 0, 3, 0},
{4, 4, 0, 3, 3, 7},
{0, 3, 7, 5, 1, 0}};
setZeroes(matrix);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j]);
}
System.out.println();
}
}
Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
public static int canCompleteCircuit(int[] gas, int[] cost) {
int total=0,tank=0,index=0;
for (int i = 0;i<gas.length;i++) {
tank += gas[i] - cost[i];
if (tank < 0){
index = i+1;
tank =0;
}
total +=gas[i] - cost[i];
}
return (total<0)?-1:index;
}
//这种解法时间发杂都有点大
//public static int canCompleteCircuit(int[] gas, int[] cost) {
// int remainingPetrol = 0;
// int N = gas.length;
// int startPos = -1;
// for (int i = 0; i < N; i++) {
// startPos = i;
// for (int m = i; m < N; m++) {
// remainingPetrol += gas[m] - cost[m];
// if (remainingPetrol < 0) {
// startPos = -1;
// break;
// }
// }
// for (int n = 0; n < i; n++) {
// remainingPetrol += gas[n] - cost[n];
// if (remainingPetrol < 0) {
// startPos = -1;
// break;
// }
// }
// if (startPos != -1) {
// break;
// }
// remainingPetrol=0;
// }
// return startPos;
//}
public static void canCompleteCircuitTest(){
int[] gas = {2,3,1};
int[] cost = {3,1,2};
int startPos = canCompleteCircuit(gas,cost);
System.out.println("开始位置:"+startPos);
}