剑指OFFER-调整数组顺序使奇数位于偶数前面(Java)

1. 数组中重复的数字

1.1 题目描述

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中第一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
返回描述:
如果数组中有重复的数字,函数返回true,否则返回false。
如果数组中有重复的数字,把重复的数字放到参数duplication[0]中。(ps:duplication已经初始化,可以直接赋值使用。)

1.2 核心代码实现

import java.util.*;
public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个,赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        if(numbers == null || length == 0){
            return false;
        }
        /*方法一
        for(int i = 0; i < length; i++){
            for(int j = i + 1; j < length; j++){
                if(numbers[i] == numbers[j]){
                    duplication[0] = numbers[i];
                    return true;
                }
            }
        }
        return false;
        */
        
        //方法二
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i < length; i++){
            if(list.contains(numbers[i])){
                duplication[0] = numbers[i];
                return true;
            }else{
                list.add(numbers[i]);
            }
        }
        return false;
    
    }
}

题目描述:
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中第一个重复的数字。 例如,如果输入长度为7的数组[2,3,1,0,2,5,3],那么对应的输出是第一个重复的数字2。没有重复的数字返回-1。

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param numbers int整型一维数组 
     * @return int整型
     */
    public int duplicate (int[] numbers) {
        if(numbers == null || numbers.length <= 1) return -1;
        int len = numbers.length;
        int res = -1;
        int[] array = new int[len];
        here:
        for(int num : numbers){
            array[num]++;
            for(int i = 0; i < array.length; i++){
                if(array[i] > 1){
                    res = i;
                    break here;
                }
            }
        }
        return res;
    }
}

2. 字符流中第一个不重复的字符

2.1 题目描述

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。

2.2 返回值描述

如果当前字符流没有存在出现一次的字符,返回#字符。

2.3 核心代码实现

import java.util.*;

public class Solution {
    //Insert one char from stringstream
    Map<Character, Integer> map = new LinkedHashMap<>();
    public void Insert(char ch){
        if(map.containsKey(ch)){
            map.put(ch, -1);
        }else{
            map.put(ch, 1);
        }
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce(){
        for(char ch: map.keySet()){
            if(map.get(ch) == 1) return ch;
        }
        return '#';
    
    }
}

3. 调整数组顺序使奇数位于偶数前面

3.1 题目描述

输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

3.2 示例1

输入:

[1,2,3,4]

返回值:

[1,3,2,4]

3.3 核心代码实现

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param array int整型一维数组 
     * @return int整型一维数组
     */
    public int[] reOrderArray (int[] array) {
        if(array == null || array.length == 0) return new int[0];
        int len = array.length;
        for(int i = 0; i < len; i++){
            for(int j = 0; j < len - 1 - i; j++){
                if((array[j] & 1) == 0 && (array[j + 1] & 1) == 1){
                    int num = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = num;
                }
            }
        }
        return array;
        
        /*
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for(int num : array){
            if(num % 2 == 0){
                list2.add(num);
            }else{
                list1.add(num);
            }
        }
        list1.addAll(list2);
        //int[] res = list1.stream().mapToInt(Integer::intValue).toArray();
        int[] res = new int[list1.size()];
        for(int i = 0; i < list1.size(); i++){
            res[i] = list1.get(i);
        }
        return res;
        */
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值