题目描述
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
public class Solution {
public int Sum_Solution(int n) {
int sum = (int)Math.pow(n,2)+n;
return sum>>1;
}
题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
import java.util.ArrayList;
import java.util.*;
public class Solution {
public ArrayList<String> Permutation(String str) {
ArrayList<String> result = new ArrayList<>();
if(str.length()==0 ||str == null){
return result;
}
PermutationHelper(str.toCharArray(),0,result);
Collections.sort(result);
return result;
}
private void PermutationHelper(char[] chars,int i,ArrayList<String> result){
//已经递归到了字符串最后一位,判断集合中有没有这个字符串,没有则加入
if(i== chars.length-1){
if(!result.contains(new String(chars))){
result.add(new String(chars));
return;
}
}else{
//首次传进来的i为0,代表首位字符
//依次处理i与i后面的每个字符(索引j)交换
for(int j=i; j<chars.length; j++){
swap(chars,i,j);//交换
PermutationHelper(chars,i+1,result);//继续递归交换后的子串
swap(chars,i,j);//还原
}
}
}
private void swap(char[] chars,int i,int j){
if(i != j){
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
};
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<TreeNode> treelist = new ArrayList<TreeNode>();
ArrayList<Integer> tlist = new ArrayList<Integer> ();
if(root == null){
return tlist;
}
if(root!= null){
treelist.add(root);
tlist.add(root.val);
}
for (int i = 0; i<treelist.size(); i++){
TreeNode node = treelist.get(i);
if(node.left != null){
treelist.add(node.left);
tlist.add(node.left.val);
}
if(node.right != null){
treelist.add(node.right);
tlist.add(node.right.val);
}
}
return tlist;
}
}
题目描述
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if (root1 ==null)
return false;
if(root2 ==null)
return false;
return isSubtree(root1, root2)||HasSubtree(root1.left, root2)||HasSubtree(root1.right, root2);
}
public static boolean isSubtree(TreeNode root1,TreeNode root2){
if (root2 == null)
return true;
if(root1 ==null){
return false;
}
if(root1.val == root2.val){
return isSubtree(root1.left, root2.left)&&isSubtree(root1.right, root2.right);
}
return false;
}
}
题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode list = null;
if(list1 ==null&&list2 == null)
return null;
if(list1 == null)
return list2;
if(list2 == null)
return list1;
if(list1.val <=list2.val){
list = list1;
list.next = Merge(list1.next, list2);
}else
{
list = list2;
list.next = Merge(list1, list2.next);
}
return list;
}
}
题目描述
输入一个链表,反转链表后,输出新链表的表头。
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null)
return null;
ListNode pre = null;
ListNode next = null;
while(head!= null){
//先用next保存下一节点的信息
next = head.next;
//保存完next,就可以让head从指向next变成指向pre了
head.next = pre;
//head指向pre后,就继续依次反转下一个节点
//让pre,head,next依次向后移动一个节点,继续下一次的指针反转
pre = head;
head = next;
}
//如果head为null的时候,pre就为最后一个节点了,但是链表已经反转完毕,pre就是反转后链表的第一个节点
return pre;
}
}
题目描述
输入一个链表,输出该链表中倒数第k个结点。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if (head == null)
return null;
ListNode p1 = head;
ListNode p2 = head;
while(k>0&&p2!=null){
p2 = p2.next;
k--;
}
if(p2==head || (p2==null&&k>0))
return null;
while(p2!= null){
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
}
题目描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
//冒泡排序法
public class Solution {
public void reOrderArray(int [] array) {
int len = array.length;
for (int i =0; i<len; i++){
for(int j=0;j<len-1; j++){
if(array[j]%2==0&&array[j+1]%2!=0){
int temp = array[j];
array[j] = array[j+1];
array[j+1]=temp;
}
}
}
}
}
题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0,第1项是1)。
(n<=39)
class Solution {
public:
int Fibonacci(int n) {
if(n<=1)
return n;
int res = 0;
int n1 =0;
int n2 =1;
for (int i =2; i<=n; i++){
res = n1+n2;
n1=n2;
n2= res;
}
return res;
}
};
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array == null||array.length ==0)
return 0;
if(array.length ==1)
return array[0];
for(int i = 0; i<array.length;i++){
if(array[i]>array[i+1])
return array[i+1];
}
return 0;
}
}
题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
public class Solution {
public int JumpFloor(int target) {
if(target ==1)
return 1;
if(target ==2)
return 2;
return JumpFloor(target-1)+JumpFloor(target-2);
}
}
题目描述
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int row = (int)array.size();
int col = (int)array[0].size();
int i=0;
int j=col-1;
//0row and 0col
if(row==0||col==0){
return false;
}
if(target>array[row-1][col-1]||target<array[0][0])
{
return false;
}
while(i<row&&j>=0){
if(target>array[i][j]){
i++;
}
else if(target<array[i][j]){
j--;
}
else{
return 1;
}
}
return 0;
}
};
题目描述
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
/*
分析
将长度为1的空格替换为长度为3的“%20”,字符串的长度变长。
如果允许我们开辟一个新的数组来存放替换空格后的字符串,
那么这道题目就非常简单。设置两个指针分别指向新旧字符串首元素,
遍历原字符串,如果碰到空格就在新字符串上填入“%20”,
否则就复制元字符串上的内容。但是如果面试官要求
在原先的字符串上操作,并且保证原字符串有足够长的空间来存放替换后的字符串,
那么我们就得另想方法。
如果从前往后替换字符串,那么保存在空格后面的字符串肯定会被覆盖,
那么我们就考虑从后往前进行替换。
首先遍历原字符串,找出字符串的长度以及其中的空格数量,
根据原字符串的长度和空格的数量我们可以求出最后新字符串的长度。
设置两个指针point1和point2分别指向原字符串和新字符串的末尾位置。
如果point1指向内容不为空格,那么将内容赋值给point2指向的位置,
如果point1指向为空格,那么从point2开始赋值“02%”
直到point1==point2时表明字符串中的所有空格都已经替换完毕。
*/
class Solution {
public:
void replaceSpace(char *str,int length) {
//遍历出原字符串长度和空格个数
int oldstringlen;
int blankNum = 0;
for(oldstringlen = 0; str[oldstringlen]!='\0'; oldstringlen++){
if(str[oldstringlen] == ' '){
blankNum++;
}
}
//计算出新字符串的长度
int newstringLen = oldstringlen + blankNum * 2 ;
//int newstringLen = oldstringLen + blankNumber * 2;
if(newstringLen > length){
return;
}
str[newstringLen] = '\0'; //字符串最后一位置为\0
int point1 = oldstringlen -1;
int point2 = newstringLen -1;
while(point1 >=0 && point2>point1){
if(str[point1] == ' '){
str[point2--] = '0';
str[point2--] = '2';
str[point2--] = '%';
}
else{
str[point2--] = str[point1];
}
point1--;
}
}
};
题目描述
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
利用递归以及&&的短路原理:
前面的限制条件,表明最好的方法是用递归的方法,但是需要做一个判断,从n到1,当小于1的时候,自动结束
class Solution {
public:
int Sum_Solution(int n) {
int sum = n;
sum&&(sum+=Sum_Solution(n-1));
return sum;
}
};