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

LeetCode-11-7的更多相关文章

  1. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  2. [LeetCode] 11. Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  3. LeetCode 11 水池蓄水问题

    今天给大家分享的是一道LeetCode中等难度的题,难度不大,但是解法蛮有意思.我们一起来看题目: Link Container With Most Water Difficulty Medium 题 ...

  4. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

  5. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  6. LeetCode 11

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

  7. LeetCode——11. Container With Most Water

    一.题目链接:https://leetcode.com/problems/container-with-most-water/ 二.题目大意: 给定n个非负整数a1,a2....an:其中每一个整数对 ...

  8. LeetCode 11 Container With Most Water(分支​判断问题)

    题目链接 https://leetcode.com/problems/container-with-most-water/?tab=Description   Problem: 已知n条垂直于x轴的线 ...

  9. LeetCode(11)题解: Container With Most Water

    https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, . ...

  10. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

随机推荐

  1. 开源管理系统OSSIM设置 语言为中文简体

    最近研究OSSIM系统,OSSIM的安装是做好的ISO,操作系统选择的是CentOS 64Bit系统.我使用的OSSIM 4.11 的ISO安装,虽然系统说明支持中文,实际上,只是台湾的繁体中文而以. ...

  2. 怎样优雅的研究 RGSS3 番外(一) ruby 实现的后缀自己主动机

    *我真的不会 ruby 呀* #encoding:utf-8 #==================================================================== ...

  3. mysql bin-log三种模式

    MySQL的bin-log日志备份有三种模式,分别是:ROW.Statement.Mixed 一.Row 日志会记录成每一行数据被修改成的形式,然后再slave端再对相同的数据进行修改,只记录要修改的 ...

  4. library not found for -lPods-AFNetworking解决放案

    出现library not found for -lPods-AFNetworking这个报错, 来自于我从git上面把我项目直接Download下来的,我的项目里面用了CocoaPods的,如今pr ...

  5. C/C++知识要点4——printf函数以及cout的计算顺序

    printf函数的计算顺序:先从右到左压栈,然后从左到右出栈. 例程: #include"stdio.h" int main() { int arr[] = { 1, 2, 3, ...

  6. 用jquery替换dojo中的ajax

    function getpoints(closeid) {/*获取数据列表*/ var closesid = closeid; $.ajax({ url:'*.ashx") %>?op ...

  7. nginx+python+fastcgi环境配置(flup版本)

    昨天花了一整天的时间研究搭建了nginx+python+fastcgi环境,并测试没问题,由于是第一次,并且参考了网上很多东西,网上也有很多,但还是把自己的过程记录下. 主要感谢这位兄弟的文章给了我很 ...

  8. 前端webview开发中遇到的一些问题及其解决方法

    最近做了一个webview中的兑换页面,本来以为很简单,想不到遇到了远远超出预期数量的BUG,记下来,以备后患. 1 inline-block元素折行 BUG描述:现在我有三个DIV,要在一列等宽排列 ...

  9. Win10系统如何配置Tomcat环境变量

    我们知道win10用户在配置Tomcat环境变量的时候,首先需要配置JAVA,这样才能配置Tomcat环境.很多用户并不知道要如何进行配置,下面就给大家介绍win10系统怎样Tomcat环境变量的. ...

  10. JavaScript 函数语法

    函数就是包裹在花括号中的代码块,前面使用了关键词 function: function functionname() { 这里是要执行的代码 } 当调用该函数时,会执行函数内的代码. 可以在某事件发生 ...