LeetCode (used to call i has 1337 code) is a social platform for preparing IT technical interviews. We strive to provide you with the best learning experience in preparing interviews for companies in the IT industry.
To be successful in a technical interview, we believe it is mainly repeating these three important steps:
Code. Read. Discuss.
We strive to provide you the LeetCode platform as the ultimate solution for preparing technical interviews.
1. Code -> Code solution using the Online Judge system.
2. Read -> Read high quality article featuring in-depth thought process. Also read other LeetCoders’ code.
3. Discuss -> Discuss your thoughts about the problem with other LeetCoders.
We hope that through our platform, you will grow into a LeetCoder. Not only will you be successful in all of your interviews, and most importantly, you will be a better coder in general !
This is Part 3 (Java Solution)
Part 1:
www.westca.com/Forums/...inese.html
Part 2:
www.westca.com/Forums/...inese.html
S算法问题: Spiral Matrix
问题描述:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
S算法问题: Search in Rotated Sorted Array
问题描述:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
算法问题推荐解法来了...
Binary search. O(lgn) eg. [4 5 6] -7- 8 1 2, 5 6 0 -1- [2 3 4]
解法(Java)
代码: |
public class SearchinRotatedSortedArray {
public int search(int[] A, int target) {
int low = 0;
int high = A.length - 1;
int mid = 0;
while (low <= high) {
mid = (low + high) / 2;
if (A[mid] == target)
return mid;
else {
if (A[low] <= A[mid]) {
if (target >= A[low] && target < A[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else {
if (target <= A[high] && target > A[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
}
return -1;
}
}
|
S算法问题: Search Insert Position
问题描述:
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
S算法问题: Set Matrix Zeroes
问题描述:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?