Leecode Two Sum(Java版)

/**
 * @author WF帆少
 * @微信 13025261795
 */
class Solution {
	
    public int[] twoSum(int[] nums, int target) {

        // 声明一个变量来存放nums.length可提高效率
    	int numsLength = nums.length;

    	int[] indicesTemp = new int[numsLength];

        // 符合target的索引数量
    	int count = 0;

        // 暴力搜索符合target的索引
    	for (int i = 0; i < numsLength - 1; i++) {
    		for (int j = i + 1; j < numsLength; j++) {
    			if (nums[i] + nums[j] == target) {
    				indicesTemp[count++] = i;
    				indicesTemp[count++] = j;
    			}
    		}
    	}
        
        // 数组indicesTemp去0操作
    	int[] indices = new int[count];

        // 深拷贝
    	for (int i = 0; i < count; i++) {
    		indices[i] = indicesTemp[i];
    	}

    	return indices;
    }
		
}

 

你可能感兴趣的:(Leecode)