6. ZigZag Conversion,将字符串按Z字形排列并横向输出
public class Solution {public String convert ( String s , int nRows ) {int len = s . length ();if ( len == 0 || nRows <= 1 ) return s ;String [] ans = new String [ nRows ];Arrays . fill ( ans , "" );int row = 0 , delta = 1 ;for ( int i = 0 ; i < len ; i ++) {ans [ row ] += s . charAt ( i );row += delta ;if ( row >= nRows ) {row = nRows - 2 ;delta = - 1 ;}if ( row < 0 ) {row = 1 ;delta = 1 ;}}String ret = "" ;for ( int i = 0 ; i < nRows ; i ++) {ret += ans [ i ];}return ret ;}}
7 inverse integer
这道题是反转输出整数,需要考虑溢出的问题
首先我们要清楚int的范围int -2147483648~2147483647
如果反转后超出这个范围 我们就要返回-1
public class Solution {
public static int reverse(int x)
{
char[] numArray = null;
String numStr="";
if (x == 0) {
return 0;
} else if (x > 0) {
numArray = (x + "").toCharArray();
} else {
numStr+="-";
numArray = (x + "").substring(1).toCharArray();
}
for(int i =numArray.length-1;i>=0;i--){
numStr+=numArray[i];
}
long res=Long.parseLong(numStr);
if(res<-2147483648||res>2147483647)
return 0;
else
{
return (int)res;
}
}
}
10 10. Regular Expression Matching
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
<span style="font-size:14px;">public class Solution { public boolean isMatch(String s, String p) { if(p.length() == 0) return s.length() == 0; //p's length 1 is special case if(p.length() == 1 || p.charAt(1) != '*') { if(s.length() < 1 || (p.charAt(0) != '.' && s.charAt(0) != p.charAt(0))) return false; return isMatch(s.substring(1), p.substring(1)); } else { int len = s.length(); int i = -1; while(i<len && (i < 0 || p.charAt(0) == '.' || p.charAt(0) == s.charAt(i))){ if(isMatch(s.substring(i+1), p.substring(2))) return true; i++; } return false; } } }</span>
27. Remove Element
<span style="font-size:14px;">public class Solution { public int removeElement(int[] nums, int val) { int i=0; int j=0; while(j < nums.length){ if(nums[j] != val){ nums[i] = nums[j]; i++; } j++; } return i; } }</span>
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
package leetcode; import java.util.Stack; public class Le20 { public static boolean isValid(String s) { char[] ca=s.toCharArray(); if(s.length()==0) return true; if(s.length()==1) { if(ca[0]!='('&&ca[0]!=')'&&ca[0]!='['&&ca[0]!=']'&&ca[0]!='['&&ca[0]!='{'&&ca[0]!='}') return true; else return false; } Stack stack=new Stack(); for(int i=0;i<ca.length;i++) { if(ca[i]=='('||ca[i]==')'||ca[i]=='['||ca[i]==']'||ca[i]=='['||ca[i]=='{'||ca[i]=='}') { if(stack.isEmpty()) { stack.push(ca[i]); } else { char tmp=(char)stack.peek(); if((tmp=='('&&ca[i]==')')||(tmp=='['&&ca[i]==']')||(tmp=='{'&&ca[i]=='}')) { stack.pop(); } else { stack.push(ca[i]); } } } } if(stack.size()==0) { return true; } else { return false; } } public static void main(String[] args) { boolean flag=isValid("a()b"); System.out.println(flag); } }
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWord to endWord, such that:
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog"
,
return its length 5
.
Note:
public int ladderLength(String beginWord, String endWord, Set<String> wordList) { wordList.add(endWord); if(wordList.contains("beginWord")) { wordList.remove("beginWord"); } Queue<String> que = new LinkedList<String>(); ArrayList<String> remove=new ArrayList<String>(); HashMap<String,Integer> hashmap=new HashMap<String,Integer>(); HashMap<String,ArrayList<String>> result=new HashMap<String,ArrayList<String>>(); hashmap.put(beginWord, 1); que.add(beginWord); int floor=1; while(!que.isEmpty()) { String tmp=que.poll(); result.put(tmp, new ArrayList<String>()); int cur=hashmap.get(tmp); System.out.println("第几层?"+cur+"正在处理的string为:"+tmp); if(tmp.equals(endWord)) { System.out.println("这就是答案"+tmp+" "+cur); return cur; } if(cur!=floor) { System.out.println("这是新的一层"); for(int i=0;i<remove.size();i++) { wordList.remove(remove.get(i)); System.out.println("移除set中上一层的元素:"+remove.get(i)); } floor=cur; System.out.println("更新现在正在访问的层数floor:"+floor); remove.clear(); System.out.println("清除所有remove内容"); } for(String s:wordList) { if(near(tmp,s)) { System.out.println(tmp+"可以经过一步变为:"+s); ArrayList<String> list=new ArrayList<String>(); System.out.println("新建链表 为期赋值 然后加入到 上一个结点的list中"); result.get(tmp).add(s); que.add(s); System.out.println("队列中加入新的结点"+s); remove.add(s); System.out.println("需要移除的string:"+s); hashmap.put(s, hashmap.get(tmp)+1); System.out.println("为此结点的深度加一 "+s+" "+hashmap.get(tmp)); } } } return 0; }
public static boolean near(String s1,String s2) { if(s1.length()!=s2.length()) { return false; } int len1=s1.length(); int len2=s2.length(); int count=0; for(int i=0,j=0;i<s1.length();i++,j++) { if(s1.charAt(i)!=s2.charAt(j)) { count++; } } if(count==1) { return true; } return false; }