1. Two Sum php版+go

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, and you may not use the same element twice.

Example:

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

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

1.暴力破解,刚接触这到题的时候,我最容易想到的就是直接暴力求,双循环,算法复杂度n^2。


```php
class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[]
     */
    function twoSum($nums, $target) {
        for($i=0;$i<count($nums);$i++){
			for($j = $i+1;$j<count($nums);$j++){
				if($nums[$i]+$nums[$j]==$target){
					return [$i,$j];
				}
			}
		}
		return [];
    }
}

2.受桶排序的启发,将数组第一遍循环将数字转为另一个数组的下标,第二遍循环,查找[target-下标]值是否存在,其中考虑,target-v=k的问题。

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[]
     */
    function twoSum($nums, $target) {
        $has = [];
		foreach($nums as $k=>$v){
			$has[$v] = $k; 
		}
		foreach($nums as $k=>$v){
			if(isset($has[$target - $v]) && $target - $v!= $k){
				return [$k,$has[$target-$v]];
			}
		}
		return [];
    }
}

3.对桶排序的优化,可以在循环时即判断又写入,同时可以避免target - v = k问题。

php版本
class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[]
     */
    function twoSum($nums, $target) {
        $numss = array();
        foreach($nums as $k=>$v){
            if(isset($numss[$v])){
                return [$numss[$v],$k];
            }
            $numss[$target - $v] = $k;
        }
        return [];
    }
}
go版本
func twoSum(nums []int, target int) []int {
    numss := make(map[int]int,len(nums))
    for k,v := range nums{
        if c,ok := numss[v];ok{
            return []int{c,k}
        }
        numss[target - v] = k
    }
    return []int{}
}

第一次做这道题时,我最先考虑到的是暴力,第二次考虑到了之前的桶排序思路,最后一种是对桶排序的优化,这个数组是从小到大排列的有序数组,target - v 所生成的目标值一定是在当前v之后的,基于这种情况就可以边循环边写入数组里面,如果有存在值时输出即可,同时可以完全不考虑target - v = k情况,如果后面看到更优解再补充。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A random number of Rabbit images ranging from 1 to 10 are displayed for each operand and the user is expected to enter the values of the two operands and the result of adding the two operands, in the given text fields. When the user clicks on the button ‘Check!’, one of two things can happen: Case 1: all three input values are correct i) the text changes to ‘"Correct! Have another go?"’. ii) the number of Rabbit images displayed for each of the two operands changes. See Figure 2 for an example. iii) the three text fields are reset (i.e. they are made empty). 2/5 Case 2: at least one of the input values entered is incorrect i) the text changes to ‘Wrong! Try again!’. ii) the number of Rabbit images displayed does NOT change. iii) the text fields do NOT change.Implement SumItUp as a Java application. You application must satisfy ALL the specific requirements given below: a) The title of the top-level container must be ‘Welcome to SumItUp!’. b) The initial text should be ‘Enter two operands, result and click on 'Check!'’. See Figure 1. c) There should be no more than 4 Rabbit images per row. See Hint 1. d) The text fields should be wide enough to display at least TWO characters. e) The button ‘Check!’ must not resize when the GUI is resized. See Hint 2 and Figure 3. f) The ‘plus sign’ icon should appear vertically centered between the two sets of Rabbit images and must not resize when the GUI is resized. See Hint 2 and Figure 3. g) When first launched and whenever a correct answer is given, the number of displayed Rabbit images for each operand should change to any number between 1 and 10 (inclusive). See Hint 3 and Hint 4. Note: It is possible for the next number(s) to be the same as the current number(s). h) Nothing should happen if the user clicks the ‘Check!’ button while at least one of the text fields are empty, i.e. no errors should be thrown in this case. Note: You can assume that only a numeric value will be entered into the text fields.
06-09

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值