字节常见面试题来源:codeTop
无重复字符的最长子串
题目描述:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
解题思路:滑动窗口,set判定重复,滑动窗口左右边界的移动
class Solution {
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<>();
int right = -1;
int res = 0;
for(int left = 0; left < s.length(); left++) {
if(left != 0) {
set.remove(s.charAt(left - 1));
}
while(right + 1 < s.length() && !set.contains(s.charAt(right + 1))) {
right++;
set.add(s.charAt(right));
}
res = Math.max(res, right - left + 1);
}
return res;
}
}
K个一组翻转链表
题目描述:https://leetcode-cn.com/problems/reverse-nodes-in-k-group/
解题思路:见代码注释
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
//添加头节点
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;//翻转部分的前一个节点
ListNode end = dummy;//翻转部分的最后一个节点
//翻转链表,表内头节点start尾节点end
//,表外头节点pre表外尾节点next
while(end.next != null) {
for(int i = 0;i < k && end != null; i++ ) end = end.next;
if(end == null) break;
ListNode start = pre.next;
ListNode next = end.next;//翻转部分前一个节点
end.next = null;
pre.next = reverse(start);
start.next = next;
pre = start;
}
return dummy.next;
}
/**
* 牢记这个翻转
* @param start
* @return
*/
public ListNode reverse(ListNode start) {
ListNode pre = null;
ListNode cur = start;
while(cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
翻转链表
题目描述:https://leetcode-cn.com/problems/reverse-linked-list/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while(cur != null){
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
三数之和
题目描述:https://leetcode-cn.com/problems/3sum/
解题思路:1.前两个指针遍历 2.跳过重复数 3. 双指针减少数组运算复杂度
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
if(nums == null || nums.length == 1|| nums.length ==2 ) return list;
Arrays.sort(nums);
for(int i = 0; i < nums.length; ++i) {
if(i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int k = nums.length - 1;
for(int j = i + 1; j < nums.length; ++j) {
if(j > i + 1 && nums[j] == nums[j -1]) continue;
while(j < k && nums[i] + nums[j] + nums[k] > 0) {
--k;
}
if(j == k) break;
if(nums[i] + nums[j] + nums[k] == 0) {
List<Integer> addlist = new ArrayList<Integer>();
addlist.add(nums[i]);
addlist.add(nums[j]);
addlist.add(nums[k]);
list.add(addlist);
}
}
}
return list;
}
}
数组中的第k个最大元素
题目描述:https://leetcode-cn.com/problems/kth-largest-element-in-an-array/
class Solution {
public int findKthLargest(int[] nums, int k) {
if(nums == null) return -1;
if(nums.length < k ) return -1;
Arrays.sort(nums);
int right = nums.length;
for(int i = 0;i < k ;i++) {
right--;
}
return nums[right];
}
}
买卖股票的最佳时机
题目描述:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
class Solution {
public int maxProfit(int[] prices) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for(int i = 0; i < prices.length; i++) {
if(prices[i] < minprice) {
minprice = prices[i];
}else if( prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
}