简体   繁体   中英

Sum of integers in an array

So, basically, the questions asks me to find the sum of the numbers in an array. Except the number '13' is very unlucky, so it does not count '13' and the number that come immediately after a '13'.

This is what I did:

public int sum13(int[] nums) {
  int d = 0;
  int sum = 0;

  for (int i=0;i<nums.length;i++){
    if(nums[i] == 13){
    d = i;
    break;
    }
    else{
    d = nums.length;
    }
  }
  for(int i=0;i<d;i++){
   sum = sum + nums[i];
  }
  return sum;
}

Even though I pass most of the tests, I still can't understand how to exclude the number right next to the 13 from the sum.

For example, sum13({1, 2, 2, 1, 13}) → 6 PASSES! sum13({13, 1, 13}) → 0 PASSES! But, sum13({13, 1, 2, 13, 2, 1, 13}) → 3 RETURNS 0 instead as it stops at the first instance of 13.

Why are you using two loops? If the number is 13 , just don't add it or the next number to the sum, like this:

public int sum13(int[] nums) {
    int sum = 0;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == 13) {
            i++;
        } else {
            sum = sum + nums[i];
        }
    }
    return sum;
}

As per your question you exclude if the number is 13 & its immediate successor. So when you find 13 increment the loop counter once more:

 for(int i=0;i<nums.length;i++){
   if(nums[i] == 13) {
     i++;
    }
   else {
     sum = sum + nums[i];
   }
  }

This will naturally skip the next element of 13, if there's one within the array.

How about summing directly in the first for loop? Shorter, easier and faster way :)

public int sum13(int[] nums) {
  int sum = 0;

  for (int i=0;i<nums.length;i++){
    if (nums[i] != 13) {
      sum += nums[i];
    } else {
      i++;
    }
  }
  return sum;
}

Did you hear what continue does?

if(nums[i] == 13){
     d = i;
     continue;
}
public int sum13(int[] nums) {
      boolean skip = false;
      int sum = 0;

      for (int i=0;i<nums.length;i++){
        if(nums[i] == 13){
            skip=true;
            continue;
        }
        else{
        if(skip) {
            skip=false;
            continue;
        }
        sum+=nums[i];
        }
      }

      return sum;
    }

What you are actually doing is stopping at the first occurrence of 13.

try this:

public int sum13(int[] nums) {
    int sum = 0;

    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == 13) {
            i++;
        } else {
            sum += nums[i];
        }
    }

    return sum;
}

I didn't test it but it should work...

Cheers!

Try this

   public int mySum(int[] nums, int except) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
             sum += (nums[i] == except) ? 0: nums[i];
        }
        return sum;
    }

Then

mySum(nums, 13)

use the continue; statement when the value equals to 13.

public int sum13(int[] nums) {
  int sum = 0;
  for (int i=0;i<nums.length;i++){
    if(nums[i] == 13){
       continue;
    }
    else{
       sum = sum + nums[i];
    }
  }
  return sum;
}

Try this.

public int sum(int[] nums) {
    int sum = 0;
    for (int i = 0; i < nums.length;) {
        if (nums[i] == 13)
            i = i + 2;
        else {
            sum = sum + nums[i];
            i++;
        }
    }
    return sum;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM