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

最新文章

  1. [vijos P1040] 高精度乘法
  2. Google Protocol Buffer 的使用和原理[转]
  3. xml文档解析
  4. Linux驱动框架之framebuffer驱动框架
  5. HTML5:离线存储(缓存机制)-IndexDB
  6. BZOJ1230 [Usaco2008 Nov]lites 开关灯
  7. openstack与VMware workStation的区别
  8. Ubuntu 修改时间
  9. JAVA基础--线程
  10. Java Sftp上传下载文件
  11. 基础二 day4
  12. HDU 4918 Query on the subtree(动态点分治+树状数组)
  13. A - ACM Computer Factory(网络流)
  14. Laravel4.2取得配置文件值
  15. jquery超炫的列表筛选插件
  16. VMware提示无法打开内核设备 \\.\Global\vmx86: 系统找不到指定的文件解决方案
  17. Android(java)学习笔记209:Android线程形态之 HandlerThread
  18. 基于tcp交互的python聊天程序
  19. 动手动脑:String.equals()的使用方法
  20. 网络--路由表&amp;IP选路

热门文章

  1. Atitit。Cas机制&#160;软件开发&#160;编程语言&#160;无锁机制&#160;java&#160;c#&#160;php
  2. 设计模式之里氏替换原则(LSP)
  3. mysql数据库记录
  4. 解决Eclipse的dropins中svn插件不能加载的问题
  5. oracle中查看sql语句的执行计划
  6. C# 委托和Lambda表达式
  7. 文件上传下载:commons-fileupload + Servlet 2.5
  8. uboot下tftp传输文件
  9. ubuntu 12.10 apt-get 源
  10. 解决easyui tabs中href无法跨域跳转