当前位置:网站首页>Leetcode face T10 (1-9) array, ByteDance interview sharing
Leetcode face T10 (1-9) array, ByteDance interview sharing
2022-07-02 02:09:00 【wqwq_ twenty-two】
Sparse array search . There's an ordered array of strings , It's interspersed with empty strings , Write a method , Find the position of a given string .
Example 1:
Input : words = [“at”, “”, “”, “”, “ball”, “”, “”, “car”, “”, “”,“dad”, “”, “”], s = “ta”
Output :-1
explain : There is no return -1.
Example 2:
Input :words = [“at”, “”, “”, “”, “ball”, “”, “”, “car”, “”, “”,“dad”, “”, “”], s = “ball”
Output :4
Tips :
words The length of is [1, 1000000] Between
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/sparse-array-search-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
stay Java To determine whether the string values are equal, use s1,equals(s1). Don't use it directly ==, It took a long time mmp.
class Solution {
// direct method
// public int findString(String[] words, String s) {
// for(int i =0;i <words.length;i++){
// if(words[i].equals(s)){
// return i;
// }
// }
// return -1;
// }
// Binary search
public int findString(String[] words, String s) {
// Direct binary search
int left = 0;
int right = words.length - 1;
//[left.right)
while(left <= right){
while(left < words.length && words[left].equals("")){
left++;
}
while(right >= 0 && words[right].equals("")){
right–;
}
int mid = left + (right - left) / 2;
while(mid < words.length && words[mid].equals("")){
mid++;
}
if(words[mid].equals(s)){
return mid;
}else if(words[mid].compareTo(s) > 0){
// Shrink the right border , Lock left boundary
right = mid - 1;
}else if(words[mid].compareTo(s) < 0){
// Shrink the left border , Lock the right boundary
left = mid + 1;
}
}
return -1;
}
}
Q10.9 Sort matrix search
Given M×N matrix , Every line 、 Each column is arranged in ascending order , Write code to find an element .
Example :
The existing matrix matrix as follows :
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false.
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/sorted-matrix-search-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0) {
return false;
}
int m = matrix.length, n = matrix[0].length, row = 0, col = n - 1;
while (row < m && col >= 0) {
if (matrix[row][col] < target) {
row++;
} else if(matrix[row][col] > target) {
col–;
} else {
return true;
}
}
return false;
}
}
Q10.10 Rank of digital stream
Suppose you are reading a string of integers . Every once in a while , You want to find out the numbers x The rank of ( Less than or equal to x The number of values of ). Please implement data structures and algorithms to support these operations , in other words :
Realization track(int x) Method , This method is called every time a number is read in ;
Realization getRankOfNumber(int x) Method , Returns less than or equal to x The number of values of .
Be careful : This question is slightly changed from the original one
Example :
Input :
[“StreamRank”, “getRankOfNumber”, “track”, “getRankOfNumber”]
[[], [1], [0], [0]]
Output :
[null,0,null,1]
Tips :
x <= 50000
track and getRankOfNumber Methods are called no more than 2000 Time
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/rank-from-stream-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
int[] nums;
public StreamRank() {
nums = new int[50002];
}
public void track(int x) {
// Avoid for 0
++x;
for (int i = x; i <= nums.length - 1; i += lowBit(i))
nums[i]++;
}
public int getRankOfNumber(int x) {
int sum = 0;
++x;
for (int i = x; i > 0; i -= lowBit(i))
sum += nums[i];
return sum;
}
public int lowBit(int x) {
return x & (-x);
}
Q10.11 Peak and valley
In an array of integers ,“ peak ” Is an element greater than or equal to an adjacent integer , Accordingly ,“ valley ” Is an element less than or equal to an adjacent integer . for example , In the array {5, 8, 4, 2, 3, 4, 6} in ,{8, 6} It's the peak , {5, 2} It's the valley . Now, given an array of integers , Sort the array in alternating order of peaks and valleys .
Example :
Input : [5, 3, 1, 2, 3]
Output : [5, 1, 3, 2, 3]
Tips :
nums.length <= 10000
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/peaks-and-valleys-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
greedy , Refer to the explanation of the question :
(1) If i Is the position of the peak , Then judge whether the current position is less than the previous position ( The former is Gu ), If less than , The exchange , If it is greater than, it will not be processed . namely : if(nums[i]<nums[i-1]) swap(nums[i],nums[i-1]);
(2) If i For the location of the valley , Then judge whether the current position is greater than the previous position ( The previous one is the peak ), If more than , The exchange , If it is greater than, it will not be processed . namely : if(nums[i]>nums[i-1]) swap(nums[i],nums[i-1]);
public void wiggleSort(int[] nums) {
/* Sort the array O(nlogn)
int[] res = Arrays.copyOf(nums, nums.length);
Arrays.sort(res);
int index = res.length - 1;
for(int i = 0;i < res.length;i += 2) {
nums[i] = res[index–];
}
for(int i = 1;i < res.length;i += 2) {
nums[i] = res[index–];
}
*/
// Direct traversal O(n)
for(int i = 1;i < nums.length;i++) {
Review the interview notes :
This information I started with spring recruitment , I'll put all the blogs 、 Forum . High quality... On the website Android Develop advanced interview questions and collect them , Then the whole network looks for the best solution . Every interview question is 100% real one + Optimal solution . The context of knowledge + A lot of details .
Save everyone's time in searching materials on the Internet to learn , You can also share it with your friends and learn together .
Leave a little praise for the article , You can get it for free ~
Poke me for :GitHub
《960 page Android Development Notes 》
《1307 page Android Develop interview tips 》
Including Tencent 、 Baidu 、 millet 、 Ali 、 Letv 、 Meituan 、58、 Cheetah 、360、 Sina 、 Sohu and other first-line Internet companies were asked questions in the interview . Being familiar with the knowledge listed in this article will greatly increase the probability of passing the first two rounds of technical interview .
《507 page Android Development related source code analysis 》
As long as the programmer is , Whether it's Java still Android, If you don't read the source code , Just look at API file , It's just a matter of skin , This is not conducive to the establishment and completion of our knowledge system and the improvement of actual combat technology .
B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)**
《960 page Android Development Notes 》
[ Outside the chain picture transfer in …(img-kEEjSeXJ-1644918789060)]
《1307 page Android Develop interview tips 》
Including Tencent 、 Baidu 、 millet 、 Ali 、 Letv 、 Meituan 、58、 Cheetah 、360、 Sina 、 Sohu and other first-line Internet companies were asked questions in the interview . Being familiar with the knowledge listed in this article will greatly increase the probability of passing the first two rounds of technical interview .
[ Outside the chain picture transfer in …(img-0Q8Ytred-1644918789061)]
《507 page Android Development related source code analysis 》
As long as the programmer is , Whether it's Java still Android, If you don't read the source code , Just look at API file , It's just a matter of skin , This is not conducive to the establishment and completion of our knowledge system and the improvement of actual combat technology .
The real ability to exercise is to directly read the source code , Not limited to reading the source code of the major systems , It also includes a variety of excellent open source libraries .
边栏推荐
- Sword finger offer 62 The last remaining number in the circle
- How to solve MySQL master-slave delay problem
- Is the knowledge of University useless and outdated?
- D discard the virtual recovery method
- * and & symbols in C language
- 【C#】使用正则校验内容
- The difference between new and malloc
- New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
- JMeter (II) - install the custom thread groups plug-in
- There are spaces in the for loop variable in the shell -- IFS variable
猜你喜欢
How to execute an SQL in MySQL
Number of palindromes in C language (leetcode)
Volume compression, decompression
What is the MySQL column to row function
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
Webgpu (I): basic concepts
Medical management system (C language course for freshmen)
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
【毕业季】研究生学长分享怎样让本科更有意义
MySQL中一条SQL是怎么执行的
随机推荐
Post infiltration flow encryption
leetcode2312. Selling wood blocks (difficult, weekly race)
What are the skills of spot gold analysis?
If you want to rewind the video picture, what simple methods can you use?
JMeter (I) - download, installation and plug-in management
Using mongodb in laravel
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
Construction and maintenance of business websites [15]
1217 supermarket coin processor
Based on configured schedule, the given trigger will never fire
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
[question] - why is optical flow not good for static scenes
leetcode2312. 卖木头块(困难,周赛)
JPM 2021 most popular paper released (with download)
Pytest testing framework
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
RTL8189FS如何关闭Debug信息
【深度学习】infomap 人脸聚类 facecluster