1、和为S的两个数字
public class Solution {
public static void main(String[] args) {
System.out.println(new Solution().FindNumbersWithSum(new int[]{1, 2, 4, 3, 9}, 3));
}
public ArrayList<Integer> FindNumbersWithSum(int [] array, int sum) {
ArrayList<Integer> ret = new ArrayList<>();
int left = 0, right = array.length - 1;
while (left < right) {
if (sum == array[left] + array[right]) {
ret.add(array[left]);
ret.add(array[right]);
return ret;
} else if (sum > array[left] + array[right]) {
left++;
} else {
right--;
}
}
return ret;
}
}
2、和为S的连续正数序列
/**
* 和为S的连续正数序列
*
* 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,
* 他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,
* 他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,
* 你能不能也很快的找出所有和为S的连续正数序列?
*
*
* @author maxinze
*/
public class Solution {
public static void main(String[] args) {
System.out.println(new Solution().FindContinuousSequence(100));
}
public ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
int low = 1, high = 2, curnum = 3;
while (high < sum) {
if (curnum > sum) {
curnum -= low;
low++;
} else if (curnum < sum) {
high++;
curnum += high;
} else {
ArrayList<Integer> tmp = new ArrayList<>();
for (int i = low; i <= high; i++) {
tmp.add(i);
}
ret.add(tmp);
curnum -= low;
low++;
high++;
curnum += high;
}
}
return ret;
}
}
3、左旋转字符串
public class Solution {
public static void main(String[] args) {
// System.out.println(new Solution().LeftRotateString("abcXYZdef", 3));
System.out.println(new Solution().LeftRotateString("abc", 10));
}
public String LeftRotateString(String str, int n) {
// 边界
if (str.length() == 0 || str == null) {
return "";
}
// n > str.length()
if (str.length() < n) {
n = str.length() / 2;
}
char[] chars = str.toCharArray();
reverse(chars, 0, n - 1);
reverse(chars, n, chars.length - 1);
reverse(chars, 0, chars.length - 1);
return new String(chars);
}
private void reverse(char[] chars, int i, int j) {
while (i < j) swap(chars, i++, j--);
}
private void swap(char[] chars, int i, int j) {
char t = chars[i];
chars[i] = chars[j];
chars[j] = t;
}
public String LeftRotateString2(String str, int n) {
// 解法1优化:n取模来避免无用遍历
// 边界
if (str.length() == 0 || str == null) {
return "";
}
// 取模
n %= str.length();
// 创建sb
StringBuilder sb = new StringBuilder(str);
// 循环n次,每次左移一位
while (n > 0) {
// 取第一位
char temp = sb.charAt(0);
// 删除第一位
sb.deleteCharAt(0);
// 添加至最后一位
sb.append(temp);
n--;
}
return sb.toString();
}
}
4、翻转单词顺序列
/**
* 先翻转每个单词,再翻转整个字符串。
* <p>
* 题目应该有一个隐含条件,就是不能用额外的空间。虽然 Java 的题目输入参数为 String 类型,
* 需要先创建一个字符数组使得空间复杂度为 O(N),但是正确的参数类型应该和原来一样,为字符数组,
* 并且只能使用该字符数组的空间。任何使用了额外空间的解法在面试时都会大打折扣,包括递归解法。
*
* @author maxinze
*/
public class Solution {
public static void main(String[] args) {
System.out.println(new Solution().ReverseSentence("nowcoder. a am I"));
}
public String ReverseSentence(String str) {
int n = str.length();
int j = 0, i = 0;
char[] chars = str.toCharArray();
while(j <= n) {
//这条件是 chars[j] == ' '
if(j == n || chars[j] == ' ') {
reverse(chars, i, j - 1);
i = j + 1;
}
j++;
}
reverse(chars, 0, n - 1);
return new String(chars);
}
private void reverse(char[] c, int i, int j) {
while (i < j)
swap(c, i++, j--);
}
private void swap(char[] c, int i, int j) {
char tmp = c[i];
c[i] = c[j];
c[j] = tmp;
}
}
代码仓库:https://gitee.com/codingce/codingce-leetcode