LintCode 刷题

九章算法 Chapter7 两个指针

Remove Duplicate Numbers in Array
思路:删除
相向双指针

Window Sum:
given an array nums and a value k and a window, the window's size is k, move the window from the start, in each iteration, return the sum of the element between the window until the end.
Think:
两个Pointer to 表示window的starting point and ending point, for loop to iteration the window, and a for loop to calculate sum, until the end of the array

Move Zeros
given an array nums and moving all the 0 element to the end of the array and maintain relative order of non-zero elements
Think:create a same length result array and traverse the old array, if the element is zero, set the new array end is 0, if it not 0, move it to the first element.

Q:Valid Plaindrome
判断一个只含有 数字和字母的数组是否是回文的,空集算作回文

思路:
判断 first element == end element
if is 回文,if not 不是回文

Q:Rotate String
给一个字母数组和一个 offset值,将字母数组的后offset个元素位置翻转 abcd 3 bcda abcdefg 2 fgabcde

Two Sum 一个大类

Hashset
对于排序数组,不能够做到插入和查找 都是logn
Two Sum- Data structure design

Two Unique Pairs
找出一个数组中不重复加和为
任何指针移动都要注意 left < right 的问题

--Oct 4--
Search a 2D Matrix
Des: write an algorithm to search a value in a sorted 2D matrix, the smallest one is in the first row leftest, the largest one is in the last row rightest.
My solution: Binary Search -> think the matrix as an linear array

public class TwoDimentionMatrix {
    public static void main(String [] args){
          int[][] matrix = new int[2][3]; //declare a 2d matrix 2rows 3columns
        for (int i = 0; i < matrix.length; i++) {
            System.out.println(matrix.length); //matrix.length = number of rows
            for (int j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = i + j;
            }
        }
        System.out.println(Arrays.deepToString(matrix));
}

错误总结:

  1. binary search的三步:起点(mid) + while + 终点(any point)
    2.2d array -> 1d array [row*column / column] [row * column % column]
    3.the program must have a return value(the return value must out of the loop because this would avoid having no return line when the program does not enter the loop)

Oct 5
Maximum Number in Mountain Sequence (Accepted)
My solution:
1.先判断slope 为 + or -, 为+ 则直接返回最后一个值, 为- 则直接返回第一个值
2.如果为 先+后-, 则上坡 start 上移 start = mid + 1, 下坡 end 上移 end = mid - 1, 终点在最大值(该点 既大于左边 又大于右边)

_关于binary search 的终点判断_:
start <= end 对应于 start =  mid + 1 和 end = mid - 1
start + 1 < end 对应于 start = mid 和 end = mid


jiuzhang solution:
直接二分法,上坡 start上移, 下坡end 上移,最后返回两个值 nums[start], nums[end] 直接取二者中的更大的一个
while (start + 1 < end) {
            int mid = (start + end) / 2;
            if (nums[mid] > nums[mid + 1]) {
                end = mid;
            } else {
                start = mid;
            }
        }
        return Math.max(nums[start], nums[end]);

Maximum Depth of Binary Tree
Des: maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Think: recursive, 一直找非leaf的节点
错误总结:
1.divide and conquer 思路不清晰 仍然在判断左子树 和 右子树的情况,而不是专注于判断这一层的情况
2.不会使用提供的方法 maxDepth(TreeNode root) 只含有一个root 节点,判断这个节点是否为空节点,不是空节点 -> 一定包含子树-> height + 1
3.return的出口中包含计数的部分 return Math.max(left, right) + 1 这里每调用一次left or right 都会 +1表示含有子节点 表示height + 1


Oct 6

Search in a Big Sorted ArrayAccepted
Des: give a big sorted integer array(don't know the length), only give a method reader.get(k), to find a target value and return its first index in the array.
Think: use while loop to find the first element that bigger than the target value as the end point, then we can

错误总结

优化:
1.用下面的代码能够省去 check index = 0时的情况

int index = 1
while(reader.get(index - 1) > target)

replace-> 
int index = 1
if(reader.get(0) == target){
    return 0;
}
while(reader.get(index) > taget) 

2.可以将reader.get(mid) > target 与reader.get(mid) == reader.get(mid) 合并, 因为mid不一定是the first index,最后返回一个start 和一个end 判断取值 start = target/ end > target or start < target/end = target

if (reader.get(mid) < target){
    start = mid;
}
else{
    end = mid;
}
if(reader.get(start) == target) {
    return start;
}
if(reader.get(end) == target) {
    return end;
}

Oct 7

Find Minimum in Rotated Sorted Array(Accepted 但是没有用到Binary Search)
Des: 一个数组(不存在重复值)中间可能存在翻转的情况,找出这个数组中的最小值。 Give a "half" sorted array (rotated in someplace of the element), return the minimum value
Think: for 循环遍历一遍数组,找到那个既比左边小,又比右边小的数,则是最小值 数组分成两部分 分别用Binary search -> no need for that 只要循环一次,判断一个rotate point, 如果有这个point, 直接返回这个point + 1位置的值。
错误总结
1.数组循环终点 nums.length - 1 而不是 nums.length
2.注意return 语句不能全部都在if() 条件从句下

Oct 8
Find Peak Element (Accepted)
Des: give a array(at least 3 element + A[0] < A[1] && A[A.length - 2] > A[A.length - 1] , return the peek position P (A[P] > A[P - 1] && A[P] > A[P + 1]
Think: start: A[0] end: A[length - 1] target: A[mid + 1] or A[mid - 1]
My error:
1.没有注意返回的是 index, 直接返回 start 或 end, 而不需要返回 A[start] 或 A[end]

__ First Bad Version __:
Des: 一些提交版本中间出现了一个错误版本,导致后面的所有版本都出现了错误,找出这个错误的版本
Think: Binary Search 的target变成一个判断 True or False 的函数即可

My error:
将isBadVersion() 函数的逻辑理解错误
直接将(start + end) 会产生 Overflow 的结果 2147483647 + 1 overflow , click here for more information

Search in Rotated Sorted Array: (看过提示后 Accepted)
Des: 可能会rotated 的一个数组,找到一个target值,返回index, 如果找不到返回 -1
Think: 分为两类:左半边 - 右半边

Search for a Range(Accepted 参考了__Total Occurrence of Target__问题的九章答案):
Des: Given a sorted array of n integers, find the starting and ending position of a given target value. return int [ ]!!!
Think: find the first index of the target value, then find the last index of the target value, return [first, last]
My error:

  1. initialization of array error: declare an array int[] result = new int[2] -> initialize: result[0] = result[-1] = -1/ int[] result = {1,2};
posted @ 2017-09-29 09:56  AugusKong  阅读(375)  评论(0编辑  收藏  举报