两数之和、有效的括号

1.正则表达式。

2.栈:FILO

public class IsValid {
    public boolean isValid1(String s) {
            if (s == null || s.length() % 2 != 0)
                return false;
            while (s.length() > 0){
                String temp = s;
                s = s.replaceAll("\\[]","").replaceAll("\\{}","").replaceAll("\\(\\)","");
                if(temp.equals(s))
                    return false;
            }
            return true;
        }
    public boolean isValid(String s) {
        if(s.isEmpty())
            return true;
        Stack<Character> stack=new Stack<Character>();
        for(char c:s.toCharArray()){
            if(c=='(')
                stack.push(')');
            else if(c=='{')
                stack.push('}');
            else if(c=='[')
                stack.push(']');
            else if(stack.empty()||c!=stack.pop())
                return false;
        }
        if(stack.empty())
            return true;
        return false;
    }
    public static void main(String[] args) {
        IsValid i=new IsValid();
        System.out.println(i.isValid("()[]{"));


    }
}

 数组:

public class TwoSum {

    public static int[] twoSum(int[]nums,int target){
        Map<Integer,Integer>hashTable=new HashMap<Integer,Integer>();
        for (int i = 0; i < nums.length; i++) {

            if(hashTable.containsKey(target-nums[i])){
                return new int[]{hashTable.get(target-nums[i]),i};
            }
            hashTable.put(nums[i],i);
        }
        return new int[0];
    }
    public static void main(String[] args) {
        int[]nu=TwoSum.twoSum(new int[]{1,2,3,4,5},3);
        for (int i = 0; i < nu.length; i++) {
            System.out.println(nu[i]);
            //System.out.println(arr);//[[I@4eec7777 二维数组地址值
        }
    }
}

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值