class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.length()==0) return 0;
HashMap<Character,Integer>mp=new HashMap<>();
int left=0;
int res=0;
for(int i=0;i<s.length();i++){
if(mp.get(s.charAt(i))!=null){
left=Math.max(left,mp.get(s.charAt(i))+1);
}
mp.put(s.charAt(i),i);
res=Math.max(res,i-left+1);
}
return res;
}
}
class Solution {
public int reverse(int x) {
int res=0;
while(x!=0){
int tmp=x%10;
if(res>Integer.MAX_VALUE/10) return 0;
if(res<-Integer.MAX_VALUE/10) return 0;
res=res*10+tmp;
x=x/10;
}
return res;
}
}
class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
int tmp=x;
int rev=0;
while(tmp!=0){
rev=rev*10+tmp%10;
tmp=tmp/10;
}
return rev==x;
}
}
class Solution {
public:
int romanToInt(string s) {
int result=0;
map<char, int> mp = { {'I',1} ,{'V', 5} ,{'X', 10},{'L', 50} ,
{'C', 100} ,{'D', 500} ,{'M', 1000} };
for(int i=0;i<s.length();i++){
if(mp[s[i]] < mp[s[i+1]])
result-=mp[s[i]];
else
result+=mp[s[i]];
}
return result;
}
};
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length==0) return "";
String res=strs[0];
int i=1;
while(i<strs.length){
while(strs[i].indexOf(res)!=0){
res=res.substring(0,res.length()-1);
}
i++;
}
return res;
}
}
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode fast=head;
ListNode slow=head;
for(int i=0;i<n;i++){
fast=fast.next;
}
if(fast==null) return head.next; 此时删除的是第一个节点
while(fast.next!=null){
fast=fast.next;
slow=slow.next;
}
slow.next=slow.next.next;
return head;
}
}
class Solution {
public boolean isValid(String s) {
Stack<Character>st=new Stack();
char[] ch=s.toCharArray();
for(int i=0;i<ch.length;i++){
if(ch[i]=='(') st.push(')');
else if(ch[i]=='[') st.push(']');
else if(ch[i]=='{') st.push('}');
else if(!st.isEmpty()&&ch[i]==st.peek()){
st.pop();
}
else return false;
}
return st.isEmpty()?true:false;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null||nums.length==0) return 0;
int p=0,q=1;
while(q<nums.length){
if(nums[p]!=nums[q]){
nums[p+1]=nums[q];
p++;
}
q++;
}
return p+1;
}
}
class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp=new int[amount+1];
Arrays.fill(dp,amount+1);
dp[0]=0;
for(int i=1;i<=amount;i++){
for(int coin:coins){
if(i-coin<0) continue;
dp[i]=Math.min(dp[i],dp[i-coin]+1);
}
}
return dp[amount]==amount+1?-1:dp[amount];
}
}
class Solution {
public int change(int amount, int[] coins) {
int[]dp=new int[amount+1];
Arrays.fill(dp,0);
dp[0]=1;
for(int coin:coins){
for(int j=coin;j<=amount;j++){
dp[j]=dp[j]+dp[j-coin];
}
}
return dp[amount];
}
}
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
//可以类比于背包问题
int n=s.length();
//dp[i]表示s中以i-1结尾的字符串是否可以被拆分
boolean[]dp=new boolean[n+1];
dp[0]=true;
for(int i=1;i<=n;i++){
for(int j=0;j<i;j++){
if(dp[j]&&wordDict.contains(s.substring(j,i))){
dp[i]=true;
break;
}
}
}
return dp[n];
}
}
class Solution {
public int combinationSum4(int[] nums, int target) {
int []dp=new int[target+1];
dp[0]=1;
for(int i=1;i<=target;i++){
for(int num:nums){
if(i>=num){
dp[i]+=dp[i-num];
}
}
}
return dp[target];
}
}
class Solution {
public int minDistance(String word1, String word2) {
int m=word1.length(),n=word2.length();
int [][]dp=new int[m+1][n+1];
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(word1.charAt(i-1)==word2.charAt(j-1)){
dp[i][j]=dp[i-1][j-1]+1;
}
else{
dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}
return m+n-2*dp[m][n];
}
}
class Solution {
public int minDistance(String word1, String word2) {
int m=word1.length();
int n=word2.length();
int [][]dp=new int[m+1][n+1];
for(int i=1;i<=m;i++){
dp[i][0]=i;
}
for(int i=1;i<=n;i++){
dp[0][i]=i;
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(word1.charAt(i-1)==word2.charAt(j-1)){
dp[i][j]=dp[i-1][j-1];
}
else{
dp[i][j]=Math.min(dp[i-1][j],Math.min(dp[i][j-1],dp[i-1][j-1]))+1;
}
}
}
return dp[m][n];
}
}