LeetCode刷刷记录
一遍考研,一遍还是要刷刷题。感觉自己的时间安排的不是很好,还是要抓紧自己的日常时间,当然,也要练练刷题的手感。
1.第一题就两重循环找到索引就OK,因为是无序的,所以就不能用二分来查找,题目中每个数的下标是定死的,所以不能排序后再二分。真是太年轻,什么都想试试(4.5)
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] arr = new int[2];
int cnt = 0;
for(int i = 0; i < nums.length; ++i){
for(int j = i+1; j < nums.length; ++j) {
if(nums[i] + nums[j] == target){
arr[cnt] = i;
cnt++;
arr[cnt] = j;
cnt++;
}
}
}
return arr;
}
}
2.第二题就是个简单的java单链表,将两个链表合成一个链表。需要简单考虑一下进位问题,调试还是调试了一会儿,链表又有段时间没用过了,想了半天。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int t1, t2;
ListNode head;
t1 = (l1.val + l2.val) % 10;
ListNode l = new ListNode(t1);
head = l;
t2 = (l1.val + l2.val) / 10;
while(l1.next != null && l2.next != null){
l1 = l1.next;
l2 = l2.next;
t1 = (l1.val + l2.val + t2) % 10;
t2 = (l1.val + l2.val + t2) / 10;
ListNode ltmp = new ListNode(t1);
l.next = ltmp;
l = l.next;
}
while(l1.next!=null){
l1 = l1.next;
t1 = (l1.val + t2) % 10;
t2 = (l1.val + t2) / 10;
ListNode ltmp = new ListNode(t1);
l.next = ltmp;
l = l.next;
}
while(l2.next != null) {
l2 = l2.next;
t1 = (l2.val + t2) % 10;
t2 = (l2.val + t2) / 10;
ListNode ltmp = new ListNode(t1);
l.next = ltmp;
l = l.next;
}
if(t2 != 0){
ListNode ltmp = new ListNode(t2);
l.next = ltmp;
l = l.next;
}
return head;
}
}
我去,返回头看自己以前的代码,感觉昨天写的好搓啊
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode ret = new ListNode(0);
ListNode cur = ret;
int sum = 0;
while(true) {
if(l1 != null) {
sum += l1.val;
l1 = l1.next;
}
if(l2 != null) {
sum += l2.val;
l2 = l2.next;
}
cur.val = sum % 10;
sum /= 10;
if(l1 != null || l2 != null || sum != 0) {
//cur = (cur.next = new ListNode(0));
cur.next = new ListNode(0);
cur = cur.next;
} else {
break;
}
}
return ret;
}
}
3.第37题sudu,这个题目好吧,我猥琐的用了之前的代码,就dfs就可以
package LeetCode; /**
* Created by lenovo on 2016-04-07.
*/ public class Solution {
static int[][] row = new int[9][9];
static int[][] col = new int[9][9];
static int[][] per = new int[9][9];
static int[][] map = new int[9][9];
static boolean isFind = false; public void solveSudoku(char[][] board) {
fill(row);
fill(col);
fill(per);
isFind = false;
int k;
for(int i = 0; i < 9; ++i)
for(int j = 0; j < 9; ++j){
if(board[i][j] != '.'){
k = board[i][j] - '0';
map[i][j] = k - 1;
if(k != 0){
row[i][k-1] = col[j][k-1] =
per[(i/3)*3+(j/3)][k-1] = 1;
}
}else{
map[i][j] = -1;
}
} dfs(0, 0, board);
}
static void fill(int[][] a){
for(int i = 0; i < 9; ++i){
for(int j = 0; j < 9; ++j){
a[i][j] = 0;
}
}
}
public static void dfs(int x, int y, char[][] board){
int u = x * 9 + y + 1;
if(x == 9){
isFind = true;
for(int i = 0; i < 9; ++i)
for(int j = 0; j < 9; ++j)
board[i][j] = (char)(map[i][j] + '0' + 1);
}
if(isFind) return;
if(map[x][y] != -1){
dfs(u/9, u%9, board);
return;
} for(int i = 0; i < 9 && !isFind; ++i){
int k = (x/3)*3 + y/3;
if(row[x][i] == 0 && col[y][i] == 0 && per[k][i] == 0){
row[x][i] = col[y][i] = per[k][i] = 1;
map[x][y] = i; dfs(u/9, u%9, board); row[x][i] = col[y][i] = per[k][i] = 0;
map[x][y] = -1;
}
}
} public static void main(String[] args){
char[][] board = { {'.','.','9','7','4','8','.','.','.'},
{'7','.','.','.','.','.','.','.','.'},
{'.','2','.','1','.','9','.','.','.'},
{'.','.','7','.','.','.','2','4','.'},
{'.','6','4','.','1','.','5','9','.'},
{'.','9','8','.','.','.','3','.','.'},
{'.','.','.','8','.','3','.','2','.'},
{'.','.','.','.','.','.','.','.','6'},
{'.','.','.','2','7','5','9','.','.'}}; Solution s = new Solution();
s.solveSudoku(board);
}
}
但是,这个题目我有疑惑,并不是算法的疑惑,而是java中static变量的疑惑。因为有过用java写面向过程的代码(好吧,是java的语法问题,关于static的,以前用一直都没有问题,然后今天就有问题了)然后好好找下,看看(4.7)
LeetCode刷刷记录的更多相关文章
- LeetCode解题记录(贪心算法)(二)
1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等 ...
- Leetcode解题记录
尽量抽空刷LeetCode,持续更新 刷题记录在github上面,https://github.com/Zering/LeetCode 2016-09-05 300. Longest Increasi ...
- mysql——leetcode问题记录
问题: 表1: Person +-------------+---------+ | 列名 | 类型 | +-------------+---------+ | PersonId | int | | ...
- [leetcode] 题解记录 11-20
博客园markdown太烂, 题解详情https://github.com/TangliziGit/leetcode/blob/master/solution/11-20.md Leetcode So ...
- [leetcode] 题解记录 1-10
博客园markdown太烂, 题解详见https://github.com/TangliziGit/leetcode/blob/master/solution/1-10.md Leetcode Sol ...
- LeetCode解题记录(贪心算法)(一)
1. 前言 目前得到一本不错的算法书籍,页数不多,挺符合我的需要,于是正好借这个机会来好好的系统的刷一下算法题,一来呢,是可以给部分同学提供解题思路,和一些自己的思考,二来呢,我也可以在需要复习的时候 ...
- [leetcode解题记录]Jump Game和Jump Game II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- LeetCode解题记录(双指针专题)
1. 算法解释 双指针主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务.也可以延伸到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,则也称为滑动窗口(两个指针包围的区域 ...
- Leetcode Scratching Recorder
Author: Zhong-Liang Xiang Start from: August 7th, 2017 这个题为连滚带爬的Leetcode填坑记录就这么开始了. 网上看了看, 一堆小伙从2014 ...
随机推荐
- JS判断字符串长度(中文长度为2,英文长度为1)
目的:计算字符串长度(英文占1个字符,中文汉字占2个字符) 方法一: String.prototype.gblen = function() { var len = 0; for (var i=0; ...
- Func
Func<List<int>, string> getStr = (list) => { var returnStr = ""; if (list.A ...
- bzoj3744 Gty的妹子序列
我是萌萌的传送门 感觉这题还是不错的--虽然其实算是比较水的题= = 首先分块,令f[i][j]表示第i块到第j块的逆序对数,询问的时候直接计算不完整块与完整块以及不完整块之间的逆序对. 不完整块之间 ...
- Winform自定义控件基础(一)
1.设置图像和文字以抗锯齿的方式呈现 g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = TextRenderingHin ...
- ORACLE发送带附件邮件的二三事之一
在oracle使用过程中,我们可以通过pl/sql生成数据文件,也可以通过spool on spool off生成,但某些环境下,我们需要通过存储过程处理数据,数据处理完,需要自动生成数据文件,手工导 ...
- mmap为什么比read/write快(兼论buffercache和pagecache)
参考文献: <从内核文件系统看文件读写过程>http://www.cnblogs.com/huxiao-tee/p/4660352.html?utm_source=tuicool& ...
- 接触PHP快4个月
就要下班了,接触php快4个月,掌握的不好,需要实战,看到自己博客空空的,就mark一下吧!下班了...
- C#单例模式的多种写法
它的主要特点不是根据客户程序调用生成一个新的实例,而是控制某个类型的实例数量-唯一一个.(<设计模式-基于C#的工程化实现及扩展>,王翔).也就是说,单例模式就是保证在整个应用程序的生命周 ...
- Object-c 内存管理
内存管理 主要内容 1.内存管理的概念 2.引用计数 3.如何持有对象所有权 4.自动释放池 5.@property的使用 什么是内存管理 内存管理是关于如何管理对象生 ...
- CSS3简易表盘时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...