2021-03-15-废宅的每日拯救计划

我自己的想法是一日练一题,因为自己的基础实在太差了orz,感觉简单题可以很快做出来,中等难度就要会想久,困难题好像根本没有信心点进去看。
周围熟悉的人和自己都是差不多的水平,自己也没有长者智者的指引。学习的知识好像差不多只能应付考试,不想安逸于现状又不知道要怎么突破。
不想人生被迷茫侵蚀。
自我拯救一下
#两数之和

class Solution {
    
    
    public int[] twoSum(int[] nums, int target) {
    
    
        int[] a = new int[2];
        for(int i=0;i<nums.length;i++){
    
    
            for(int j=i+1;j<nums.length;j++){
    
    
                if(nums[i]+nums[j]==target){
    
    
                    a[0] = i;
                    a[1] = j;
                }
            }
        }
        return a;
    }
}

#整数反转

class Solution {
    
    
    public int reverse(int x) {
    
    
    	int res=0;
    	while(x!=0)
    	{
    
    
    		int t=x%10;
    		int newRes=res*10+t;
    		if((newRes-t)/10!=res)
    			return 0;
    		res=newRes;
    		x=x/10;
    	}
    	return res;

    }
    
}

#回文数

class Solution {
    
    
    public boolean isPalindrome(int x) {
    
    
    	if(x<0) return false;
    	else{
    
    
    		int num=x;
    		int res=0;
    		while(x!=0)
    		{
    
    
    			res=res*10+x%10;
    			x=x/10;
    		}
    		if(res==num) return true;
    		else return false;
    	}

    }
}

#罗马型转整数

public class Solution {
    
    
    public int romanToInt(String s) {
    
    
    	int sum=0;
    	int first=getNum(s.charAt(0));
    	for(int i=1;i<s.length();i++)
    	{
    
    
    		int n=getNum(s.charAt(i));
    		if(first<n)
    		{
    
    
    			sum=sum-first;
    		}else{
    
    
    			sum=sum+first;
    		}
    		first=n;
    	}
    	sum=sum+first;
    	return sum;
    	
    }
    public int getNum(char ss)
    {
    
    
    	switch(ss){
    
    
    	case 'I':
    		return 1;
    	case 'V':
    		return 5;
    	case 'X':
    		return 10;
    	case 'L':
    		return 50;
    	case 'C':
    		return 100;
    	case 'D':
    		return 500;
    	case 'M':
    		return 1000;
    	default:
    		return 0;
    		
    	}
    }

}

#有效的括号

import java.util.Stack;

class Solution {
    
    
	public boolean isValid(String s) {
    
    
		Stack<Character> str = new Stack<Character>();
		for (int i = 0; i < s.length(); i++) {
    
    
			if (s.charAt(i) == '(') {
    
    
				str.push('(');
			}
			if (s.charAt(i) == ')') {
    
    
				if (!str.empty() && str.pop() == '(')
					continue;
				else
					return false;
			}
			if (s.charAt(i) == '{') {
    
    
				str.push('{');
			}
			if (s.charAt(i) == '}') {
    
    
				if (!str.empty() && str.pop() == '{')
					continue;
				else
					return false;
			}
			if (s.charAt(i) == '[') {
    
    
				str.push('[');
			}
			if (s.charAt(i) == ']') {
    
    
				if (!str.empty() && str.pop() == '[')
					continue;
				else
					return false;
			}
			
		}
		if(str.empty())
			return true;
		else return false;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_45921943/article/details/114850202