LeetCode-11-7

1.Reverse String

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

Example:
Given s = "hello", return "olleh".

1. 可以直接用String的reverse方法,就是要注意的是要用StringBuilder的方法,不然一直newString会超时

class Solution {
    public String reverseString(String s) {
        return new StringBuilder(s).reverse().toString();
    }
}

2. 这是个正确的答案,我最开始思路也是这样,但是可能因为我的代码用了String的拼接,就算改成了Stringbuilder也超时

 char[] word = s.toCharArray();
        int i = 0;
        int j = s.length() - 1;
        while (i < j) {
            char temp = word[i];
            word[i] = word[j];
            word[j] = temp;
            i++;
            j--;
        }
        return new String(word);

2. Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = "hello", return "holle".

Example 2:
Given s = "leetcode", return "leotcede".

1. 可能要麻烦点,就是正常的思路,每次如果碰到元音字母,就停下指针,当两个都是元音字母,交换

class Solution {
    public String reverseVowels(String s) {
        char [] chars = s.toCharArray();
        int i = 0 ;
        int j = s.length() - 1;
        while(i < j) {
            if(isvowels(chars[i]) && isvowels(chars[j])) {
                char tmp = chars[i];
                chars[i] = chars[j];
                chars[j] = tmp;
                i++;
                j--;
            }else if(isvowels(chars[i])) {
                j--;
            }else if(isvowels(chars[j])){
                i++;
            }else {
                i++;
                j--;
            }
        }
        return new String(chars);
    }
    
    public boolean isvowels(char ch) {
        switch(ch) {
            case 'a':
                return true;
            case 'e':
                return true;
            case 'i':
                return true;
            case 'o':
                return true;
            case 'u':
                return true;
            case 'A':
                return true;
            case 'E':
                return true;
            case 'I':
                return true;
            case 'O':
                return true;
            case 'U':
                return true;
            default:
                return false;
        }
    }
}

3. Reverse String II

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.

Example:

Input: s = "abcdefg", k = 2
Output: "bacdfeg"
1.
class Solution {
     public String reverseStr(String s, int k) {
        char[] arr = s.toCharArray();
        int n = arr.length;
        int i = 0;
        while(i < n) {
            int j = Math.min(i + k - 1, n - 1);
            swap(arr, i, j);
            i += 2 * k;
        }
        return String.valueOf(arr);
    }
    private void swap(char[] arr, int l, int r) {
        while (l < r) {
            char temp = arr[l];
            arr[l++] = arr[r];
            arr[r--] = temp;
        }
    }
}

4. Reverse Words in a String III

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
1.
class Solution {
    public String reverseWords(String s) {
        String [] strings = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for(int i  = 0 ; i< strings.length ; i++) {
            sb.append(reverse(strings[i]) + " ");
        }
        return sb.toString().trim();
    }
    public String reverse(String str) {
        int low = 0;
        int high = str.length() - 1;
        char [] res = str.toCharArray();
        while(low < high) {
            char tmp = res[low];
            res[low++] = res[high];
            res[high--] = tmp;
        }
        return new String(res);
    }
}

5. Happy Number

Write an algorithm to determine if a number is "happy".

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100

12 + 02 + 02 = 1

1. 利用了set的特性,add方法如果有重复的返回false,比较巧

class Solution {
    public static  boolean isHappy(int n) {
       Set<Integer> set = new HashSet<>();
        int res ,tmp;
        while(set.add(n)) {
            res = 0;
            while(n > 0) {
                tmp = n % 10;
                res += tmp * tmp;
                n /= 10;
            }
            if(res == 1) {
                return true;
            }else {
                n = res;
            }
        }
        return false;
    }
}

6. Ugly Number

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.

class Solution {
    public boolean isUgly(int num) {
        if(num <= 0) {
            return false;
        }
       for(int i = 2; i < 6; i++) {
           while(num % i == 0) {
               num /= i;
           }
       }
        return num == 1;
    }
}
原文地址:https://www.cnblogs.com/qjx-2016/p/7799098.html