leetcode-month1-week4

Count and Say

package ygy.test.week4;

/**
 * Created by guoyao on 2017/9/24.
 */
public class CountandSay {

    public static void main(String[] args) {
        System.out.println(countAndSay(4));
    }
    //1
    //11
    //21
    //1211

    public static String countAndSay(int n) {
        StringBuilder temp=new StringBuilder("1");
        StringBuilder stringBuilder;
        int count;
        for (int i=1; i < n; i++) {
            stringBuilder=temp;   //1
            temp=new StringBuilder();  // " "
            count = 1 ;
            char ch=stringBuilder.charAt(0);  // 1
            for (int j=1; j < stringBuilder.length(); j++) {
                if (stringBuilder.charAt(j) != ch) {
                    temp.append(count).append(ch);
                    count=1;
                    ch=stringBuilder.charAt(j);
                    continue;
                }
                count++;
            }
            temp.append(count).append(ch);    //11
        }
        return temp.toString();
    }
}

Implement strStr

package ygy.test.week4;

/**
 * Created by guoyao on 2017/9/23.
 */
public class ImplementstrStr {

    public static void main(String[] args) {
        System.out.println(strStr("a","a"));
    }

    /**
     * Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
     * 大串包小串
     */

    public  static  int strStr(String haystack, String needle) {
        if (needle.length() == 0) {
            return  0 ;
        }

        for(int i = 0 ; i < (haystack.length() - needle.length() + 1) ;i ++) {
            if (haystack.substring(i, needle.length() + i).equals(needle)) {
                return i;
            }
        }
        return  -1 ;
    }
}

Maximum Subarray

package ygy.test.week4;

/**
 * Created by guoyao on 2017/9/24.
 */
public class MaximumSubarray {

    public static void main(String[] args) {
        System.out.println(maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
        System.out.println(maxSubArray_2(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));

    }

    //{ -1 , -2 , 3 , -4 , 9 , 2,24 ,-12}

    public static int maxSubArray(int[] nums) {
        int sum=0;
        int lastSum=Integer.MIN_VALUE;
        for (int i=0; i < nums.length; i++) {
            if (sum < 0) {
                sum=nums[i];
            } else {
                sum+=nums[i];
            }
            if (sum > lastSum) {
                lastSum=sum;
            }
        }
        return lastSum;
    }

    //leetcode
    public static int maxSubArray_2(int[] nums) {
        int subNum=nums[0];
        int maxNum=nums[0];
        for (int i=1; i < nums.length; i++) {
            subNum=Math.max(subNum + nums[i], nums[i]);
            maxNum=Math.max(maxNum, subNum);
        }
        return maxNum;
    }
}

Search Insert Position

package ygy.test.week4;

/**
 * Created by guoyao on 2017/9/23.
 */
public class SearchInsertPosition {
    public static void main(String[] args) {
        int[] nums={1, 2, 3, 4, 56, 467};
        System.out.println(searchInsert(nums, 5));
    }

    /**
     *Given a sorted array and a target value, return the index if the target is found.
     * If not, return the index where it would be if it were inserted in order.
     You may assume no duplicates in the array.
     Here are few examples.
     [1,3,5,6], 5 → 2
     [1,3,5,6], 2 → 1
     [1,3,5,6], 7 → 4
     [1,3,5,6], 0 → 0
     */

    public static int searchInsert(int[] nums, int target) {
        if (nums.length == 0) {
            return 0;
        }
        if (nums.length == 1 && target < nums[0]) {
            return 0;
        }
        int start = 0 , end = nums.length -1 ;
        int mid=(start + end) >> 1;
        while (start <= end) {
            if (nums[mid] > target) {
                end=mid - 1;
            } else if (nums[mid] < target) {
                start=mid + 1;
            } else {
                return mid  ;
            }
            mid =  (start + end ) >> 1 ;
        }
        return  mid + 1 ;

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章