0. 常规操作
1. 部分数组复制 Arrays.copyOfRange(nums,start,end) // 包含开始元素 不包含结尾元素
2. 字符串数组复制:String[] strs = {"flower","flow","flight"};
3. 将整型转化为字符串: String str = String.valueOf(n);
4. 将char转化为int: int c = str.charAt(i)-'0';
5. 字符串转化为字符数组: char[] nums = str.toCharArray();
6. 截取字符串: String a = s.substring(index,index+i); 不含末尾元素
7. 将字符串转为整形: Integer.valueOf(s) > 255
8. 获取字符串某一个字符 char ch = str.charAt(j)
9. 比较字符串大小(求k个高频单词) words[i].compareTo(words[j])<0
10. Java转换大小写之toLowerCase()和toUpperCase()
11. char转化int:s=String.valueOf(c);i=Integer.parseInt(s);
1. 链表
83. 删除排序链表中的重复元素
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null)
return head;
ListNode dump = new ListNode(-1);
ListNode q = head; // 慢指针
dump.next = q;
ListNode p = head.next; // 快指针
int a = head.val;
while(p!=null){
if(a != p.val){
q.next = p;
a = p.val; // 存值方便下一次再比较
q = p; // 慢指针也要往下一个位置
p = p.next;
}else{
p = p.next;
}
}
q.next = null;
return dump.next;
}
}
203. 移除链表元素
/**
* 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 removeElements(ListNode head, int val) {
if(head == null)
return head;
ListNode dump = new ListNode(-1);
dump.next = null;
ListNode q = dump; // q慢指针
ListNode p = head; // p快指针
while(p != null){
if(p.val != val){
q.next = p;
q = p;
p = p.next;
}else{
p = p.next;
}
}
q.next = null;
return dump.next;
}
}
2. 二叉树
leetcode108. 将有序数组转换为二叉搜索树
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if(nums.length == 0)
return null;
int i = 0;
int j = nums.length;
int k = (i+j)/2;
int a = nums[k];
TreeNode root = new TreeNode(a);
root.left = sortedArrayToBST(Arrays.copyOfRange(nums,i,k));
root .right = sortedArrayToBST(Arrays.copyOfRange(nums,k+1,j));
return root;
}
}
111. 二叉树的最小深度
class Solution {
public int minDepth(TreeNode root) {
if(root == null)
return 0;
if(root.left== null && root.right == null)
return 1;
int res = Integer.MAX_VALUE;
if(root.left != null)
res = Math.min(minDepth(root.left),res);
if(root.right != null)
res = Math.min(minDepth(root.right),res);
return res+1;
}
}
113. 路径总和 II
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new LinkedList<>();
List<Integer> list = new LinkedList<>();
if(root == null)
return res;
dfs(res,root,targetSum,list);
return res;
}
public void dfs(List<List<Integer>> res,TreeNode root, int targetSum, List<Integer> list){
if(root == null)
return;
list.add(root.val);
if(root.left==null && root.right== null&& targetSum == root.val){
List<Integer> list1 = new LinkedList<>(list);
res.add(list1);
return;
}
if(root.left != null){
dfs(res,root.left,targetSum-root.val,list);
list.remove(list.size()-1);
}
if(root.right != null){
dfs(res,root.right,targetSum-root.val,list);
list.remove(list.size()-1);
}
}
}
543. 二叉树的直径
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int res;
public int diameterOfBinaryTree(TreeNode root) {
if(root == null)
return 0;
dfs(root);
return res;
}
public int dfs(TreeNode root){
if(root == null)
return 0;
int l = dfs(root.left);
int r = dfs(root.right);
if(l + r > res){
res = l + r;
}
return Math.max(l,r) + 1;
}
}
3. 动态规划
45. 跳跃游戏 II
class Solution {
public int jump(int[] nums) {
// int[] dp = new int[nums.length];
// 此题并不是用动态规划而是贪心算法思想
int res = 0;
int maxPosition = 0;
int end = 0;
for(int i = 0; i < nums.length; i++){
maxPosition = Math.max(maxPosition,i+nums[i]);
if(end >= nums.length-1){ //越界不用再跳了
break;
}
if(i == end){
res++;
end = maxPosition;
}
}
return res;
}
}
96. 不同的二叉搜索树
class Solution {
public int numTrees(int n) {
int[] res = new int[n+1];
res[0] = 1;
res[1] = 1;
for(int i = 2; i <= n; i++){
int sum = 0;
for(int j = 1; j <= i; j++){
sum += res[j-1]*res[i-j];
}
res[i] = sum;
}
return res[n];
}
}
3 最长回文子串(动态规划或者暴力)
class Solution {
public String longestPalindrome(String s) {
if(s.length() == 1)
return s;
char[] str = s.toCharArray();
int l = s.length();
boolean[][] dp = new boolean[l][l];
for(int i = 0; i < s.length(); i++){
dp[i][i] = true;
}
int max = 1;
int start = 0;
for(int i = 0; i < l-1; i++){
for(int j = 1; j < l; j++){
if(str[i] != str[j]){
dp[i][j] = false;
}else{
if(j - i < 3)
dp[i][j] = true;
else
dp[i][j] = dp[i+1][j-1];
}
if(dp[i][j] == true && j-i+1> max){
start = i;
max =j-i+1;
}
}
}
return s.substring(start,start + max);
}
}
4. 深度遍历与回溯
93. 复原 IP 地址
// Dfs
class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new LinkedList<>();
dfs(0,0,res,"",s);
return res;
}
public void dfs(int index, int count, List<String> res, String str, String s){
// if(count > 4) return;
if(count == 4 && index == s.length()){
res.add(str);
}
for(int i = 1; i < 4; i++){
if(index + i > s.length())
return;
String a = s.substring(index,index+i);
if(a.startsWith("0") && a.length() > 1 || Integer.valueOf(a) > 255)
return;
dfs(index+i, count+1, res, str+a+(count==3?"":"."), s);
}
}
}
131. 分割回文串
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new LinkedList<>();
List<String> list = new LinkedList<>();
backtracking(res,list,s,0);
return res;
}
public void backtracking( List<List<String>> res, List<String> list,String s,int index){
if(index == s.length()){
List<String> list1 = new LinkedList<>(list);
res.add(list1);
}
for(int i = index; i <s.length();i++){
String ss = s.substring(index,i+1);
if(!check(ss)){
continue;
}
list.add(ss);
backtracking(res,list,s,i+1);
list.remove(list.size()-1);
}
}
public boolean check(String str){
if (str == null || str.length() <= 1) {
return true;
}
int i = 0;
int j = str.length()-1;
while(i <= j){
if(str.charAt(i) == str.charAt(j)){
i++;
j--;
}else{
return false;
}
}
return true;
}
}
方法二:
47. 全排列 II (去掉重复元素)
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
boolean[] p = new boolean[nums.length];
Arrays.sort(nums);
for(int i = 0; i < nums.length; i++){
p[i] = false;
}
backtracking(nums,res,list,p);
return res;
}
public void backtracking(int[] nums, List<List<Integer>> res,List<Integer> list1, boolean[] p){
if(list1.size()==nums.length){
List<Integer> temp = new ArrayList<>(list1);
res.add(temp);
return;
}
for(int i = 0; i < nums.length; i++){
if(i != 0 && nums[i-1] == nums[i] && p[i-1] == true)
continue;
if(!p[i]){
list1.add(nums[i]);
p[i] = true;
backtracking(nums,res,list1,p);
list1.remove(list1.size()-1);
p[i] = false;
}
}
}
}
491. 递增子序列
class Solution {
public List<List<Integer>> findSubsequences(int[] nums) {
List<List<Integer>> res = new LinkedList<>();
List<Integer> list = new LinkedList<>();
backtracking(res,list,0,nums);
return res;
}
public void backtracking(List<List<Integer>> res,List<Integer> list,int index,int[] nums){
if(list.size() >= 2){
List<Integer> list1 = new LinkedList<>(list);
res.add(list1);
}
HashSet<Integer> set = new HashSet<>();
for(int i = index; i < nums.length; i++){
if(set.contains(nums[i])){
continue;
}
set.add(nums[i]);
if(list.size() == 0 || list.get(list.size()-1) <= nums[i]){
list.add(nums[i]);
backtracking(res,list,i+1,nums);
list.remove(list.size()-1);
}
}
}
}
5. 排序算法
leetcode 215. 数组中的第K个最大元素 (快速排序)
class Solution {
public int findKthLargest(int[] nums, int k) {
int kk = nums.length-k+1;
return sort(nums,kk,0,nums.length-1);
}
public int sort(int[] nums,int k,int star, int end){
int a = quickSort(nums,star,end);
if(k-1 == a){
return nums[a];
}else if( a > k-1){
return sort(nums,k,star,a-1);
}else{
return sort(nums,k,a+1,end);
}
}
public int quickSort(int[] arr, int start, int end){
int i = start;
int j = end;
int key = arr[i];
while (i < j){
while(i < j && arr[j] >= key){
j--;
}
arr[i] = arr[j];
while(i < j && arr[i] <= key){
i++;
}
arr[j] = arr[i];
}
arr[i] = key;
return i;
}
}
692. 前K个高频单词
class Solution {
public List<String> topKFrequent(String[] words, int k) {
List<String> res = new LinkedList<>();
HashMap<String,Integer> map = new HashMap<>();
HashSet<String> set = new HashSet<>();
for(String s:words){
if(map.containsKey(s)){
map.put(s,map.get(s)+1);
}else{
map.put(s,1);
}
}
for(int i = 0; i < words.length; i++){
for(int j = i+1; j < words.length; j++){
if(map.get(words[i]) > map.get(words[j])){
String temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
}
for(int i = 0; i < words.length; i++){
for(int j = i+1; j < words.length; j++){
if(map.get(words[i]) == map.get(words[j]) && words[i].compareTo(words[j])<0){
String temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
}
int count = 0;
for(int i = words.length-1; i >= 0; i--){
if(!set.contains(words[i])){
count++;
set.add(words[i]);
res.add(words[i]);
if(count == k)
break;
}else{
continue;
}
}
return res;
}
}
6. 字符串
二级标题
// An highlighted block
var foo = 'bar';
7. 双指针
leetcode 18. 四数之和
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new LinkedList<>();
if(nums.length < 4)
return res;
Arrays.sort(nums);
int length = nums.length;
for(int i = 0; i < nums.length-3; i++){
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for(int j = i+1; j < nums.length-2; j++){
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int left = j+1;
int right = nums.length-1;
while(left < right){
if(nums[i] + nums[j] + nums[left] + nums[right] == target){
List<Integer> list = new LinkedList<>();
list.add(nums[i]);
list.add(nums[j]);
list.add(nums[left]);
list.add(nums[right]);
res.add(list);
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
left++;
while (left < right && nums[right] == nums[right - 1]){
right--;
}
right--;
}else if(nums[i] + nums[j] + nums[left] + nums[right] > target){
right--;
}else{
left++;
}
}
}
}
return res;
}
}
华为面试:三色排序(不在是数组排序了而是列表)
package com.scw.test1;
import java.util.LinkedList;
import java.util.List;
public class ThreeColorSort {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(2);
list.add(1);
list.add(0);
list.add(2);
list.add(1);
list.add(0);
list.add(0);
list.add(1);
list.add(2);
Sort(list);
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
}
public static void Sort(List<Integer> list){
int i = 0;
int j = list.size()-1;
int k = 0;
while(i <= j && k <= j){ // k不能越界了
if(list.get(k) == 0){
int temp = list.get(i);
list.set(i,0);
list.set(k,temp);
i++;
if(temp != 0 && temp != 2){
k++;
}
}else if(list.get(k) == 2){
int temp = list.get(j);
list.set(j,2);
list.set(k,temp);
j--;
if(temp != 2 && temp != 0){
k++;
}
}else{ // 2 1 0 2 1 0 0 1 2
k++;
}
}
}
}
8 其他
8.1 字符串求和
leetcode 989 号算法题:数组形式的整数加法
leetcode 66 号算法题:加 1
leetcode 415 号算法题:字符串相加
leetcode 67 号算法题:二进制求和
class Solution {
public String addBinary(String a, String b) {
char[] aa = a.toCharArray();
char[] bb = b.toCharArray();
int[] res = new int[Math.max(aa.length,bb.length)+1];
int p = aa.length-1;
int q = bb.length-1;
int n = 0;
int flag = 0;
while(0 <= p && 0 <= q){
int m = (aa[p] - '0' ) + (bb[q] - '0') + flag;
if(m >= 2){
res[n] = m-2;
n++;
flag = 1;
p--;
q--;
}else{
res[n] = m;
n++;
flag = 0;
p--;
q--;
}
}
while(p>=0){
int i = aa[p] - '0'+ flag;
if(i >= 2){
res[n] = i-2;
n++;
flag = 1;
p--;
}else{
res[n] = i;
n++;
flag = 0;
p--;
}
}
while(q>=0){
int i = bb[q] - '0' + flag;
if(i >= 2){
res[n] = i-2;
n++;
flag = 1;
q--;
}else{
res[n] = i;
n++;
flag = 0;
q--;
}
}
if(flag ==1){
res[n] = 1;
}else{
res[n] = 0;
}
StringBuilder strs = new StringBuilder();
if(res[n] == 1){
strs.append(1);
}
for(int i = res.length -2; i >= 0; i--){
strs.append(res[i]);
}
return strs.toString();
}
}
leetcode 2 号算法题:两数相加