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 二维数组地址值
}
}
}