LeetCode刷题记

初衷

为了让暑假这段时间不荒废掉,我决定做点正事之后再(7月末)出去毕业旅行

  • 刷LeetCode上的题目,总共不超过250到题目,先完成50道,顺便捡起来数据结构和算法的内容
  • 用python把脑卒中的项目再写一遍,有可能写不完,写完爬虫的部分就行了
  • 了解下git R 想想到底该不该看看node.js STL等等这些鬼东西

LeetCode 刷题记

学长强烈推荐,那就做一下,有好多前辈做过这些工作,我只能模仿抄袭了看来。不过有时间,重在坚持^^
1.具体对题目的discuss LeetCode上有
2.主要是java,天,AppStore上居然有题解。

LeetCode 1 Two Sum

原题

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

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

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

中文翻译

给出一个整型数组,找到两个数,他们的和等于目标数。
函数 twoSum 返回这两个数的下标,第一个数的下标要比第二个数的下标小,注意:下标不是零开始的
可以假定每个输入只有一个确定的方案

分析


先给出一个用手想的方案

public class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int a,index1,index2;
        int[] ts = new int[2];
        for(int i=0;i<numbers.length;i++){
            if(numbers[i]<target){
                a = target-numbers[i];
                for(int j=0;j<numbers.length;j++){
                    if(numbers[j] == a){
                        index1 = (j>i)?i:j;
                        index2 = (j>i)?j:i;
                        ts[0] = index1;
                        ts[1] = index2;
                    }
                }
            }
        }
        return ts;
    }
}

我接触了很少的算法竞赛的思想影响,这个项目那个作业,根本不考虑什么时间复杂度。所以第一次提交就超时了,原来这种OJ的测试数据量很大啊…复杂度是 O(n2)
这个题目的缺点,可以假定有且只有一个解决方案,那么其实数和下标是互相对应的,一一对应,要降低时间开销,想到map的键值存储进行查找


Hash方法
对hash的原理等等研究的很浅,现在知道其然不知道其所以然,以后再解释hash….
前辈的map都是C++写的,java的map不是键值一一对应的,如果要根据值V来查找键K需要遍历,这样复杂度又回去了,网上还有导入Apache Commons Collections 等BidiMap‘新奇’map实现根据V来找K,前提是像这个题目一样键值一一对应。没有必要啊,还得导入包..
索性就C++做法了

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> mapping;
        vector<int> result;
        for(int i=0;i<nums.size();i++){
            mapping[nums[i]]=i;
        }
        for(int i=0;i<nums.size();i++){
            int gap = target-nums[i];
            //map的find方法没有找到返回end迭代器,题目要求第二个下标比第一个大
            if(mapping.find(gap)!=mapping.end()&&mapping[gap]>i){
            //push_back在容器后加值
                result.push_back(i+1);
                result.push_back(mapping[gap]+1);
                break;
            }
        }
        return result;
    }
};

AC之后,得到官方solution

O(n2) runtime, O(1) space – Brute force:

The brute force approach is simple. Loop through each element x and find if there is another value that equals to target – x. As finding another value requires looping through the rest of array, its runtime complexity is O(n2) .

O(n) runtime, O(n) space – Hash table:

We could reduce the runtime complexity of looking up a value to O(1) using a hash map that maps a value to its index.

看到就是如此,这道题标签就是hash


网上还有一种做法,就是将数据排序后,两边逼近得到最后值
大概就是这个意思:

数据:2,4,6,8,11
目标数:14
2+11<14 左移
4+11>14 右移
4+8<14 左移
6+8=14 OK

Java实现:将数据值和下标一一存储起来,实现Comparable接口,重写compareTo方法,compareTo里比较的是什么,sort就按照什么从小到大进行比较。

public class Solution {
    public static class Node implements Comparable<Node>{
        int value,index;
        public Node(int v,int i){
            value=v;
            index=i;
        }
        public int compareTo(Node n){
            return this.value-n.value;
        }
    }
    public int[] twoSum(int[] nums, int target) {
        Node[] nodes = new Node[nums.length];
        for(int i;i<nums.length;i++){
            nodes[i] = new Node(numbers[i],i+1);
        }
        //sort以value进行排序
        Arrays.sort(nodes);
        int i = 0;
        int j = nodes.length;
        int[] result=new int[2];
        while(i<j){
            if(nodes[i].value+nodes[j].value<target){

            }
            else if(nodes[i].value+nodes[j].value>target){

            }
            else (nodes[i]+nodes[j]=target){
                break;
            }
        }
        //获得两个数的下标
        result[0] = nodes[i].index;
        result[1] = nodes[j].index;
        Array.sort(result)
        return result;
    }

}

这篇文章写的很简单,太琐碎…不过,就算是开始

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值