Leetcode-Java (四)

最近两个周在忙项目,所以一直没有刷题。今天接着来,走起。。。。

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);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值