LeetCode--137. Single Number II && 260. Single Number III之暴力搜法

来源:互联网 发布:基本款手提包 知乎 编辑:程序博客网 时间:2024/06/06 09:15

137. Single Number II 

题目:

Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

分析:给一个整数数组,其中除了一个数外其他整数的数目均为三个,找出单个的那个整数,本代码使用暴力搜索法。分情况处理,排序后单个数在最后或在中间和开头的结构不一样,分情况处理。

代码:

public class Solution {
    public int singleNumber(int[] nums) {
        if(nums.length==2||nums.length==3) return 0;
        if(nums.length==1){//长度为1,直接输出第一个数即可
return nums[0];
}else{
            Arrays.sort(nums);//对输入的数排序
            if(nums[nums.length-2]!= nums[nums.length-1]){//单个数在最后,直接输出最后一个数即可
 return nums[nums.length-1];
}else{
               int result=0;
               for(int i=0,j=1,k=2;i<nums.length-1;){//单个数在中间和头部的情况
               if((nums[i] == nums[j])&&(nums[j]==nums[k])){
          i=i+3;
          j=j+3;
                          k=k+3;
           }else{
                     result = nums[i];
                     break;
                    }
                }  
                return result;
            }
        }
    }
}

260. Single Number III

题目:

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity

分析:给一个整数数组,除了两个整数外,其他的整数的数目均为两个,找到两个单个的整数,分情况处理,排序后两个单个的整数在头部,尾部,中间,一中间一尾部处理

代码:

public class Solution {
    public int[] singleNumber(int[] nums) {
        int result[] = new int[2];
        if(nums.length==2){//长度为2,直接输出即可
return nums;
}else{
            Arrays.sort(nums);//对输入的数排序
            if((nums[nums.length-2]!= nums[nums.length-1])&&(nums[nums.length-2]!= nums[nums.length-3])){//排序后单个数在最后两个,直接输出最后两数即可
result[0]=nums[nums.length-2];
                result[1]=nums[nums.length-1];
                return result;
}else if((nums[0]!= nums[1])&&(nums[1]!= nums[2])){//排序后两个单数在首位的情况
                result[0]=nums[0];
                result[1]=nums[1];
                return result;
            }else if((nums[nums.length-2]!= nums[nums.length-1])&&(nums[nums.length-2]==nums[nums.length-3])){//一个在尾部的情况
                  result[1] = nums[nums.length-1];
                  for(int i=0,j=1;i<nums.length-1;){//排序后另外一个单数在头部或中间的情况
                  if(nums[i] == nums[j]){
            i=i+2;
            j=j+2;}
                  else{
                       result[0]=nums[i];
                       break;
                     }
                   } 
                  return result;
            }else{
              ArrayList<Integer> allList = new ArrayList<Integer>();
               for(int i=0,j=1;i<nums.length-1;){//排序后两个单数在中间的情况
               if(nums[i] == nums[j]){
          i=i+2;
          j=j+2;
           }else{
                     allList.add(nums[i]);
                     i=i+1;
                     j=j+1;
                    }
                }  
                Integer tmpInteger[] = new Integer[2];
                allList.toArray(tmpInteger);
                int ary[] = new int[2];
                for (int i = 0; i < 2; i++) {  
                    ary[i] = tmpInteger[i].intValue();
                }  
                return ary;
            }
        }
    }
}




原创粉丝点击