Here is my java solution not using hashmap


  • 0
    E
    public int[] twoSum(int[] nums, int target){
        int[] result = new int[2];
        int[] temp = nums.clone();
    
        //quicksort
        Arrays.sort(temp);
    
        //get low high value whose sum == target
        int low = -1;
        int high = 0;
        while (low < temp.length - 1){
            low += 1;
            high = low + 1;
            while(high < temp.length){
                if (temp[low] + temp[high] < target)
                    high += 1;
                else
                    break;
            }
            if (high < temp.length && temp[low] + temp[high] == target)
                break;
        }
    
        //get index from origin array
        result[0] = find_index(temp[low], -1, nums);
        result[1] = find_index(temp[high], result[0], nums);
    
        if (result[0] > result[1]){
            int tt = result[0];
            result[0] = result[1];
            result[1] = tt;
        }
        return result;
    }
    
    public int find_index(int value, int pre, int[] numbers){
        for (int i = 0; i < numbers.length;i ++)
            if (value == numbers[i] && numbers[i] != pre - 1)
                return i + 1;
        return 0;
    }

  • 2
    F
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            int[] temp = new int[2];
            int[] nums_sort = nums.clone();
            Arrays.sort(nums_sort);
            int start = 0;
            int end = nums_sort.length - 1;
            while(start < end){
                if((nums_sort[start] + nums_sort[end]) == target){
                    temp[0] = getIndex(nums_sort[start],-1,nums);
                    temp[1] = getIndex(nums_sort[end],temp[0]-1,nums);
                    if(temp[0] > temp[1]){
                        int a = temp[1];
                        temp[1] = temp[0];
                        temp[0] = a;
                    }
                    break;
                }
                else if((nums_sort[start] + nums_sort[end]) < target){
                    start += 1;
                }
                else if((nums_sort[start] + nums_sort[end]) > target){
                    end -= 1;
                }
            }
            if(start >= end){
                temp = null;
            }
            return temp;
        }
        
       public int getIndex(int temp, int pre, int[] nums){
            for(int i=0;i<nums.length;i++){
                if(temp == nums[i] && i != pre)
                return i+1;
            }
            return 0;
    	}
    }

  • 0
    E

    cool, you made the code more concise.


  • 0
    S


  • 0
    S
    public int[] findIndexes(int[] values, int[] nums) {
    	int[] indexes = { 0, 0 };
    	int j = 0;
    	for (int i = 0; i < nums.length; i++) {
    		if (values[j] == nums[i]) {
    			indexes[j] = i;
    			j++;
    		}
    		if (j == 2) {
    			break;
    		}
    	}
    	return indexes;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.