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; }