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. Android N(7.0) 在ListView里显示EditText时软键盘弹出时会自动切换到全键盘的问题?

    Android N(7.0) 在ListView里显示EditText时软键盘弹出时会自动切换到全键盘的问题? 问题症状描述 Activity 在AndroidManifest.xml里设置andro ...

  2. hdu5305Friends dfs

    //给一个无向图 , 每条边能够是online边也能够是offline边,问 //有多少种方法使得每一个节点的online边和offline边一样多 #include<cstdio> #i ...

  3. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  4. 李洪强iOS开发之OC[006] - 类和对象

  5. Yarn源码分析之MapReduce作业中任务Task调度整体流程(一)

    v2版本的MapReduce作业中,作业JOB_SETUP_COMPLETED事件的发生,即作业SETUP阶段完成事件,会触发作业由SETUP状态转换到RUNNING状态,而作业状态转换中涉及作业信息 ...

  6. socket心跳检测

    一.什么是心跳检测 判断对方(设备,进程或其它网元)是否正常动行,一般采用定时发送简单的通讯包,如果在指定时间段内未收到对方响应,则判断对方已经当掉.用于检测TCP的异常断开. 基本原因是服务器端不能 ...

  7. 【JavaEE】Springmvc+Spring+Hibernate整合及example

    前面两篇文章,分别介绍了Springmvc和Spring的搭建方法,本文再搭建hibernate,并建立SSH最基本的代码结构. Hibernate和前面两个比就比较复杂了,Hibernate是一个o ...

  8. javascript拼接html代码

    转自开源中国社区:http://www.oschina.net/code/snippet_94055_21640经常做jsp开发的朋友可能遇到一个情况,显示列表数据不是table,而是div或者其他很 ...

  9. cvsba-1.0.0/utils/test_cvsba.cpp:.......error: ‘numeric_limits’ is not a member of ‘std’... error: expected primary-expression before ‘float’....

    cvsba库http://www.uco.es/investiga/grupos/ava/node/39,不知道怎么回事,记得以前编译没有错误,不知道作者是否更新了还是怎么着,新的现在有以下错误: 解 ...

  10. python静态网页爬虫之xpath(简单的博客更新提醒功能)

    直接上代码: #!/usr/bin/env python3 #antuor:Alan #-*- coding: utf-8 -*- import requests from lxml import e ...