移除元素
class Solution {
public int removeElement(int[] nums, int val) {
int slow=0;
int fast=0;
for(fast=0;fast<nums.length;fast++)
{
if(nums[fast]!=val)
{
nums[slow++]=nums[fast];
}
}
return slow;
}
}
反转字符串
class Solution {
public void reverseString(char[] s) {
for(int left=0,right=s.length-1;left<=right;left++,right--)
{
char temp=s[left];
s[left]=s[right];
s[right]=temp;
}
}
}
替换空格
class Solution {
public String replaceSpace(String s) {
if(s==null) return null;
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)==' ')
{
sb.append("%20");
}
else{
sb.append(s.charAt(i));
}
}
return sb.toString();
}
}
反转字符串里的单词
class Solution {
public String reverseWords(String s) {
StringBuilder sb=removeSpace(s);
reverse(sb,0,sb.length()-1);
reverseEach(sb);
return sb.toString();
}
public StringBuilder removeSpace(String s)
{
int left=0;
int right=s.length()-1;
while(s.charAt(left)==' ') left++;
while(s.charAt(right)==' ') right--;
StringBuilder sb=new StringBuilder();
while(left<=right)
{
char c=s.charAt(left);
if(c!=' '||sb.charAt(sb.length()-1)!=' ')
{
sb.append(c);
}
left++;
}
return sb;
}
public void reverse(StringBuilder sb,int left,int right)
{
while(left<=right){
char temp=sb.charAt(left);
sb.setCharAt(left,sb.charAt(right));
sb.setCharAt(right,temp);
left++;
right--;
}
}
public void reverseEach(StringBuilder sb)
{
int start=0;
int end=1;
while(start<sb.length())
{
while(end<sb.length()&&sb.charAt(end)!=' ')
{
end++;
}
reverse(sb,start,end-1);
start=end+1;
end=start+1;
}
}
}
反转链表
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null) return null;
ListNode pre=null;
ListNode cur=head;
ListNode temp=null;
while(cur!=null)
{
temp=cur.next;
cur.next=pre;
pre=cur;
cur=temp;
}
return pre;
}
}
删除链表倒数第n个节点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null) return null;
ListNode dummyNode=new ListNode(0);
dummyNode.next=head;
ListNode slow=dummyNode;
ListNode fast=dummyNode;
for(int i=0;i<n;i++)
{
fast=fast.next;
}
while(fast.next!=null)
{
slow=slow.next;
fast=fast.next;
}
slow.next=slow.next.next;
return dummyNode.next;
}
}
链表相交
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode l1=headA;
ListNode l2=headB;
while(l1!=l2)
{
l1=l1==null?headB:l1.next;
l2=l2==null?headA:l2.next;
}
return l1;
}
//假设相交(相交点8->8):headA(1->2->3->8->8): 3+2;headB(7->6->8->8): 2+2。则 h1走3+2+2+2 = 9步(每一步访问:1->2->3->8->8->7->6->8->8); h2走2+2+2+3 = 9步(每一步访问:7->6->8->8->1->2->3->8->8)。 看最后两步。 不相交最终满足 h1 == h2 == null
}
环形链表
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null) return null;
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null)
{
slow=slow.next;
fast=fast.next.next;
if(slow==fast)
{
ListNode x=head;
ListNode y=slow;
while(x!=y)
{
x=x.next;
y=y.next;
}
return x;
}
}
return null;
}
}
有效的括号
class Solution {
public boolean isValid(String s) {
char[] m=s.toCharArray();
Stack<Character> stack=new Stack<>();
for(int i=0;i<m.length;i++)
{
if(m[i]=='{')
{
stack.push('}');
}
else if(m[i]=='[')
{
stack.push(']');
}
else if(m[i]=='(')
{
stack.push(')');
}
else if(stack.isEmpty()||stack.peek()!=m[i])
{
return false;
}
else{
stack.pop();
}
}
return stack.isEmpty();
}
}
删除字符串相邻重复项
class Solution {
public String removeDuplicates(String s) {
//Stack<Character> m=new Stack<>();
char[] m=s.toCharArray();
Stack<Character> stack=new Stack<>();
for(int i=0;i<m.length;i++)
{
if(!stack.isEmpty()&&m[i]==stack.peek())
{
stack.pop();
}
else{
stack.push(m[i]);
}
}
String str="";
while(!stack.isEmpty())
{
str=stack.pop()+str;
}
return str;
}
}
逆波兰表达式求值
class Solution {
public int evalRPN(String[] tokens) {
int n=tokens.length;
Stack<Integer> stack=new Stack<>();
for(int i=0;i<n;i++)
{
String token=tokens[i];
if(isNumber(tokens[i]))
{
stack.push(Integer.parseInt(token));
}
else{
int nums2=stack.pop();
int nums1=stack.pop();
switch(tokens[i])
{
case "+":
stack.push(nums1+nums2);
break;
case "-":
stack.push(nums1-nums2);
break;
case "*":
stack.push(nums1*nums2);
break;
case "/":
stack.push(nums1/nums2);
break;
}
}
}
return stack.peek();
}
public boolean isNumber(String token)
{
return !(token.equals("+")||token.equals("/")||token.equals("*")||token.equals("-"));
//return !("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token));
}
}
滑动窗口最大值
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
Deque<Integer> queue=new ArrayDeque<>();
int n=nums.length;
int[] res=new int[n-k+1];
int idx=0;
for(int i=0;i<nums.length;i++)
{
while(!queue.isEmpty()&&queue.peek()<(i-k+1))
{
queue.poll();
}
while(!queue.isEmpty()&&nums[queue.peekLast()]<nums[i])
{
queue.pollLast();
}
queue.offer(i);
if(i>=k-1)
{
res[idx++]=nums[queue.peek()];
}
}
return res;
}
}
前k个高频元素(Stream流)
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
return map.entrySet()
.stream()
.sorted((m1, m2) -> m2.getValue() - m1.getValue())
.limit(k)
.mapToInt(Map.Entry::getKey)
.toArray();
}
}