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;
}
}
最新文章
- [vijos P1040] 高精度乘法
- Google Protocol Buffer 的使用和原理[转]
- xml文档解析
- Linux驱动框架之framebuffer驱动框架
- HTML5:离线存储(缓存机制)-IndexDB
- BZOJ1230 [Usaco2008 Nov]lites 开关灯
- openstack与VMware workStation的区别
- Ubuntu 修改时间
- JAVA基础--线程
- Java Sftp上传下载文件
- 基础二 day4
- HDU 4918 Query on the subtree(动态点分治+树状数组)
- A - ACM Computer Factory(网络流)
- Laravel4.2取得配置文件值
- jquery超炫的列表筛选插件
- VMware提示无法打开内核设备 \\.\Global\vmx86: 系统找不到指定的文件解决方案
- Android(java)学习笔记209:Android线程形态之 HandlerThread
- 基于tcp交互的python聊天程序
- 动手动脑:String.equals()的使用方法
- 网络--路由表&;IP选路
热门文章
- Atitit。Cas机制&#160;软件开发&#160;编程语言&#160;无锁机制&#160;java&#160;c#&#160;php
- 设计模式之里氏替换原则(LSP)
- mysql数据库记录
- 解决Eclipse的dropins中svn插件不能加载的问题
- oracle中查看sql语句的执行计划
- C# 委托和Lambda表达式
- 文件上传下载:commons-fileupload + Servlet 2.5
- uboot下tftp传输文件
- ubuntu 12.10 apt-get 源
- 解决easyui tabs中href无法跨域跳转