LeetCode && AcWing 01

Author:龙箬
Data Science and Big Data Technology
Change the world with data!
CSDN@weixin_43975035
算法刷题,坚持下去,就是胜利

AcWing

找出数组中重复的数字

LeetCode && AcWing 01_第1张图片

代码如下:

public class AcWing13 {
    public int duplicateArray(int[] nums){
        for (int i=0;i<nums.length;i++){
            if (nums[i]<0 || nums[i]>nums.length)
                return -1;
        }
        int[] res=new int[nums.length];
        for (int n:nums){
            if (res[n]==1){
                System.out.println(n);
                return n;
            }
            res[n]=1;
        }
        return -1;
    }

    public static void main(String[] args){
        int[] nums={-1,2,3,5,4,3,2,6,7};
        AcWing13 deplicateArray=new AcWing13();
        deplicateArray.duplicateArray(nums);
    }
}

不修改数组找出重复的数字

LeetCode && AcWing 01_第2张图片
代码如下:

//解法一:暴力解决
public class AcWing14 {
    public int duplicateInArray(int[] nums){
        int n=nums.length;
        for (int i=0;i<n-1;i++)
            for (int j=i+1;j<n;j++)
                if (nums[i]==nums[j])
                    return nums[i];
        return 0;
    }

    public static void main(String[] args){
        int[] nums={9,8,2,3,5,4,3,2,6,7};
        AcWing14 duplicateInArray=new AcWing14();
        duplicateInArray.duplicateInArray(nums);
    }
}
//解法二:抽屉原理(鸽巢原理)
public class AcWing14 {
    public int duplicateInArray(int[] nums) {
        int n= nums.length;
        //抽屉原理   分治思想
        int l = 1, r = n-1;
        while(l < r){
            int mid = (l + r)/2;  // 划分的区间:[l, mid], [mid + 1, r]
            int count=0; //个数
            for(int x : nums) //x为数组中的值
                if(x >= l && x <= mid)
                    ++count;
            if (count > mid - l + 1) //左区间个数多
                r = mid; //左区间
            else
                l = mid + 1; //右区间
        }
        System.out.println(r);
        return r;
   }

    public static void main(String[] args){
        int[] nums={9,8,2,3,5,4,3,2,6,7};
        AcWing14 duplicateInArray=new AcWing14();
        duplicateInArray.duplicateInArray(nums);
    }
}

二维数组中的查找

LeetCode && AcWing 01_第3张图片
代码如下:

public class AcWing15 {
    public boolean searchArray(int[][] array, int target) {
        if (array==null || array.length==0 || array[0].length==0)
            return false;
        int row=0,col=array[0].length-1;
        while(row<=array.length-1 && col>=0){
            if (target==array[row][col])
                return true;
            else if (target>array[row][col])
                row++;
            else col--;
        }
        return false;
    }

    public static void main(String[] args){
        int[][] array={{},{}};
        int target=1;
        AcWing15 searchArray=new AcWing15();
        searchArray.searchArray(array,target);
    }
}

替换空格

LeetCode && AcWing 01_第4张图片

代码如下:

public class AcWing16 {
    public String replaceSpaces(StringBuffer str){
        StringBuffer res=new StringBuffer();
        int len=str.length()-1;
        for (int i=0;i<=len;i++){
            if (str.charAt(i)==' ')
                res.append("%20");
            else res.append(str.charAt(i));
        }
        return res.toString();
    }

    public static void main(String[] args){
        AcWing16 replaceSpaces=new AcWing16();
        StringBuffer str=new StringBuffer("We are happy.");
        replaceSpaces.replaceSpaces(str);
    }
}

从尾到头打印链表

LeetCode && AcWing 01_第5张图片
代码如下:

class ListNode{
    int val;
    ListNode next;
    ListNode(int x){
        val=x;
    }

    public void add(int newval){
        ListNode newNode=new ListNode(newval);
        if (this.next==null)
            this.next=newNode;
        else
            this.next.add(newval);
    }
}

public class AcWing17 {
    public int[] printListReversingly(ListNode head) {
        int count=0;
        ListNode node=head;
        while (node!=null){
            node=node.next;
            count++;
        }
        int[] res=new int[count];
        while (head!=null){
            res[--count]=head.val;
            head=head.next;
        }
        return res;
    }
    
    public static void main(String[] args){
        ListNode l1=new ListNode(2);
        l1.add(3);
        l1.add(5);
        AcWing17 printListReversingly =new AcWing17();
        printListReversingly.printListReversingly(l1);
    }
}

如有侵权,请联系侵删。
代码如有错误,欢迎留言批评改正。

你可能感兴趣的:(笔记,leetcode,算法,java,数据结构)