剑指offer(1/17)

条件限制求和

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

public class Solution {
    public int Sum_Solution(int n) {
        int sum = n;
        boolean ans = (n>0)&&((sum += Sum_Solution(n-1))>0);
        return sum;
    }
}
class Solution {
public:
        int Sum_Solution(int n)
    {
        int sum = n;
        bool ans = (n>0)&&((sum+=Sum_Solution(n-1))>0);
        return sum;
    }
};
/*
解题思路:
1.需利用逻辑与的短路特性实现递归终止。 
2.当n==0时,(n>0)&&((sum+=Sum_Solution(n-1))>0)只执行前面的判断,为false,然后直接返回0;
3.当n>0时,执行sum+=Sum_Solution(n-1),实现递归计算Sum_Solution(n)。
*/

不用±*/求和

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

public class Solution {
    public int Add(int num1,int num2) {
        return num2==0 ? num1 : Add(num1^num2,(num1&num2)<<1);
    }
}
class Solution {
public:
    int Add(int num1, int num2)
    {
        return num2==0?num1:Add(num1^num2,(num1&num2)<<1);
    }
};

把字符串转换成整数

将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0

public class Solution {
    public int StrToInt(String str) {
        if(str==null || str.length()==0) return 0;
        int tag = 1,start = 0;
        char ch = str.charAt(0);
        if(ch == '+')
            start = 1;
        else if(ch == '-'){
            tag = 0;
            start = 1;
        }
        long res = 0;
        for(int i = start;i < str.length();i++){
            char c = str.charAt(i);
            if(c>='0' && c<='9'){
                res = res * 10 + c - '0';
//                 if(tag==1 && res>Integer.MAX_VALUE){
//                     throw new IllegalArgumentException("超上界");
//                 }
//                 if(tag==0 && res<Integer.MIN_VALUE){
//                     throw new IllegalArgumentException("超下界");
//                 }
            }else{
                return 0;
            }
        }
        if(tag == 0)
            return (int) -res;
        else
            return (int) res;
    }
}
class Solution {
public:
    int StrToInt(string str) {
        if (str.empty())return 0;
        int res=0;
        int mark= (str[0]=='-')?-1:1;
        int i= (str[0]=='+'||str[0]=='-')?1:0;
        for(;i<str.size();i++)
        {
            if(str[i]<'0'||str[i]>'9')
                return 0;
            res=res*10+str[i]-'0';
        } 
        return res*mark;
    }
};

数组中重复的数字

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

public class Solution {
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        for(int i = 0; i != length; i++){
            int idx = numbers[i]%length;
            if(numbers[idx] >= length){
                duplication[0] = idx;
                return true;
            }
            numbers[idx] += length;
        }
        return false;
    }
}
class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) {
        if(length < 2)
            return false;
        vector<bool> hash(length,false);
        for(int i = 0; i < length; i++)
        {
            if(hash[numbers[i]] == false)
                hash[numbers[i]] = true;
            else
            {
                *duplication = numbers[i];
                return true;
            }
        }
        return false;
    }
};

构建乘积数组

给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]=A[0]A[1]…*A[i-1]A[i+1]…*A[n-1]。不能使用除法。(注意:规定B[0] = A[1] * A[2] * … * A[n-1],B[n-1] = A[0] * A[1] * … * A[n-2];)
对于A长度为1的情况,B无意义,故而无法构建,因此该情况不会存在。

import java.util.ArrayList;
public class Solution {
    public int[] multiply(int[] A) {
        int len = A.length;
        int[] B = new int[len];
        int ret = 1;
        for(int i = 0;i < len; ret *= A[i++])
            B[i] = ret;
        ret = 1;
        for(int i = len - 1; i >= 0; ret *= A[i--])
            B[i] *= ret;
        return B;
    }
}
class Solution {
public:
    vector<int> multiply(const vector<int>& A) {
        int n = A.size();
        vector<int> B(n,1);
        int ret = 1;
        //下三角
        for(int i = 0;i < n; ret *= A[i++]){
            B[i] = ret;
        }
        ret = 1;
        //上三角
        for(int i = n-1;i >= 0;ret *= A[i--]){
            B[i] *= ret; //要保留上面下三角的乘积,所以要*=
        }
        return B;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值