leetcode 系列

 

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
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

27. Remove Element



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;
    }
}

20. Valid Parentheses

 

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

127. Word Ladder

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

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:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
public  int ladderLength(String beginWord, String endWord, Set wordList)
	{
		wordList.add(endWord);
		if(wordList.contains("beginWord"))
		{
			wordList.remove("beginWord");
		}
		Queue que = new LinkedList();
		ArrayList  remove=new ArrayList();
		HashMap hashmap=new HashMap();
		HashMap> result=new HashMap>();
		
		hashmap.put(beginWord, 1);
		que.add(beginWord);
		int floor=1;
		while(!que.isEmpty())
		{
			String tmp=que.poll();
			result.put(tmp, new ArrayList());
			
			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 list=new ArrayList();
					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





你可能感兴趣的:(leetcode,算法)