ListNode virtualNode = new ListNode(0);
int flag=0;
ListNode temp=virtualNode;
while (l1!=null||l2!=null){
if(l1!=null&&l2!=null){
int value=l1.val+l2.val+flag;
temp.next=new ListNode(value%10);
if(value>=10){
flag=1;
}else{
flag=0;
}
temp=temp.next;
l1=l1.next;
l2=l2.next;
}else if(l1!=null&&l2==null){
int value=l1.val+flag;
temp.next=new ListNode(value%10);
if(value>=10){
flag=1;
}else{
flag=0;
}
temp=temp.next;
l1=l1.next;
}else{
int value=l2.val+flag;
temp.next=new ListNode(value%10);
if(value>=10){
flag=1;
}else{
flag=0;
}
temp=temp.next;
l2=l2.next;
}
}
if(flag==1){
temp.next=new ListNode(1);
}
return virtualNode.next;
if(s.length()==0){
return 0;
}
Map<Character,Integer>map=new HashMap<>();
char[] chars = s.toCharArray();
map.put(chars[0],0);
int[]dp=new int[s.length()];
dp[0]=1;
int res=1;
for(int i=1;i<chars.length;i++){
if(map.containsKey(chars[i])){
dp[i]=Math.min(dp[i-1]+1,i-map.get(chars[i]));
}else{
dp[i]=dp[i-1]+1;
}
map.put(chars[i],i);
res=Math.max(res,dp[i]);
}
return res;
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int totalLength=nums1.length+nums2.length;
if(totalLength%2==1){
return findRes(nums1,0,nums2,0,totalLength/2+1);
}else{
int res1= findRes(nums1,0,nums2,0,totalLength/2);
int res2= findRes(nums1,0,nums2,0,totalLength/2+1);
return (res1+res2)/2.0;
}
}
private int findRes(int[]nums1,int start1,int[]nums2,int start2,int k){
int length1=nums1.length-start1;
int length2=nums2.length-start2;
if(length1>length2){
return findRes(nums2,start2,nums1,start1,k);
}
if(length1==0){
return nums2[start2+k-1];
}
if(k==1){
return Math.min(nums1[start1],nums2[start2]);
}
int newStart1=start1+Math.min(length1,k/2)-1;
int newStart2=start2+Math.min(length2,k/2)-1;
if(nums1[newStart1]>nums2[newStart2]){
return findRes(nums1,start1,nums2,newStart2+1,k-(newStart2-start2+1));
}else{
return findRes(nums1,newStart1+1,nums2,start2,k-(newStart1-start1+1));
}
}
}
boolean[][]dp=new boolean[s.length()][s.length()];
int res=0;
String ans="";
for(int j=0;j<s.length();j++){
for(int i=0;i<=j;i++){
if(s.charAt(i)==s.charAt(j)&&(j-i<2||dp[i+1][j-1])){
dp[i][j]=true;
if(j-i+1>res){
res=j-i+1;
ans=s.substring(i,j+1);
}
}
}
}
return ans;
class Solution {
public boolean isMatch(String s, String p) {
boolean[][]dp=new boolean[p.length()+1][s.length()+1];
dp[0][0]=true;
for(int i=2;i<=p.length();i++){
if(p.charAt(i-1)=='*'&&dp[i-2][0]){
dp[i][0]=true;
}
}
for(int i=1;i<=p.length();i++){
for(int j=1;j<=s.length();j++){
if(p.charAt(i-1)=='.'&&dp[i-1][j-1]){
dp[i][j]=true;
}else if(p.charAt(i-1)=='*'){
if(dp[i-1][j]||dp[i-2][j]){
dp[i][j]=true;
}else if((p.charAt(i-2)==s.charAt(j-1)||p.charAt(i-2)=='.')&&dp[i][j-1]) {
dp[i][j] = true;
}
}else if(p.charAt(i-1)==s.charAt(j-1)&&dp[i-1][j-1]){
dp[i][j]=true;
}
}
}
return dp[dp.length-1][dp[0].length-1];
}
}
class Solution {
private List<List<Integer>>res=new ArrayList<>();
public List<List<Integer>> threeSum(int[] nums) {
if(nums.length<3){
return res;
}
Arrays.sort(nums);
for(int i=0;i<nums.length-2;i++){
if(nums[i]>0){
break;
}
if(i>0&&nums[i]==nums[i-1]){
continue;
}
int left=i+1;
int right=nums.length-1;
while (left<right){
if(nums[i]+nums[left]+nums[right]<0){
left++;
}else if(nums[i]+nums[left]+nums[right]>0){
right--;
}else{
res.add(Arrays.asList(nums[i],nums[left],nums[right]));
while (left<right&&nums[right]==nums[right-1]){
right--;
}
while (left<right&&nums[left]==nums[left+1]){
left++;
}
right--;
left++;
}
}
}
return res;
}
}
class Solution {
private List<String>res=new ArrayList<>();
private StringBuilder sb=new StringBuilder();
public List<String> generateParenthesis(int n) {
backTrack(0,0,n);
return res;
}
private void backTrack(int left,int right,int n){
if(left==n&&right==n){
res.add(sb.toString());
return;
}
if(right>left){
return;
}
if(left<=n){
sb.append("(");
backTrack(left+1,right,n);
sb.deleteCharAt(sb.length()-1);
}
if(right<=n){
sb.append(")");
backTrack(left,right+1,n);
sb.deleteCharAt(sb.length()-1);
}
}
}
if(lists==null||lists.length==0){
return null;
}
PriorityQueue<ListNode> queue = new PriorityQueue<>(new Comparator<ListNode>() {
@Override
public int compare(ListNode o1, ListNode o2) {
return o1.val-o2.val;
}
});
for (ListNode list : lists) {
if(list!=null){
queue.add(list);
}
}
ListNode virtualNode = new ListNode(0);
ListNode temp=virtualNode;
while (!queue.isEmpty()){
ListNode node = queue.poll();
temp.next=node;
temp=temp.next;
if(node.next!=null) {
node=node.next;
queue.add(node);
}
}
return virtualNode.next;
int length=nums.length-1;
for(int i=length;i>0;i--){
if(nums[i]>nums[i-1]){
Arrays.sort(nums,i,length+1);
}
for(int j=i;j<=length;j++){
if(nums[j]>nums[i-1]){
int temp=nums[i-1];
nums[i-1]=nums[j];
nums[j]=temp;
return;
}
}
}
Arrays.sort(nums);
return;
class Solution {
public int longestValidParentheses(String s) {
if(s.length()==0){
return 0;
}
char[] chars = s.toCharArray();
boolean[]dp=new boolean[chars.length];
Stack<Integer> stack = new Stack<>();
for(int i=0;i<chars.length;i++){
if(stack.isEmpty()){
stack.push(i);
}else if(chars[i]==')'&&chars[stack.peek()]=='('){
dp[stack.pop()]=true;
dp[i]=true;
}else{
stack.push(i);
}
}
int res=0;
int count=0;
for(int i=0;i<dp.length;i++){
if(dp[i]){
count++;
res=Math.max(res,count);
}else{
count=0;
}
}
return res;
}
}
int left=0;
int right=nums.length-1;
int mid;
while (left<=right){
mid=(left+right)/2;
if(target==nums[mid]){
return mid;
}
//前半部分有序,比如2345671
if(nums[left]<=nums[mid]){ //还存在2个数的情况,比如3 1
if(target>=nums[left]&&target<nums[mid]){
right=mid-1;
}else{
left=mid+1;
}
//后半部分有序,比如6712345
}else{
if(target<=nums[right]&&target>nums[mid]){
left=mid+1;
}else{
right=mid-1;
}
}
}
return -1;
class Solution {
public int[] searchRange(int[] nums, int target) {
if(nums.length==0){
return new int[]{-1,-1};
}
int left=0;
int right=nums.length-1;
int mid=0;
while (left<=right){
mid=(left+right)/2;
if(nums[mid]==target){
int start=mid;
while (start>=0&&nums[start]==target){
start--;
}
int end=mid;
while (end<nums.length&&nums[end]==target){
end++;
}
return new int[]{start+1,end-1};
}else if(target>nums[mid]){
left=mid+1;
}else {
right=mid-1;
}
}
return new int[]{-1,-1};
}
}
if(matrix.length==1){
return;
}
int len=matrix.length;
int temp;
//对角线交换元素
for(int i=0;i<len;i++){
for(int j=len-1;j>i;j--){
temp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
}
for(int i=0;i<len/2;i++){
for(int j=0;j<len;j++){
temp=matrix[j][i];
matrix[j][i]=matrix[j][len-i-1];
matrix[j][len-i-1]=temp;
}
}
//先使用2来填充,然后替换成1,0,并递增下标。
int zero=0;
int one=0;
int temp;
for(int i=0;i<nums.length;i++){
temp=nums[i];
nums[i]=2;
if(temp<=1){
nums[one]=1;
one++;
}
if(temp==0){
nums[zero]=0;
zero++;
}
}
class Solution {
private Map<Character, Integer> map = new HashMap<>();
public String minWindow(String s, String t) {
if(s.length()<t.length()){
return "";
}
for (char c : t.toCharArray()) {
map.put(c,map.getOrDefault(c,0)+1);
}
int left=0;
int right=0;
int length=Integer.MAX_VALUE;
int start=0;
char[] chars = s.toCharArray();
while (right<chars.length){
if(map.containsKey(chars[right])){
map.put(chars[right],map.get(chars[right])-1);
}
right++;
while (check(map)){
if(right-left<length){
start=left;
length=right-left;
}
if(map.containsKey(chars[left])){
map.put(chars[left],map.get(chars[left])+1);
}
left++;
}
}
if(length==Integer.MAX_VALUE){
return "";
}else{
return s.substring(start,start+length);
}
}
private boolean check(Map<Character,Integer>map){
for (Integer value : map.values()) {
if(value>0){
return false;
}
}
return true;
}
}
package Hot_100._79exist;
class Solution {
private int[][]direction=new int[][]{{0,1},{1,0},{0,-1},{-1,0}};
public boolean exist(char[][] board, String word) {
boolean[][] vis = new boolean[board.length][board[0].length];
int index=0;
for(int row=0;row<board.length;row++){
for(int col=0;col<board[0].length;col++){
if(board[row][col]==word.charAt(index)){
vis[row][col]=true;
if(backTrack(row,col,index+1,board,vis,word)){
return true;
}else{
vis[row][col]=false;
}
}
}
}
return false;
}
private boolean backTrack(int row,int col,int index,char[][]board,boolean[][]vis,String word){
if(index==word.length()){
return true;
}
for (int[] ints : direction) {
int newRow=row+ints[0];
int newCol=col+ints[1];
if(newRow>=0&&newRow<board.length&&newCol>=0&&newCol<board[0].length&&!vis[newRow][newCol]) {
if (word.charAt(index) == board[newRow][newCol]) {
vis[newRow][newCol] = true;
if (backTrack(newRow, newCol, index+1, board, vis, word)) {
return true;
}
vis[newRow][newCol] = false;
}
}
}
return false;
}
}
int[]dp=new int[heights.length+2];
for(int i=0;i<heights.length;i++){
dp[i+1]=heights[i];
}
Stack<Integer> stack = new Stack<>();
int res=0;
stack.push(0);
for(int i=1;i<dp.length;i++){
while (dp[i]<dp[stack.peek()]){
Integer index = stack.pop();
int left=stack.peek();
int right=i;
int width=right-left-1;
int height=dp[index];
dp[index]=width*height;
res=Math.max(res,dp[index]);
}
stack.push(i);
}
return res;
ListNode moreVirtualNode = new ListNode(0);
ListNode lessVirtualNode = new ListNode(0);
ListNode moreTemp=moreVirtualNode;
ListNode lessTemp=lessVirtualNode;
ListNode temp=head;
while (temp!=null){
if(temp.val<x){
lessTemp.next=temp;
lessTemp=lessTemp.next;
}else{
moreTemp.next=temp;
moreTemp=moreTemp.next;
}
temp=temp.next;
}
if(moreTemp.next!=null){
moreTemp.next=null;
}else if(lessTemp.next!=null){
lessTemp.next=null;
}
lessTemp.next=moreVirtualNode.next;
return lessVirtualNode.next;
//题目就是二叉的前序遍历使用迭代法
if(root==null){
return;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
TreeNode virtualNode = new TreeNode(0);
TreeNode temp=virtualNode;
while (!stack.isEmpty()){
int size=stack.size();
for(int i=0;i<size;i++){
TreeNode node = stack.pop();
if(node.right!=null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
temp.left=null;
temp.right=node;
temp=temp.right;
}
}
root=virtualNode.right;
if(root==null){
return 0;
}
int left=Math.max(0,dfs(root.left));
int right=Math.max(0,dfs(root.right));
//单个子树的最大值
res=Math.max(res,root.val+left+right);
//返回给上层只能左右节点选一
return root.val+Math.max(left,right);