基础常用算法

1:#### 判断是个整数是不是回文

#!/usr/bin/python
# coding=utf-8

def ishuiwen(num):
        if num <0 or num%10==0:
                ### 负数 或者末尾是0 的肯定不是回文
                return False
        i=0
        while i < num:
                i = i*10+num%10
                num = num/10
        return (num == i) or  (i/10 == num)

2:/**求一个字符串连续不重复的长度 acdd = 3, 1122345 = 4*/

/**求一个字符串连续不重复的长度 acdd = 3, 1122345 = 4*/
public static void findStringMaxNoSame(String st){

    Map<Character,Integer>  map = new HashMap<Character,Integer>();
    int j =0;
    int max =0;
    for(int i = 0; i < st.length(); i++){
        if(map.containsKey(st.charAt(i))){
           if(map.get(st.charAt(i)) > j){
               j = i;
            }
        }
        map.put(st.charAt(i),i);
        if(max < (i -j +1) ){
            max = i =j +1;
        }
    }
    System.out.println("max = [" + max+ "]  j  = "+j);
}

3. /**求一个子数组的最大的和**/

/**求一个子数组的最大的和**/
public static void  maxSubArrayList( int[] array){

    int max =0;
    int tmpMax =0;

    for(int i=0; i < array.length; i++){

        tmpMax += array[i];
        if(tmpMax <0){

            tmpMax=0;
            continue;
        }
        if(tmpMax > max){
            max =tmpMax;
        }
    }
    //全部为负数的时候
    if(max==0){
        max = array[0];
        for(int i=1; i < array.length; i++){

            if(max < array[i]){
                max = array[i];

            }
        }
    }

    System.out.println("array sub max = [" + max + "]");

}
4. 给定一个数字A,返回数组中和等于A的两个数子的index
public static void getAddIndexs(int[] array,int specalValue){

    //1<= specaVaue 放入map
    //减掉的数据是否在map中
    Map<Integer,Integer> map = new HashMap<Integer,Integer>();
    for(int i =0; i < array.length;i++){

        if((specalValue >= array[i]) && map.containsKey((specalValue-array[i]))){
            //有结果
            System.out.println(map.get((specalValue - array[i])) + " " + i);
        }
        map.put(array[i],i);
    }
}

5./*反转一个32位的整数,主要溢出*/

/*反转一个32位的整数,主要溢出*/
public static void reverse(int x) {

        // if x <0 处理; 处理类似: 1000
        String abs = String.valueOf(x);
        String intMax = String.valueOf(Integer.MAX_VALUE);
        StringBuffer sb =new StringBuffer();
        sb.append(abs);
        abs = sb.reverse().toString();
        if(intMax.compareTo(abs)<0){
            System.out.println("x reverse = [" + 0 + "]");
        }else{
            System.out.println("x reverse = [" + Integer.parseInt(abs) + "]");
        }
}

public static int reversev0(int x)
{
    int result = 0;

    while (x != 0)
    {
        int tail = x % 10;
        int newResult = result * 10 + tail;
        if ((newResult - tail) / 10 != result)//往回计算,利用溢出做比较
        { return 0; }
        result = newResult;
        x = x / 10;
    }

    return result;
}

转载于:https://my.oschina.net/u/1388024/blog/874630

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值