两两交换链表中的节点
package com.huawei.main;
import java.util.ArrayList;
import java.util.List;
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// 3.打印某个链表
List<Integer> s1 = new ArrayList<>();
List<Integer> s2 = new ArrayList<>();
ListNode head1 = new ListNode(-1,l1); // 手动添加头节点,这里打印第一个链表
ListNode p1 = head1;
while (p1.next != null){
p1 = p1.next;
s1.add(p1.val);
}
ListNode head2 = new ListNode(-1,l2); // 手动添加头节点,这里打印第一个链表
ListNode p2 = head2;
while (p2.next != null){
p2 = p2.next;
s2.add(p2.val);
}
List<Integer> s3 = new ArrayList<>();
int len = Math.max(s1.size(), s2.size());
int post=0;
int i=0;
while (i<len || post >0){
int i1=0,i2=0;
if(i<s1.size()){
i1 = s1.get(i);
}
if(i<s2.size()){
i2 = s2.get(i);
}
int val = (i1+i2+post)%10;
post= (i1+i2+post)/10;
s3.add(val);
i++;
}
ListNode ln3 = null;
for (int k=s3.size()-1;k >= 0;k--) {
ln3 = new ListNode(s3.get(k), ln3);
}
return ln3;
}
public static void main(String[] args) {
ListNode ln3 = new ListNode(3);
ListNode ln2 = new ListNode(4,ln3);
ListNode ln1 = new ListNode(2,ln2);
ListNode lt3 = new ListNode(4);
ListNode lt2 = new ListNode(6,lt3);
ListNode lt1 = new ListNode(5,lt2);
ListNode ltx = new Solution().addTwoNumbers(lt1,ln1);
// 3.打印某个链表
ListNode head = new ListNode(-1,ltx); // 手动添加头节点,这里打印第一个链表
ListNode p = head; // 辅助指针p,遍历链表
while (p.next != null){ // 注意while是先走到该节点,再打印该节点的值,只适用含头节点
p = p.next;
System.out.println(p.val);
}
}
}
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
#######################################################################################
19. 删除链表的倒数第 N 个结点
/**
* 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) {
head = new ListNode(-1,head);
int len = 0;
ListNode ln = head;
while (ln.next != null) {
ln = ln.next;
++len;
}
int del = len - n + 1;
ListNode delln = head;
int lenx = 0;
while (delln.next != null) {
if (del == lenx+1) {
if(delln.next.next !=null){
delln.next = delln.next.next;
}else{
delln.next =null;
}
}else{
delln = delln.next;
}
++lenx;
}
return head.next;
}
}
#######################################################################################
请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/valid-sudoku
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public boolean isValidSudoku(char[][] board) {
boolean rel = true;
int hlen = board.length;
int slen = board[0].length;
if(hlen != 9 || slen !=9){
return false;
}
Map key = new HashMap<>();
for (int j = 0; j < hlen; j++) {
for (int i = 0; i < hlen; i++) {
if (board[i][j]== '.') {
continue;
}
if (key.containsKey(board[i][j])) {
rel = false;
break;
} else {
key.put(board[i][j], "");
}
}
key.clear();
}
for (int i = 0; i < slen; i++) {
for (int j = 0; j < slen; j++) {
if (board[i][j] == '.') {
continue;
}
if (key.containsKey(board[i][j])) {
rel = false;
break;
} else {
key.put(board[i][j], "");
}
}
key.clear();
}
int[][] num = {{3,3},{6,3},{9,3},{3,6},{6,6},{9,6},{3,9},{6,9},{9,9}};
for(int x = 0; x < num.length;x++) {
for (int i = num[x][0]-3; i <num[x][0]; i++) {
for (int j = num[x][1]-3; j < num[x][1]; j++) {
if (board[i][j] == '.') {
continue;
}
if (key.containsKey(board[i][j])) {
rel = false;
break;
} else {
key.put(board[i][j], "");
}
}
}
key.clear();
}
return rel;
}
}
################################################################################
快速排序
class Solution {
public void sortColors(int[] nums) {
sortColorsX(nums, 0, nums.length - 1);
System.out.println(Arrays.toString(nums));
}
public void sortColorsX(int[] nums, int left, int right) {
if (left < right) {
int a = setVal(nums, left, right);
sortColorsX(nums, left, a - 1);
sortColorsX(nums, a + 1, right);
}
}
private int setVal(int[] nums, int left, int right) {
int numTemp = nums[left];
while (left < right) {
while (left < right && nums[right] >= numTemp) {
right--;
}
nums[left] = nums[right];
while (left < right && nums[left] <= numTemp) {
left++;
}
nums[right] = nums[left];
}
nums[left] = numTemp;
return left;
}
public static void main(String[] args) {
int[] nums = {2, 0, 2, 1, 1, 0};
new Solution().sortColors(nums);
}
}
######################################################################################
一个 「开心字符串」定义为:
仅包含小写字母 ['a', 'b', 'c'].
对所有在 1 到 s.length - 1 之间的 i ,满足 s[i] != s[i + 1] (字符串的下标从 1 开始)。
比方说,字符串 "abc","ac","b" 和 "abcbabcbcb" 都是开心字符串,但是 "aa","baa" 和 "ababbc" 都不是开心字符串。
给你两个整数 n 和 k ,你需要将长度为 n 的所有开心字符串按字典序排序。
请你返回排序后的第 k 个开心字符串,如果长度为 n 的开心字符串少于 k 个,那么请你返回 空字符串
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
String[] s = {"a", "b", "c"};
List<String> list = new ArrayList<>();
public String getHappyString(int n, int k) {
for (int i = 0; i < s.length; i++) {
getHappyStringMove(s, n, s[i]);
}
Arrays.sort(list.toArray());
if(list.size() >= k ){
return list.get(k-1);
}
return "";
}
public void getHappyStringMove(String[] s, int n, String str) {
if (str.length() == n) {
list.add(str);
return;
}
for (int i = 0; i < s.length; i++) {
String[] p = str.split("");
if (!p[p.length - 1].equals(s[i])) {
getHappyStringMove(s, n, str + s[i]);
}
}
}
}
#####################################################################################
给定一组单词words,编写一个程序,找出其中的最长单词,且该单词由这组单词中的其他单词组合而成。若有多个长度相同的结果,返回其中字典序最小的一项,若没有符合要求的单词则返回空字符串。
示例:
输入: ["cat","banana","dog","nana","walk","walker","dogwalker"]
输出: "dogwalker"
解释: "dogwalker"可由"dog"和"walker"组成。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-word-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
Map<String, String> m = new HashMap<>();
List<String> s = new ArrayList<>();
public boolean iSCompare(String w,int n) {
if(w.equals("") && n >1){
return true;
}
n++;
for (int i = 0; i <= w.length(); i++) {
if (m.containsKey(w.substring(0, i)) && iSCompare(w.substring(i),n)) {
return true;
}
}
return false;
}
String temp ="";
public String longestWord(String[] words) {
for (int i = 0; i < words.length; i++) {
m.put(words[i], words[i]);
}
for (int i = 0; i < words.length; i++) {
boolean rel = iSCompare(words[i],0);
if(rel) {
if (words[i].length() > temp.length()) {
temp = words[i];
}
else if(words[i].length() == temp.length()){
List<String> a = new ArrayList<>();
a.add(words[i]);
a.add(temp);
Arrays.asList(a);
temp =a.get(0);
}
}
}
return temp;
}
public static void main(String[] args) {
String word[] = {"cat", "banana", "dog", "nana", "walk", "walker", "dogwalker"};
System.out.println(new Solution().longestWord(word));
}
}
###################################################################################################################
给定一个已排序的链表的头 head , 删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表 。
public ListNode deleteDuplicates(ListNode head) {
ListNode toHead = new ListNode();
ListNode node = head;
ListNode end = toHead;
if (node == null || node.next == null) {
return head;
}
while (node != null) {
if (node.next != null && node.val == node.next.val) {
while (node.next != null && node.val == node.next.val){
node = node.next;
}
node = node.next;
} else {
toHead.next = node;
toHead = toHead.next;
node = node.next;
}
}
toHead.next = null;
return end.next;
}
#################################################################################################
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode node = new ListNode();
ListNode end = node;
while(list1 !=null || list2 != null){
if(list1 == null){
node.next = list2;
list2 = list2.next;
}else if(list2 == null ){
node.next = list1;
list1 = list1.next;
}else if(list1.val > list2.val){
node.next = list2;
list2 = list2.next;
}else{
node.next = list1;
list1 = list1.next;
}
node = node.next;
}
node.next = null;
return end.next;
}
public static void main(String[] args) {
/* ListNode head6 = new ListNode(5);
ListNode head5 = new ListNode(4,head6);
ListNode head4 = new ListNode(4, head5);
ListNode head3 = new ListNode(3, head4);*/
ListNode head2 = new ListNode(4);
ListNode head1 = new ListNode(2, head2);
ListNode head0 = new ListNode(1, head1);
ListNode head22 = new ListNode(4);
ListNode head11 = new ListNode(3, head22);
ListNode head00 = new ListNode(1, head11);
System.out.println(new Solution().mergeTwoLists(head0,head00));
}
}
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
示例 1:
输入:root = [1,null,2,3]
输出:[1,3,2]
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> rel = new ArrayList<>();
if(root !=null) {
traversal(root, rel);
}
return rel;
}
public void traversal(TreeNode root,List<Integer> rel){
if(root.left !=null) {
traversal(root.left, rel);
}
rel.add(root.val);
if(root.right !=null) {
traversal(root.right, rel);
}
}
}
#############################################################
给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p != null && q != null) {
return traversal(p, q);
}
return false;
}
public boolean traversal(TreeNode p, TreeNode q) {
if (p != null && q != null) {
if (p.val == q.val) {
return traversal(p.left, q.left) && traversal(p.right, q.right);
} else {
return false;
}
} else {
if (p == q) {
return true;
}
}
return false;
}
################################
给你一个二叉树的根节点 root , 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
public boolean isSymmetric(TreeNode root) {
if (root != null) {
return Symmetric(root.left,root.right);
}
return true;
}
public boolean Symmetric(TreeNode left,TreeNode right) {
if(left ==null && right == null){
return true;
}
if(left !=null && right != null) {
if (left.val == right.val) {
return Symmetric(left.left,right.right)&&Symmetric(left.right,right.left);
}
}
return false;
}
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
class Solution {
public int maxDepth(TreeNode root) {
return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
假设有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花不能种植在相邻的地块上,它们会争夺水源,两者都会死去。
给你一个整数数组 flowerbed 表示花坛,由若干 0 和 1 组成,其中 0 表示没种植花,1 表示种植了花。另有一个数 n ,能否在不打破种植规则的情况下种入 n 朵花?能则返回 true ,不能则返回 false。
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
if(flowerbed ==null){
return false;
}
if(flowerbed.length == 1){
if(flowerbed[0] == 0){
n--;
}
}else {
for (int i = 0; i < flowerbed.length; i++) {
if (flowerbed[i] != 1 && flowerbed.length > 1) {
if (i - 1 < 0) {
if (flowerbed[i + 1] != 1) {
flowerbed[i] = 1;
n--;
}
} else if (i + 1 >= flowerbed.length) {
if (flowerbed[i - 1] != 1) {
flowerbed[i] = 1;
n--;
}
} else {
if (flowerbed[i - 1] != 1 && flowerbed[i + 1] != 1) {
flowerbed[i] = 1;
n--;
}
}
}
}
}
return n<=0;
}
}
爱丽丝和鲍勃一起玩游戏,他们轮流行动。爱丽丝先手开局。
最初,黑板上有一个数字 n 。在每个玩家的回合,玩家需要执行以下操作:
选出任一 x,满足 0 < x < n 且 n % x == 0 。
用 n - x 替换黑板上的数字 n 。
如果玩家无法执行这些操作,就会输掉游戏。
只有在爱丽丝在游戏中取得胜利时才返回 true 。假设两个玩家都以最佳状态参与游戏。
public boolean divisorGame(int n) {
boolean dp[] = new boolean[n + 1];
dp[1] = false;
for (int i = 2; i <= n; i++) {
int x = i - 1;
dp[i] = false;
while (!dp[x] && x > 0) {
if (i % (i - x) == 0) {
dp[i] = true;
}
--x;
}
}
return dp[n];
}
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
进阶:
如果有大量输入的 S,称作 S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?
public boolean isSubsequence(String s, String t) {
boolean rel =true;
int temp = 0;
for(int i=0;i<s.length();i++){
t = t.substring(temp,t.length());
int next = t.indexOf(s.charAt(i));
if(next>=0){
temp = next+1;
}else{
rel = false;
break;
}
}
return rel;
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
TreeNode tn = new TreeNode();
public TreeNode mirrorTree(TreeNode root) {
if(root == null){
return null;
}
tn.val = root.val;
tn.right = mirrorTree(root.left,tn.left);
tn.left = mirrorTree(root.right,tn.right);
return tn;
}
public TreeNode mirrorTree(TreeNode root,TreeNode tnx) {
if(root == null){
return null;
}
tnx = new TreeNode();
tnx.val = root.val;
tnx.right = mirrorTree(root.left,tnx.left);
tnx.left = mirrorTree(root.right,tnx.right);
return tnx;
}
}
连续增加和最大
package com.huawei.main;
import java.util.*;
public class Test {
public void setNum(int num[]){
int left =0;
int right = 1;
int max = 0;
while(right < num.length){
if(num[right-1]>num[right]){
int sum=0;
for(int i=left;i<=right;i++){
sum+=num[i];
}
max = Math.max(max,sum);
left = right;
}
right++;
}
System.out.println(max);
}
public static void main(String[] args) {
int num[] = {-1,2,4,1,2,3,-9};
new Test().setNum(num);
}
}