数据结构与算法练习(Java实现)

来源:互联网 发布:淘宝买家等级一个钻 编辑:程序博客网 时间:2024/05/22 15:18
package lintcode;






/**
 * 
* @ClassName: Solution 
* @Description: TODO() 
* @author A18ccms a18ccms_gmail_com 
* @date 2017年8月14日 上午10:12:29 
*
 */
public class Solution {
    /**
     * @param str: a string
     * @return: a boolean
     */
public static void main(String[] args) {
// System.out.println(isUnique("abca"));
// System.out.println(aplusb(2, 4));
// System.out.println(strStr("hello,world","world"));
// System.out.println(isPalindrome(11));
// System.out.println(replaceSpace(new StringBuffer("we are happy")));
syso(checkDifferent("abca"));
}
public static void syso(Object object){
System.out.println(object);
}
/**

* @Title: strStr 
* @Description: TODO(对于一个给定的 source 字符串和一个 target 字符串,
* 你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。
*   如果不存在,则返回 -1。) 
* @param @param source
* @param @param target
* @param @return    设定文件 
* @return int    返回类型 
* @throws
*/
public static int strStr(String source, String target) {
        // write your code here
if (source == null || target == null) {
       return -1;
   }
   
    int i, j;
    for (i = 0; i < source.length() - target.length() + 1; i++) {
       for (j = 0; j < target.length(); j++) {
          if (source.charAt(i + j) != target.charAt(j)) {
               break;
           }
       }
       if (j == target.length()) {
           return i;
        }
    }
    return -1;
}


/**

* @Title: removeElement 
* @Description: TODO(给出一个数组 [0,4,4,0,0,2,4,4],和值 4
    *返回 4 并且4个元素的新数组为[0,0,0,2]) 
* @param @param A
* @param @param elem
* @param @return    设定文件 
* @return int    返回类型 
* @throws
*/
public int removeElement(int[] A, int elem) {
        // write your code here
int num=0;
for (int i = 0; i < A.length; i++) {
if(A[i]==elem){
num++;

}
return 1;
}
return 0;
    }
    /** 
    * @Title: isUnique 
    * @Description: TODO(//判断字符串是否重复) 
    * @param @param str
    * @param @return    设定文件 
    * @return boolean    返回类型 
    * @throws 
    */
    public static boolean isUnique(String str) {
        // write your code here
        if(str==null||str.isEmpty()){
            return true;
        }
        char[] elements=str.toCharArray();  
        
        for(char e:elements){
            if(str.indexOf(e)!=str.lastIndexOf(e)){
                return false;
            }
        }
        return true;
    }
    /** 
    * @Title: aplusb 
    * @Description: TODO(两数相加) 
    * @param @param a
    * @param @param b
    * @param @return    设定文件 
    * @return int    返回类型 
    * @throws 
    */
    public static int aplusb(int a, int b) {
        // write your code here
     if(a==0) return b;  
          if(b==0) return a;  
          int sum,i;  
          i=a^b;  
          sum=(a&b)<<1;  
          return aplusb(sum,i);  
     
    }
    /** 
    * @Title: moveZeroes 
    * @Description: TODO(给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序) 
    * @param @param nums    设定文件 
    * @return void    返回类型 
    * @throws 
    */
    public static void moveZeroes(int[] nums) {
        // Write your code here
    int one = 0;
         int fast = 0;
         int n = nums.length;
         int x = 0;
         for(int i=0;i<n;i++){
             if(nums[i]!=x) { // 不为0 的向前移动
                 nums[one] = nums[i];
                 one++;
             }
         }
         for(int i= one;i<n;i++) // 后面的就是0
             nums[i] = x;
    }
    
    public int sum(int a,int b){
   
    return a+b;
    }
    
    /**
     * 
    * @ClassName: ListNode 
    * @Description: TODO( 将两个排序链表合并为一个新的排序链表) 
    * @author A18ccms a18ccms_gmail_com 
    * @date 2017年8月14日 上午10:15:50 
    *
     */
    public class ListNode { 
        int val; 
        ListNode next = null; 
     
        ListNode(int val) { 
            this.val = val; 
        } 
public ListNode Merge(ListNode list1,ListNode list2) {  
      if(list1==null)return list2; //判断到某个链表为空就返回另一个链表。如果两个链表都为空呢?没关系,这时候随便返回哪个链表,不也是空的吗?  
      if(list2==null)return list1;  
      ListNode list0=null;//定义一个链表作为返回值  
       if(list1.val<list2.val){//判断此时的值,如果list1比较小,就先把list1赋值给list0,反之亦然  
          list0=list1;  
          list0.next=Merge(list1.next, list2);//做递归,求链表的下一跳的值  
          }  
          else{  
          list0=list2;  
          list0.next=Merge(list1, list2.next);  
          }  
return list0;  
   }  
}
    /**
     * 
    * @Title: isPalindrome 
    * @Description: TODO(回文数判断) 
    * @param @param x
    * @param @return    设定文件 
    * @return boolean    返回类型 
    * @throws
     */
    public static boolean isPalindrome(int x) {
        String  str=x+"";
      
        if(str.length()<=1){
        return true;
        }else{
        if(new StringBuffer(str).reverse().toString().equals(str)){
        return true;
        }
         return false;
    }
    }
    /*
     * 请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
     */
    public static String replaceSpace(StringBuffer str) {
        //从后往前,先确定字符串的长度和替换后的长度
     int len=str.length();
     int count=0;
     for(int i=0;i<str.length();i++){
         if(str.charAt(i)==' ')
             count++;
     }
     int newLen=2*count+len;
     int index=newLen-1;
     char []ptr=new char[newLen];
     while(len>0){
         if(str.charAt(len-1)!=' '){
             ptr[index--]=str.charAt(len-1);
         }else{
             ptr[index--]='0';
             ptr[index--]='2';
             ptr[index--]='%';
         }     
         --len;
     }
      return new String(ptr);




  }
    /*
     * 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。
     * 也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},
     * 那么对应的输出是第一个重复的数字2
     */
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        for(int i = 0; i < length; i++){
            //由于数字都是0到length -1范围内的,将以遍历到的数字为下标的元素加上length
            //当第二 次找到该下标元素,发现它大于length-1时,说明,该下标数字重复了。
            //当遍历到一个元素,发现它大于length -1时,让它前去length,就为原来的元素
            int index = numbers[i];//当遍历到一个元素,发现它大于length -1时,让它前去length,就为原来的元素
            if(index > length - 1)
                index -= length;
            if(numbers[index] > length - 1){//当第二 次找到该下标元素,发现它大于length-1时,说明,该下标数字重复了。
              //  *duplication = index;
                return true;
            }
            numbers[index] += length;//由于数字都是0到length -1范围内的,将以遍历到的数字为下标的元素加上length
        }
        return false;
        
    }
    /*
     * 给定一个二叉树,找到其最大深度。最大深度是从根节点到最远叶节点的最长路径中的节点数
     */
    /* public int maxDepth(TreeNode root){
    if (root == null)
             return 0;
         int leftDepth = maxDepth(root.left);
         int rightDepth = maxDepth(root.right);
         return leftDepth > rightDepth ? (leftDepth+1): (rightDepth+1);
     }*/
    /*
     * 请实现一个算法,确定一个字符串的所有字符是否全都不同。这里我们要求不允许使用额外的存储结构。
     */
public static boolean checkDifferent(String iniString) {
   // write code here
String str=iniString;  
      char c;  
      for(int i=0;i<str.length();i++){  
          c=str.charAt(i);  
          for(int j=i+1;j<str.length();j++){
          if(str.charAt(j)==c){
          return false;
          }else {
continue;
}
          }
     
   }
      return true;
}

 
}
原创粉丝点击