提交六次的代码——LeetCode学习笔记1

嘿!这里是正在学习编程的CH。

最近在网上搜索有关LeetCode的题目解答,发现解答并不多,于是也来做一个。这是第一篇文章,希望大家喜欢!

初来乍到,请多关照!


第一次听说LeetCode是在b站,当时为了找一些题目巩固自己的基础找练习的网站,于是找到了这个几乎是程序员必知的网站——

力扣​leetcode-cn.com/?utm_source=LCUS&utm_medium=ip_redirect&utm_campaign=transfer2china

在上面就开始练习,就开始从第一题做起。


以下进入正题:题目要求是这样的:

提交六次的代码——LeetCode学习笔记1_第1张图片

第一次是一次错误,就不给大家展示了。

第二次我想了想,要输出一个一维数组就要先设定一个

int[] nums2 = new int[2];

然后要做到从nums里面找到两个数nums[i]和nums[j]使得加起来正好是target,那么就套两层循环并最后在nums2当中填进去符合条件的i,j并最后输出nums2.所以第二次的总体代码如下:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] nums2 = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    nums2[0] = i;
                    nums2[1] = j;
                }
            }
        }
        return nums2;
    }
}

但是最后令我没有想到的是:这两个要输出的数反了!

于是再改第三版:

这次把i,j调换一下:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] nums2 = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if ((nums[i] + nums[j] == target) && (i != j)) {
                    nums2[0] = j;
                    nums2[1] = i;
                }
            }
        }
        return nums2;
    }
}

这次顺利通过!


但是仔细一看,

7%?

改!

第四版:看见了官方标答,里面有用到HashMap,于是直接上:(利用HashMap的快速查询的特性)

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] nums2 = new int[2];
        Map map1 = new HashMap();
        for(int i = 0; i < nums.length; i++) {
            if(map1.containsKey(target - nums[i])){
                nums2[0] = map1.get(target - nums[i]);
                nums2[1] = i;
            }
            map1.put(nums[i],i);
        }
        return nums2;
    }
}

这次发现果然,运行时间小了很多。看来就是for的问题。


但是再仔细一看:

47%?

再改!

(严于律己,逃)第五版没啥好说的,也是一次错误。

第六版:彻底放弃for,利用while再次将时间缩短:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map map1 = new HashMap();
        int j = 0;
        while (j < nums.length) {
            if(map1.containsKey(target - nums[j])) {
               return new int[]{map1.get(target - nums[j]) , j};
            }
            map1.put(nums[j] , j++);
        }
        return new int[0];
    }
}

再一看:

终于!这上了优秀线才舒服。


附上六次提交的过程。最后一版是我改到的最好的版本。

提交六次的代码——LeetCode学习笔记1_第2张图片

再附一个LeetCode网站上0ms(100%)的:

import java.util.HashMap;

class Solution {
    public int[] twoSum(int[] nums, int target) {

        HashMap map = new HashMap<>();
        int left = 0;
        int right = nums.length - 1;

        while (left <= right) {
            if (map.containsKey(target - nums[left])) {
                return new int[]{map.get(target - nums[left]), left};
            }
            map.put(nums[left], left++);
            if (map.containsKey(target - nums[right])) {
                return new int[]{map.get(target - nums[right]), right};
            }
            map.put(nums[right], right--);
        }

        return new int[0];
    }
}

学习编程ing,想一起的小伙伴私信哦~感谢各位!

你可能感兴趣的:(java)