LeetCode1

作者: beardnick | 来源:发表于2018-08-02 09:48 被阅读0次

用第一题熟悉一下环境

题目很简单,数组中找出两个数,使其和等于给出的target

思路

直接暴力求解,双重for循环

时间复杂度分析

n - 1 + n - 2 + n - 3 + ... + 1 = (n - 1 + 1) * (n - 1) / 2 = O(n^2)

目测可行

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

                for (int i = 0; i < nums.length - 1; i ++) {

                    for (int j = i + 1; j < nums.length; j ++) {

                if(nums[i] + nums[j] == target){

                    return new int[]{i , j};

                }

            }

        }

        return null;

    }

相关文章

  • LeetCode1

    用第一题熟悉一下环境 题目很简单,数组中找出两个数,使其和等于给出的target 思路 直接暴力求解,双重for循...

  • leetcode1

    按难度排序,每天刷点leetcode题,抄点解法,大部分解答是在leetcode的dicuss中找到的,没有一一引...

  • Leetcode1

    题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,...

  • leetcode1

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 给定 nums = [2, 7, 11, 15], ...

  • LeetCode1: Single Number

    1、一个非空整数数组,除了一个元素只出现一次,其余元素都出现了两次。找出这个一单独出现的元素。note: 线性的时...

  • leetcode1 two sum

    问题:Given an array of integers, returnindicesof the two nu...

  • LeetCode1:Two Sum

    注:最精华的思想是:x = nums[i] dict[x] = i取出第i个数字(以0为开头),把它的值装载...

  • leetcode1 Two Sum

    题目 https://leetcode.com/problems/two-sum/ 思路 盲猜N^2的遍历会超时,...

  • Leetcode1:python实现

    给定一个整数数组 nums和一个整数目标值 target,请你在该数组中找出 和为目标值 的那两个整数,并返回它们...

  • Leetcode1——两数之和

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。你可以假设每个输入只对应一种答案,且同样的元素不能被重...

网友评论

      本文标题:LeetCode1

      本文链接:https://www.haomeiwen.com/subject/etcjvftx.html