LeetCode 1.Two Sum

1.39 ms 暴力

    public class Solution {

        public int[] twoSum(int[] nums, int target) {
            int first = 0;
            int second = 0;

            for (first = 0; first < nums.length; first++) {
                for (second = first + 1; second < nums.length; second++) {
                    if (nums[first] + nums[second] == target) {
                        return new int[] { first, second };
                    }
                }
            }
            return new int[] { first, second };
        }
    }

2.62 ms 暴力

    public class Solution {


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

            int second = 0;
            List<Integer> visited = new ArrayList<Integer>();
            for (; second < nums.length; second++){
                int num = target - nums[second];
                if (visited.contains(num)){
                    return new int[]{visited.indexOf(num), second};
                }else{
                    visited.add(target - num);
                }

            }
            return null;
        }
    }

3.12ms 排序+二分查找

    public class Solution {

        private int findBybsearch(int start, int num, List<NumCell> numCells) {

            int l = start;
            int r = numCells.size();
            while (l < r) {

                int m = (l + r) >> 1;
                NumCell numCell = numCells.get(m);
                if (num > numCell.value) {
                    l = m + 1;
                }
                if (num == numCell.value) {
                    return numCell.index;
                }
                if (num < numCell.value){
                    r = m;
                }
            }

            return -1;
        }

        private class NumCell implements Comparable<NumCell> {

            public NumCell(int v, int i) {
                this.value = v;
                this.index = i;
            }

            public int value;
            public int index;

            @Override
            public int compareTo(NumCell o) {
                return this.value - o.value;
            }
        }

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

            List<NumCell> numCells = new ArrayList<NumCell>();

            for (int i = 0; i < nums.length; i++) {
                numCells.add(new NumCell(nums[i], i));
            }

            Collections.sort(numCells);

            for (int i = 0; i < numCells.size(); i++) {
                int num = target - numCells.get(i).value;
                int index0 = numCells.get(i).index;
                int index1 = findBybsearch(i, num, numCells);

                if (-1 != index1) {
                    return new int[] { Math.min(index0, index1), Math.max(index0, index1) };
                }
            }

            return null;
        }
    }

4.8ms Java的Map

    public class Solution {

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

            Map<Integer, Integer> NumMap = new HashMap<>();

            for (int i = 0; i < nums.length; i++) {
                if (NumMap.containsKey(target - nums[i])) {
                    return new int[] { NumMap.get(target - nums[i]), i };
                } else {
                    NumMap.put(nums[i], i);
                }
            }
            return null;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值