1. 程式人生 > >2、位元組跳動-陣列與排序

2、位元組跳動-陣列與排序

1、三數之和

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) break;
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            int j = nums.length - 1;
            int target = 0 - nums[i];
            int k = i + 1;
            while (k < j) {
                if (nums[k] + nums[j] == target) {
                    List<Integer> item = Arrays.asList(nums[i], nums[k], nums[j]);
                    result.add(item);
                    while (k < j && nums[k] == nums[k + 1]) k++;
                    while (k < j && nums[j] == nums[j - 1]) j--;
                    k++;j--;
                } else if (nums[k] + nums[j] < target) {
                    k++;
                } else {
                    j--;
                }
            }
        }
        return result;
    }
}

原文:https://blog.csdn.net/qq_35170267/article/details/81031368 
 

2、島嶼的最大面積

class Solution {
    
    public int maxAreaOfIsland(int[][] grid) {
        int i,j,temp,result=0;
        for(i=0;i<grid.length;i++){
            for(j=0;j<grid[0].length;j++){
                if(grid[i][j]==1){
                    result=Math.max((temp=countArea(grid,i,j)),result);
                }
            }
        }
        return result;
    }
    
     public int countArea(int [][] grid,int i,int j){
        if(i<0||j<0||i>=grid.length||j>=grid[0].length||grid[i][j]!=1) return 0;
            
         grid[i][j]=0;
            return 1+countArea(grid,i+1,j)+countArea(grid,i-1,j)+countArea(grid,i,j+1)+countArea(grid,i,j-1);
    }
      
}

原文:https://blog.csdn.net/qq_38959715/article/details/80937405 

 

3、搜尋旋轉排序陣列

public int search(int[] nums, int target) {
    if(nums==null||nums.length<1) return -1;
    int left = 0;
    int right = nums.length - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) return mid;
        //條件1
        if (nums[mid] >= nums[left]) {
            if (target < nums[mid] && target >= nums[left]) {
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        //條件2
        if (nums[mid] <= nums[right]) {
            if (target > nums[mid] && target <= nums[right]) {
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
    }
    return -1;
}

參考:https://www.cnblogs.com/keeya/p/9689927.html

 

4、最長連續遞增序列

public class Test48 {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int[] res=new int[n];
        for(int i=0;i<n;i++)
            res[i]=sc.nextInt();
        System.out.println(getMaxlength(res));
            
    }

    public static int getMaxlength(int[] A) {
        int size=A.length;
        if (size <= 0)
            return 0;
       
        int res = 1;
        int current = 1;
        for (int i = 1; i < size; i++) {
            if (A[i] > A[i - 1]) {
                current++;
            } else {
                if (current > res) {
                   
                    res = current;
                }
                current = 1;
            }
        }
        return res;
    }
    
}

參考:https://blog.csdn.net/boguesfei/article/details/82901414

5、陣列中的第K個最大元素

public int findKthLargest(int[] nums, int k) {
    Arrays.sort(nums);
    return nums[nums.length - k];
}

參考:https://blog.csdn.net/ccccc1997/article/details/81673753

 

6、最長連續序列

public  int longestcontinueArrays(int arr[])
    {

        if(arr==null||arr.length==0)
            return 0;
        int longest=0;
        int len=1;
        Arrays.sort(arr);
        //對陣列進行排序
        for(int i=0;i<arr.length;i++)
        {
            if(i+1<arr.length&&arr[i]+1==arr[i+1])
            {
                len++;
                longest=Math.max(longest, len);
                //longest 儲存當前最長連續陣列的長度。
            }
            else {
                len=1;
                //當不連續時將len=1
            }
        }
        return longest;

    }

原文:https://blog.csdn.net/u013309870/article/details/70242770 

 

7、第k個排列

public class Solution {
    public String getPermutation(int n, int k) {
        k--;
        List<Integer> list = new ArrayList<Integer>();//注意儲存1-n
        StringBuilder s = new StringBuilder();
        int times = n-1;
        for(int i=1;i<=n;i++){
            list.add(i);
        }
        int factorail = 1;//階乘
        for(int i=2;i<n;i++){//不要×n
            factorail*=i;
        }
        while(times>=0){
            int indexList = k/factorail;
            s.append(list.get(indexList));
            list.remove(indexList);
            k=k%factorail;
            if(times!=0){
                factorail/=times;
            }
            times--;
        }
        return s.toString();
    }
}

原文:https://blog.csdn.net/Lynn_Baby/article/details/80948414 

 

8、朋友圈

 

public class Solution {
    public void dfs(int[][] M, int[] visited, int i) {
        for (int j = 0; j < M.length; j++) {
            if (M[i][j] == 1 && visited[j] == 0) {
                visited[j] = 1;
                dfs(M, visited, j);
            }
        }
    }
    public int findCircleNum(int[][] M) {
        int[] visited = new int[M.length];
        int count = 0;
        for (int i = 0; i < M.length; i++) {
            if (visited[i] == 0) {
                dfs(M, visited, i);
                count++;
            }
        }
        return count;
    }
}

原文:https://blog.csdn.net/mine_song/article/details/70195463 

 

9、合併區間

 

public class Solution {

    public List<Interval> merge(List<Interval> intervals) {
        List<Interval> result = new LinkedList<>();

        if (intervals == null || intervals.size() < 1) {
            return result;
        }

        // 先對區間進行排序,使用一個匿名內部類
        Collections.sort(intervals, new Comparator<Interval>() {
            @Override
            public int compare(Interval o1, Interval o2) {
                return o1.start - o2.start;
            }
        });

        // 排序後,後一個元素(記為next)的start一定是不小於前一個(記為prev)start的,
        // 對於新加入的區間,假設next.start大於prev.end就說明這兩個區間是分開的,要添
        // 加一個新的區間。否則說明next.start在[prev.start, prev.end]內。則僅僅要看
        // next.end是否是大於prev.end,假設大於就要合併區間(擴大)
        Interval prev = null;
        for (Interval item : intervals) {

            if (prev == null || prev.end < item.start) {
                result.add(item);
                prev = item;
            } else if (prev.end < item.end) {
                prev.end = item.end;
            }
        }

        return result;
    }
}

參考:https://www.cnblogs.com/gccbuaa/p/7088508.html

 

10、接雨水


public class Solution {
    /**
     * @param heights: an array of integers
     * @return: a integer
     */
    public int trapRainWater(int[] heights) {
        // write your code here
        int left = 0, right = heights.length - 1; 
        int res = 0;
        if(left >= right)
            return res;
        int leftheight = heights[left];
        int rightheight = heights[right];
        while(left < right) {
            if(leftheight < rightheight) {
                left ++;
                if(leftheight > heights[left]) {
                    res += (leftheight - heights[left]);
                } else {
                    leftheight = heights[left];
                }
            } else {
                right --;
                if(rightheight > heights[right]) {
                    res += (rightheight - heights[right]);
                } else {
                    rightheight = heights[right];
                }
            }
        }
        return res;
 
    }
}

原文:https://blog.csdn.net/qq_14927217/article/details/72861208