LeetCode刷题系列(1)

突发奇想,感觉自己应该提升一下算法的能力,怎么办呢,来刷LeetCode吧!

Leet Code OJ 1. Two Sum

题目:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

翻译:

给定一个整形数组和一个整数target,返回2个元素的下标,它们满足相加的和为target。
你可以假定每个输入,都会恰好有一个满足条件的返回结果。

Java版代码1(时间复杂度O(n)):

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map map=new HashMap<>();
        for(int i=0;i

Java版代码2(时间复杂度O(n^2)):

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] result=new int[2];
        for(int i=0;i

Leet Code OJ 2. Add Two Numbers

题目:

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

给出两个表示两个非负整数的非空链表。数字以相反的顺序存储,它们的每个节点都包含一个数字。加上这两个数并返回一个链表。
你可以假设这两个数字不包含任何前导零,除了第0个数字本身。

你可能感兴趣的:(LeetCode刷题系列(1))