大奋
算法一坨,学习一下,主要跟着这个仓库刷。
leetcode 27 移除元素
前面写过了,简简又单单
class Solution {
public int removeElement(int[] nums, int val) {
int length = 0;
for (int i = 0; i < nums.length; i ++) {
if (nums[i] != val) {
nums[length ++] = nums[i];
}
}
return length;
}
}
leetcode 344 反转字符串
前面写过了,简简又单单
class Solution {
public void reverseString(char[] s) {
int left = 0, right = s.length - 1;
char tmp;
while (left < right) {
tmp = s[left];
s[left] = s[right];
s[right] = tmp;
left ++;
right --;
}
}
}
KarmaCoder 54 替换数字
前面写过了,简简又单单
import java.util.Scanner;
public class Main {
public static String replaceNumbers(String s) {
int length = 0;
for (int i = 0; i < s.length(); i ++) {
length += Character.isDigit(s.charAt(i)) ? 6 : 1;
}
char[] t = new char[length];
int j = 0;
for (int i = 0; i < s.length(); i ++) {
if (Character.isDigit(s.charAt(i))) {
t[j ++] = 'n';
t[j ++] = 'u';
t[j ++] = 'm';
t[j ++] = 'b';
t[j ++] = 'e';
t[j ++] = 'r';
}
else {
t[j ++] = s.charAt(i);
}
}
return new String(t);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
System.out.println(replaceNumbers(s));
scanner.close();
}
}
leetcode 151 反转字符串中的单词
初生题,并非简单
class Solution {
public String reverseWords(String s) {
char[] t = s.toCharArray();
int left = 0, right = t.length - 1, i = 0, j = 0, a, b;
char tmp;
// 翻转整个字符串
while (left < right) {
tmp = t[left];
t[left] = t[right];
t[right] = tmp;
left ++;
right --;
}
left = 0;
right = 0;
while (right < t.length) {
// 找到一个单词的开头
while (left < t.length) {
if (left == 0) {
if (t[left] != ' ') {
left = 0;
break;
}
else {
left ++;
}
}
else if (t[left - 1] == ' ' && t[left] != ' ') {
break;
}
else {
left ++;
}
}
// 找到一个单词的结尾
right = left;
while (right < t.length) {
if (right == t.length - 1 && t[right] != ' ') {
break;
}
else if (right + 1 < t.length && t[right + 1] == ' ' && t[right] != ' ') {
break;
}
else {
right ++;
}
}
// 翻转单词并拷贝到开头
if (left < t.length) {
a = left;
b = right;
while (a < b) {
tmp = t[a];
t[a] = t[b];
t[b] = tmp;
a ++;
b --;
}
for (j = left; j <= right; j ++) {
t[i ++] = t[j];
}
// 单词结尾要加空格
if (right == t.length - 1) {
break;
}
else if (right < t.length) {
t[i ++] = ' ';
}
left = right + 1;
}
}
// 如果最后一个是空格,就去掉
if (t[i - 1] == ' ') {
i --;
}
return new String(t).substring(0, i);
}
}
leetcode 206 反转链表
之前写过了,think一下就行。就是要把新链表的head记下来,再把旧链表的头记下来。最后从旧链表的头取出一个节点,插到新链表的头部,完成反转。注意更新两个链表的头节点就行。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode p1 = head, p2 = head;
head = null;
while (p1 != null) {
p2 = p1.next;
p1.next = head;
head = p1;
p1 = p2;
}
return head;
}
}
leetcode 19 删除链表的倒数第 N 个结点
之前做过,但是忘了可以加虚拟头节点来简化了。这样写的会麻烦点
走了不到n步,说明没有倒数第n个节点,删不了
走了n步,说明head就是要删的节点
走了n+1步,说明当前head的下一个节点就是倒数第n个节点,最后head和slow一起往后走就行
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if (head == null) {
return null;
}
ListNode fast = head, slow = null;
int i;
for (i = 0; i < n + 1 && fast != null; i ++) {
fast = fast.next;
}
if (i < n) {
return head;
}
else if (i == n) {
return head.next;
}
else {
slow = head;
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
}
}
这个是插虚拟头节点的,简洁很多:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyHead = new ListNode(-1, head);
ListNode fast = head, slow = dummyHead;
while (n > 0 && fast != null) {
fast = fast.next;
n --;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummyHead.next;
}
}
leetcode 面试题 02.07 链表相交
双指针:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p1 = headA, p2 = headB;
int length1 = 0, length2 = 0;
while (p1 != null && p2 != null) {
p1 = p1.next;
p2 = p2.next;
}
while (p1 != null) {
p1 = p1.next;
length1 ++;
}
while (p2 != null) {
p2 = p2.next;
length2 ++;
}
p1 = headA;
p2 = headB;
while (length1 > 0) {
p1 = p1.next;
length1 --;
}
while (length2 > 0) {
p2 = p2.next;
length2 --;
}
while (p1 != p2) {
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
}
官方的题解有点非人类了,相当于两个指针都遍历一遍两个链表,最后会在交点/null相遇,此时返回就行。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p1 = headA, p2 = headB;
while (p1 != p2) {
p1 = p1 == null ? headB : p1.next;
p2 = p2 == null ? headA : p2.next;
}
return p1;
}
}
leetcode 142 环形链表||
哈希方法:判断有没有在哈希表中出现过就行
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
Set<ListNode> set = new HashSet<ListNode>();
while (head != null) {
if (set.contains(head)) {
return head;
}
set.add(head);
head = head.next;
}
return null;
}
}
双指针:
fast如果走不了两步,说明链表到头了,没有环,需要立刻返回。
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head, ptr = head;
while (fast != null) {
slow = slow.next;
if (fast.next == null) {
return null;
}
else {
fast = fast.next.next;
}
if (slow == fast) {
while (ptr != slow) {
ptr = ptr.next;
slow = slow.next;
}
return ptr;
}
}
return null;
}
}
leetcode 15 三数之和
之前写过了,再写一遍确实能加深印象,并且发现之前没懂的部分。比如为什么要判断并跳过,而不是用while直接跳过呢?因为这样就会去掉重复元素的和为k的情况,正确的逻辑应该是对当前元素做三数之和判断后,下一个重复的才要去掉。
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
System.out.println(nums);
int n = nums.length;
List<List<Integer>> results = new ArrayList<>();
for (int i = 0; i < n; i ++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int k = n - 1;
for (int j = i + 1; j < n; j ++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
while (k > j && nums[i] + nums[j] + nums[k] > 0) {
k --;
}
if (k == j) {
break;
}
if (nums[i] + nums[j] + nums[k] == 0) {
results.add(List.of(nums[i], nums[j], nums[k]));
}
}
}
return results;
}
}
leetcode 18 四数之和
不写了,和三数之和是完全一样的,多一层循环就可以。
1856




被折叠的 条评论
为什么被折叠?



