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;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值