Lrtwjhs's Blog


  • 首页

  • 归档

  • 标签

  • 分类

  • 项目

  • 关于

LeetCode 26. Remove Duplicates from Sorted Array

发表于 2020-12-24 | 分类于 算法 |

题目链接:
https://leetcode.com/problems/remove-duplicates-from-sorted-array/

思路:
使用快慢指针来记录遍历的坐标,最开始时两个指针都指向第一个数字,如果两个指针指的数字相同,则快指针向前走一步,如果不同,则两个指针都向前走一步,这样当快指针走完整个数组后,慢指针当前的坐标加1就是数组中不同数字的个数。

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length <= 1) return nums.length;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}

LeetCode 20. Valid Parentheses

发表于 2020-12-24 | 分类于 算法 |

题目链接:
https://leetcode.com/problems/valid-parentheses/

思路:
用一个栈,开始遍历输入字符串,如果当前字符为左半边括号时,则将其压入栈中,如果遇到右半边括号时,若此时栈为空,则直接返回 false,如不为空,则取出栈顶元素,若为对应的左半边括号,则继续循环,反之返回 false。

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
// 如果是左括号,则入栈
if (ch == '(' || ch == '[' || ch == '{') {
stack.push(ch);
} else { // 如果是右括号,则比较其与栈顶元素是否配对
if (stack.isEmpty()) {
return false;
}
if (ch == ')' && stack.peek() != '(') {
return false;
}
if (ch == ']' && stack.peek() != '[') {
return false;
}
if (ch == '}' && stack.peek() != '{') {
return false;
}
stack.pop();
}
}
// 最后栈为空则表示完全匹配完毕
return stack.isEmpty();
}
}

LeetCode 21. Merge Two Sorted Lists

发表于 2020-12-24 | 分类于 算法 |

题目链接:
https://leetcode.com/problems/merge-two-sorted-lists/

思路:
递归的写法,当某个链表为空了,就返回另一个。然后核心还是比较当前两个节点值大小,如果 l1 的小,那么对于 l1 的下一个节点和 l2 调用递归函数,将返回值赋值给 l1.next,然后返回 l1;否则就对于 l2 的下一个节点和 l1 调用递归函数,将返回值赋值给 l2.next,然后返回 l2。

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 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 mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
ListNode head = (l1.val < l2.val) ? l1 : l2;
ListNode nonhead = (l1.val < l2.val) ? l2 : l1;
head.next = mergeTwoLists(head.next, nonhead);
return head;
}
}

LeetCode 14. Longest Common Prefix

发表于 2020-12-24 | 分类于 算法 |

题目链接:
https://leetcode.com/problems/longest-common-prefix/

思路:
将单词上下排好,则相当于一个各行长度有可能不相等的二维数组,遍历顺序和一般的横向逐行遍历不同,而是采用纵向逐列遍历,在遍历的过程中,如果某一行没有了,说明其为最短的单词,因为共同前缀的长度不能长于最短单词,所以此时返回已经找出的共同前缀。

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
String pre = strs[0];
int i = 1;
while (i < strs.length){
while (strs[i].indexOf(pre) != 0)
pre = pre.substring(0, pre.length() - 1);
i++;
}
return pre;
}
}

LeetCode 9. Palindrome Number

发表于 2020-12-23 | 分类于 算法 |

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Follow up: Could you solve it without converting the integer to a string?

阅读全文 »

LeetCode 13. Roman to Integer

发表于 2020-12-23 | 分类于 算法 |

题目链接:
https://leetcode.com/problems/roman-to-integer/

思路:
用HashMap类,用字符型作为key,其对应的数为value。当一个字符代表的数小于其后一个字符代表的数时,这个数等于后一个字符代表的数减去该字符代表的数,否则等于二者相加。用HashMap是因为不需要思考如何遍历,直接找到Key就行。

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int romanToInt(String s) {
int ans = 0;
Map < Character, Integer > map = new HashMap < > ();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
int i = 0;
while(i < s.length()) {
if((i + 1 < s.length()) && map.get(s.charAt(i + 1)) > map.get(s.charAt(i))) {
ans += map.get(s.charAt(i + 1)) - (int) map.get(s.charAt(i));
i += 2;
} else {
ans += map.get(s.charAt(i));
i++;
}
}
return ans;
}
}

LeetCode 1. Two Sum

发表于 2020-11-10 | 分类于 算法 |

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

阅读全文 »

LeetCode 2. Add Two Numbers

发表于 2020-11-10 | 分类于 算法 |

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

阅读全文 »

LeetCode 7. Reverse Integer

发表于 2020-11-10 | 分类于 算法 |

Given a 32-bit signed integer, reverse digits of an integer.

Note:
Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

阅读全文 »

HTTP状态码

发表于 2018-09-05 | 分类于 协议 |

HTTP状态码

当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求。当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应浏览器的请求。

HTTP状态码的英文为HTTP Status Code,是用以表示网页服务器HTTP响应状态的3位数字代码。它由 RFC 2616 规范定义的,并得到RFC 2518、RFC 2817、RFC 2295、RFC 2774、RFC 4918等规范扩展。

HTTP状态码的官方注册表由互联网号码分配局(Internet Assigned Numbers Authority)维护。

微软互联网信息服务 (Microsoft Internet Information Services)有时会使用额外的十进制子代码来获取更多具体信息,但是这些子代码仅出现在响应有效内容和文档中,而不是代替实际的HTTP状态代码。

阅读全文 »
123
Rutao Li

Rutao Li

要么旅行,要么读书,身体和灵魂总要一个在路上.

29 日志
5 分类
9 标签
RSS
© 2015 — 2021 Rutao Li
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.3