leedcode 初级题 (Strings 系列)

1、字符串倒叙排列。

Write a function that takes a string as input and returns the string reversed.

Example 1:

Input: "hello"
Output: "olleh"
Example 2:

Input: "A man, a plan, a canal: Panama"
Output: "amanaP :lanac a ,nalp a ,nam A"

//方法一
class Solution {
    public String reverseString(String s) { 
        char temp;
        int n = s.length();
        char[] myChar=s.toCharArray();//不用指定长度?字符串指定长度?
        for(int i=0;i<n/2;i++)//小心错误,不能为 i<n,则交换了两次又交换成了hello
        {
            temp=myChar[i];
            myChar[i]=myChar[n-i-1];
            myChar[n-i-1]=temp; 
        }
       return new String(myChar);               
    }
}


2、整数倒叙问题。

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321
Example 2:

Input: -123
Output: -321
Example 3:

Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

//方法 
class Solution {
    public int reverse(int x) {
        int result = 0;
        while(x!=0)
        {
            int tail = x%10;
            int newResult = result*10+tail;
            if((newResult-tail)/10!=result)//tail 可以省略 因为newResult/10已经去掉了末位
            {
                return 0;
            }
           result = newResult;
            x=x/10;
        }
    return result;
    }
    
}
as many others, I didn't get the line if ((newResult - tail) / 10 != result).

We're interested on what happens when an integer overflows. Well, it is rolled over. Practically speaking, if you would try

    public static void main(String[] args) {
        int rollMeOver= Integer.MAX_VALUE + 1;
        System.out.println(rollMeOver);
    }
You will get as an output -2147483648 which represents the lowest value for an integer (Integer.MIN_VALUE).

Thus, in our problem, if newResult is going to overflow we are sure that newResult / 10 != result (this is the reason that @Inception_wzd said that we don't need to subtract the tail first because by / 10 we are already losing the last digit).

By the way, the same thing happens for the underflow.

    public static void main(String[] args) {
        int rollMeOver= Integer.MIN_VALUE - 1;
        System.out.println(rollMeOver);
    }
This is going to output the Integer.MAX_VALUE which is 2147483647 .


3、寻找字符串中第一个不重复的字符。

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.
Note: You may assume the string contain only lowercase letters.

//常规解法 o(n^2)
class Solution {
    public int firstUniqChar(String s) {
        char [] myChar = s.toCharArray();
        int n = s.length();
        int j=0;
        while(j<n)
        {
             for(int i=0;i<n;i++)
            {
                if ((myChar[j]!=myChar[i])&&(i!=j)) 
                return j;

            }
            j++;
        }
       
    return -1;
    }
}

//解法二:

//Get the frequency of each character.
//Get the first character that has a frequency of one.
//Actually the code below passes all the cases. However, according to @xietao0221, we //could change the size of the frequency array to 256 to store other kinds of characters. 

public class Solution {
    public int firstUniqChar(String s) {
        int freq [] = new int[26];
        for(int i = 0; i < s.length(); i ++)
            freq [s.charAt(i) - 'a'] ++;
        for(int i = 0; i < s.length(); i ++)
            if(freq [s.charAt(i) - 'a'] == 1)
                return i;
        return -1;
    }
}

猜你喜欢

转载自blog.csdn.net/yangwei256/article/details/82355199
今日推荐