大家好,我是
方圆
无它,唯手熟尔
文章目录
快慢指针遍历
141. 环形链表
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null)
return false;
ListNode slow = head,fast = head.next;
while(slow != fast){
if(fast == null || fast.next == null)
return false;
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}
202. 快乐数
class Solution {
public boolean isHappy(int n) {
int slow = n, fast = getNext(n);
while(fast != 1) {
if(slow == fast) return false;
slow = getNext(slow);
fast = getNext(getNext(fast));
}
return true;
}
private int getNext(int n) {
int sum = 0;
while(n != 0) {
int x = n % 10;
sum += x * x;
n /= 10;
}
return sum;
}
}
876. 链表的中间结点
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow = head, fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
区间合并
56. 合并区间
class Solution {
public int[][] merge(int[][] intervals) {
List<int[]> res = new ArrayList<>();
if(intervals == null || intervals.length == 0)
return res.toArray(new int[0][]);
Arrays.sort(intervals,(x,y) -> x[0] - y[0]);
for(int i = 0; i < intervals.length; i++) {
int left = intervals[i][0];
int right = intervals[i][1];
//注意此处是大于等于
while(i < intervals.length - 1 && right >= intervals[i + 1][0]) {
i++;
right = Math.max(right,intervals[i][1]);
}
res.add(new int[] {
left,right});
}
return res.toArray(new int[0][]);
}
}
字符串操作
6. Z字形变换
//技巧性挺强的
class Solution {
public String convert(String s, int numRows) {
if(numRows == 1) return s;
int len = Math.min(s.length(),numRows);
String[] rows = new String[len];
for(int i = 0; i < rows.length; i++) {
rows[i] = "";
}
boolean down = true;
int temp = 0;
for(int j = 0; j < s.length(); j++) {
if(down) rows[temp++] += s.substring(j,j + 1);
else rows[temp--] += s.substring(j,j + 1);
if(temp == 0 || temp == len - 1) down = !down;
}
String res = "";
for(String str : rows) res += str;
return res;
}
}
14. 最长公共前缀
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0) return "";
String res = strs[0];
for(int i = 1; i < strs.length; i++) {
int j = 0;
for(; j < res.length() && j < strs[i].length(); j++)
if(res.charAt(j) != strs[i].charAt(j)) break;
res = res.substring(0,j);
if(res.equals("")) return "";
}
return res;
}
}
763. 划分字母区间
//这个技巧性也挺强的
class Solution {
public List<Integer> partitionLabels(String S) {
List<Integer> res = new ArrayList<>();
int[] last = new int[26];
for(int i = 0; i < S.length(); i++) {
last[S.charAt(i) - 'a'] = i;
}
int head = 0,tail = 0;
for(int i = 0; i < S.length(); i++) {
tail = Math.max(tail,last[S.charAt(i) - 'a']);
if(i == tail) {
res.add(tail - head + 1);
head = tail + 1;
}
}
return res;
}
}
数字操作
7. 整数反转
//越界判断
class Solution {
public int reverse(int x) {
int res = 0;
int bundry = Integer.MAX_VALUE / 10;
while(x != 0) {
int n = x % 10;
x /= 10;
if(Math.abs(res) > bundry || (res == bundry && n > 7))
return 0;
res = res * 10 + n;
}
return res;
}
}
8. 字符串转换
class Solution {
public int myAtoi(String str) {
int res = 0;
str = str.trim();
if(str.length() == 0) return 0;
boolean flag = true;
int cur = 0;
if(str.charAt(0) == '-') {
flag = false;
cur = 1;
}else if(str.charAt(0) == '+') {
cur = 1;
}
int bundry = Integer.MAX_VALUE / 10;
for(int i = cur; i < str.length(); i++) {
char n = str.charAt(i);
if(n > '9' || n < '0') break;
if(res > bundry || (res == bundry && n > '7'))
return flag ? Integer.MAX_VALUE : Integer.MIN_VALUE;
res = res * 10 + (n - '0');
}
return flag ? res : -res;
}
}
9. 回文数
class Solution {
public boolean isPalindrome(int x) {
if(x < 0) return false;
int rex = 0,temp = x;
while(temp != 0) {
int n = temp % 10;
temp /= 10;
rex = rex * 10 + n;
}
if(rex == x) return true;
else return false;
}
}
43. 字符串相乘
class Solution {
public String multiply(String num1, String num2) {
if(num1.equals("0") || num2.equals("0")) return "0";
int[] res = new int[num1.length() + num2.length()];
for(int i = num1.length() - 1; i >= 0 ; i--) {
int n1 = num1.charAt(i) - '0';
for(int j = num2.length() - 1; j >= 0; j--) {
int n2 = num2.charAt(j) - '0';
int sum = res[i + j + 1] + n1 * n2;
res[i + j + 1] = sum % 10;
res[i + j] += sum / 10;
}
}
StringBuilder ans = new StringBuilder();
for(int i = 0; i < res.length; i++) {
if(i == 0 && res[0] == 0) continue;
ans.append(res[i]);
}
return ans.toString();
}
}
172. 阶乘后的零
class Solution {
public int trailingZeroes(int n) {
int count = 0;
while(n > 0){
count += n / 5;
n /= 5;
}
return count;
}
}
258. 各位相加
class Solution {
public int addDigits(int num) {
if(num < 10) return num;
int next = 0;
while(num != 0){
next += num % 10;
num /= 10;
}
return addDigits(next);
}
}
加油儿!!!