剑指Offer行榜【牛客网】练习(十一)

28 篇文章 0 订阅
13 篇文章 0 订阅
1、孩子们的游戏

题目描述:
每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0…m-1报数…这样下去…直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!_)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)

思路:
建立一个list存放所有小朋友,指针循环m次找到需要剔除的对象。需要考虑两种情况:
1、当前指针pointer+移动距离move < 列表长度,此时pointer = pointer + move
2、当前指针pointer+移动距离move >= 列表长度,此时pointer = move - (list.size() - pointer)

注意点:
如果剔除的是最后一个元素,指针要指向开始。

代码:

import java.util.ArrayList;
public class Solution {
    //n=5 m=3
    // 0 1 2 3 4 012
    public int LastRemaining_Solution(int n, int m) {
        if(n==0){
            return -1;
        }
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0;i<n;i++){
            list.add(new Integer(i));
        }
        int pointer = 0;
        while(list.size()!=1){
            int move = (m-1)%list.size();
            if(pointer+move<list.size()){
                pointer = pointer + move;
            }else{
                pointer = move - (list.size()-pointer);
            }
            list.remove(pointer);
            if(pointer>=list.size()){
                pointer = 0;
            }
        }
        return list.get(0);
    }
}
2、求1~n之和

题目描述:
求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

思路:
短路求值

代码:

public class Solution {
    public int Sum_Solution(int n) {
        int result = n;
        boolean flag = (n>0 && (result+=Sum_Solution(n-1))>0);
        return result;
    }
}
3、不用加减乘除做加法

题目描述:
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。

思路:
模仿十进制加减法
1、得到未算进位的值,0+0=0,1+1=0,0+1=1,故此处使用^
2、得到进位值,0+0=0,1+1=1,0+1=0,故此处使用&,并使用<<1移位
3、重复1-2,直到进位为0停止

代码:

public class Solution {
    public int Add(int num1,int num2) {
        while(num2!=0){
            int temp = num1^num2;
            num2 = (num1&num2)<<1;//&且,若有一个为0则为0,进位
            num1 = temp;
        }
        return num1;
    }
}
4、字符串转换成整数

题目描述:
将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。

思路:
当前值*10+下一位

注意点:
开头为+和-要判断

代码:

public class Solution {
    public int StrToInt(String str) {
        int result = 0;
        boolean negative = false;
        if(str.length()>0){
            if(str.charAt(0)=='+'){
                str = str.substring(1);
            }
            else if(str.charAt(0)=='-'){
                str = str.substring(1);
                negative = true;
            }
        }
        for(int i=0;i<str.length();i++){
            int charval = str.charAt(i)-'0';
            if(charval>=0&&charval<=9){
                result = result*10+charval;
            }else{
                return 0;
            }
        }
        if(negative){
            return -result;
        }else{
            return result;
        }
    }
}
5、数组中重复的数字

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

思路:
声明一个list,逐个存放数组数字,如果已经存在,则为true,并得到该数字。

代码:

import java.util.ArrayList;
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) {
        ArrayList<Integer> existed = new ArrayList<>();
        for(int i=0;i<length;i++){
            if(existed.indexOf(numbers[i])>=0){
                duplication[0] = numbers[i];
                return true;
            }
            else if(existed.indexOf(numbers[i])<0){
                existed.add(numbers[i]);
            }
        }
        return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值