今天开始刷leetcode,学习android学累就做做题目,为了大四毕业更好的找到工作而努力!
题目
1.
Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given [0,1,2,4,5,7], return [“0->2”,”4->5”,”7”].
他的意思是在一个有序的数组中进行子数组归类的意思,看上面那个例子:
0,1,2是连续的,归为一个区间,这么讲比较好理解。
4,5连续,归为一个区间
7为单独的,没有连续,所以就一个7.
解题思路:
1.判断是否连续:nums[j]+1==nums[j+1],则为连续的,一直往下找,找到nums[j]+1!=nums[j+1]即是一个区间,j++,依次类推。
public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> list=new ArrayList<>();
int j=0,i;
for (i=j; i < nums.length; i++)
{
int start=nums[i],end=nums[i];
for (j = i; j < nums.length; j++)
{
if (j==nums.length-1) {
end=nums[j];
i=j++;
break;
}
if ((nums[j]+1)!=nums[j+1]) {
end=nums[j];
i=j++;
break;
}
}
if (start==end) {
list.add(start+"");
}
else {
list.add(start+"->"+end);
}
}
return list;
}
leetcode判断结果:
2.
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
意思是给点一个非负整数,各个位数相加直到相加的数字<10为止。
public class Solution {
public int addDigits(int q) {
int sum=0;
while(true)
{
while(q>0)
{
sum+=(q%10);
q/=10;
}
if (sum>=10) {
q=sum;
sum=0;
}
else {
return sum;
}
}
}
}
3.
Given a singly linked list, determine if it is a palindrome.
题目意思是给你一个单链表,让你判断是不是回文。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head)
{
Stack<Integer> stack=new Stack<>();
if (head==null||head.next==null) {
return true;
}
ListNode node=head;
ListNode nodequick=head.next;
//链表长度是否是偶数
boolean flag=true;
while(nodequick!=null)
{
stack.push(node.val);
if (nodequick.next==null) {
break;
}
else if (nodequick.next.next==null) {
flag=false;
break;
}
else {
nodequick=nodequick.next.next;
}
node=node.next;
}
node=node.next;
if (flag)
{
while(node!=null&&!stack.isEmpty())
{
if (stack.pop()!=node.val) {
return false;
}
node=node.next;
}
if (stack.isEmpty()&&node==null) {
return true;
}
else {
return false;
}
}
else {
node=node.next;
while(node!=null&&!stack.isEmpty())
{
if (stack.pop()!=node.val) {
return false;
}
node=node.next;
}
if (stack.isEmpty()&&node==null) {
return true;
}
else {
return false;
}
}
}
}
结果:
Determine whether an integer is a palindrome. Do this without extra space.
大意就是给你一个数字,让你判断是否是回文。
思路就是反转数字,比较反转后的数字是否等于原来的数字
public class Solution {
public boolean isPalindrome(int x) {
int result=0,num=x;
while(num>0)
{
result=num%10+result*10;
num/=10;
System.out.println(num);
}
if (result==x) {
return true;
}
return false;
}
}
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,不为1继续判断,当出现相同的数字的时候则会无限循环下去,这里break或者return就可以了
public class Solution {
public boolean isHappy(int n) {
int x=n;
int result=0;
int i=1;
List<Integer> list=new ArrayList<>();
while (result!=1)
{
int j=0;
result=0;
while(x>0)
{
j=x%10;
result=result+j*j;
x/=10;
}
if (list.contains(result))
{
return false;
}
list.add(result);
x=result;
i++;
}
return true;
}
}