Skip to content

Commit fbd3cb2

Browse files
committed
2020-9-5
1 parent 22f6355 commit fbd3cb2

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed
File renamed without changes.
File renamed without changes.

Java/67.二进制求和.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* @lc app=leetcode.cn id=67 lang=java
3+
*
4+
* [67] 二进制求和
5+
*
6+
* https://leetcode-cn.com/problems/add-binary/description/
7+
*
8+
* algorithms
9+
* Easy (45.54%)
10+
* Total Accepted: 19K
11+
* Total Submissions: 40.3K
12+
* Testcase Example: '"11"\n"1"'
13+
*
14+
* 给定两个二进制字符串,返回他们的和(用二进制表示)。
15+
*
16+
* 输入为非空字符串且只包含数字 1 和 0。
17+
*
18+
* 示例 1:
19+
*
20+
* 输入: a = "11", b = "1"
21+
* 输出: "100"
22+
*
23+
* 示例 2:
24+
*
25+
* 输入: a = "1010", b = "1011"
26+
* 输出: "10101"
27+
*
28+
*/
29+
class Solution {
30+
public String addBinary(String a, String b) {
31+
int i=a.length();int j=b.length();
32+
int flag=0;//进位
33+
String result="";
34+
while(i>0&&j>0){
35+
i--;
36+
j--;
37+
char ca=a.charAt(i);
38+
char cb=b.charAt(j);
39+
int t=Integer.valueOf(ca-'0')+Integer.valueOf(cb-'0')+flag;
40+
flag=t/2;
41+
result=Integer.toString(t%2)+result;
42+
}
43+
while(i>0){
44+
i--;
45+
char ca=a.charAt(i);
46+
int t=Integer.valueOf(ca-'0')+flag;
47+
flag=t/2;
48+
result=Integer.toString(t%2)+result;
49+
}
50+
while(j>0){
51+
j--;
52+
char cb=b.charAt(j);
53+
int t=Integer.valueOf(cb-'0')+flag;
54+
flag=t/2;
55+
result=Integer.toString(t%2)+result;
56+
}
57+
while(flag>0){
58+
result=Integer.toString(flag%2)+result;
59+
flag/=2;
60+
}
61+
return result;
62+
}
63+
}
64+
65+

Java/69.x-的平方根.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode.cn id=69 lang=java
3+
*
4+
* [69] x 的平方根
5+
*
6+
* https://leetcode-cn.com/problems/sqrtx/description/
7+
*
8+
* algorithms
9+
* Easy (33.78%)
10+
* Total Accepted: 27.3K
11+
* Total Submissions: 77.8K
12+
* Testcase Example: '4'
13+
*
14+
* 实现 int sqrt(int x) 函数。
15+
*
16+
* 计算并返回 x 的平方根,其中 x 是非负整数。
17+
*
18+
* 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
19+
*
20+
* 示例 1:
21+
*
22+
* 输入: 4
23+
* 输出: 2
24+
*
25+
*
26+
* 示例 2:
27+
*
28+
* 输入: 8
29+
* 输出: 2
30+
* 说明: 8 的平方根是 2.82842...,
31+
* 由于返回类型是整数,小数部分将被舍去。
32+
*
33+
*
34+
*/
35+
class Solution {//牛顿迭代法
36+
public int mySqrt(int x) {
37+
if (x == 0) return 0;
38+
double last = 0;
39+
double res = 1;
40+
while (res != last){
41+
last = res;
42+
res = (res + x / res) / 2;
43+
}
44+
return (int)res;
45+
}
46+
}
47+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @lc app=leetcode.cn id=83 lang=java
3+
*
4+
* [83] 删除排序链表中的重复元素
5+
*
6+
* https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/
7+
*
8+
* algorithms
9+
* Easy (43.08%)
10+
* Total Accepted: 19.8K
11+
* Total Submissions: 44.4K
12+
* Testcase Example: '[1,1,2]'
13+
*
14+
* 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
15+
*
16+
* 示例 1:
17+
*
18+
* 输入: 1->1->2
19+
* 输出: 1->2
20+
*
21+
*
22+
* 示例 2:
23+
*
24+
* 输入: 1->1->2->3->3
25+
* 输出: 1->2->3
26+
*
27+
*/
28+
/**
29+
* Definition for singly-linked list.
30+
* public class ListNode {
31+
* int val;
32+
* ListNode next;
33+
* ListNode(int x) { val = x; }
34+
* }
35+
*/
36+
class Solution {
37+
public ListNode deleteDuplicates(ListNode head) {
38+
if(head==null)return head;
39+
ListNode p=head;
40+
int nowval=head.val;
41+
while(p.next!=null){
42+
ListNode pre=p;
43+
p=p.next;
44+
if(p.val!=nowval){
45+
nowval=p.val;
46+
continue;
47+
}else{
48+
//删除节点
49+
pre.next=p.next;
50+
p=pre;
51+
}
52+
}
53+
return head;
54+
}
55+
}
56+

0 commit comments

Comments
 (0)